【问题标题】:how to get multiple latitudinal and longitude values and mark them with markers on the map如何获取多个纬度和经度值并在地图上用标记进行标记
【发布时间】:2019-09-23 16:18:23
【问题描述】:

我想在服务器上调出经纬度,在google map上用记号笔做标记,怎么办?

stopMapActivity



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stop_map);
        nextBtn = findViewById(R.id.addRouteButton);

        FragmentManager fragmentManager = getFragmentManager();
        MapFragment mapFragment = (MapFragment) fragmentManager
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);


        ssm = new ServerSideMethods(this,false);
        stop_list = new ArrayList<stop_list_item>();

        new Thread(new Runnable(){
            @Override
            public void run() {
                Message msg = new Message();
                msg.what = 0;
                try{
                    stop_list = ssm.getCarStopList(17900);
                    msg.what = 1;
                    for (int i = 0; i < stop_list.size(); i++) {
                        MarkerOptions makerOptions = new MarkerOptions();
                        makerOptions
                                .position(new LatLng(stopListArray.get(i).get("x"), stopListArray.get(i).get("y")))
                                .title(stopListArray.get(i).get("stop_name"));

                        mGoogleMap.addMarker(makerOptions);
                    }
                }catch (Exception e){
                    Log.e("[HB]"+this.getClass().getName(),"Error:"+e.toString());
                    msg.what = 0;
                }
            }
        }).start();


    }

错误:不兼容的类型:字符串无法转换为双精度 (.position(new LatLng(stopListArray.get(i).get("x"), stopListArray.get(i).get("y"))))此行)serveridemethod 中的代码太长,无法仅发布所需的部分,而 mapactivity 是整个代码。

此代码将生成此错误 是否有可能得到一些帮助?

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    您的问题在您的 for 循环中,您试图将字符串作为 List 的 get() 函数传递:

     for (int i = 0; i < stop_list.size(); i++) {
                        MarkerOptions makerOptions = new MarkerOptions();
                        makerOptions
                                .position(new LatLng(stopListArray.get("x"), stopListArray.get("y")))
                                .title(stopListArray.get(i).get("stop_name")); 
    
                        mGoogleMap.addMarker(makerOptions);
                    }
    

    应该是:

     for (int i = 0; i < stop_list.size(); i++) {
                        MarkerOptions makerOptions = new MarkerOptions();
                        makerOptions
                                .position(new LatLng(Double.parseDouble(stopListArray.get(i).get("x")), Double.parseDouble(stopListArray.get(i).get("y"))))
                                .title(stopListArray.get(i).get("stop_name")); 
    
                        mGoogleMap.addMarker(makerOptions);
                    }
    

    【讨论】:

    • 我已经尝试了上面的方法,但是我得到了一个错误。错误:不兼容的类型:字符串无法转换为双精度
    • 能否请您在完整的错误日志中发布完整的活动代码
    • 我已经重新修改了上面的文字。
    • 在这里,在上面的答案中修复了它。 LatLng 类构造函数采用两个双参数,而您将其传递给字符串值。需要将您的字符串值转换为双精度
    猜你喜欢
    • 2012-01-29
    • 2020-11-22
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    相关资源
    最近更新 更多