【问题标题】:Android GoogleMaps v2: error when adding multiple markers using 'for' loopAndroid GoogleMaps v2:使用“for”循环添加多个标记时出错
【发布时间】:2014-05-02 02:15:55
【问题描述】:

我需要使用for 循环向地图添加给定数量的标记。 Log 消息告诉我调用添加每个标记的函数,但地图上只显示一个标记(最多两个)。我的两个功能如下:

private void paintInMap(String description){
    map.clear(); // to erase previous markers
    String[] zones = getResources().getStringArray(R.array.zonas); // array of place names
    String[] coord = getResources().getStringArray(R.array.coordinates); // array of place coordinates (placed in the same order)
    String[] route = description.split(", "); // split the different places of the route description

    for(int i=0; i<route.length; i++){
        for(int j=0; j<zones.length; j++{
            if(route[i].equals(zones[j])){
                LatLng latLng = getCoordinates(coord[j]); // call function to get coordinates from String
                placeMarker(latLng, zones[j]);
            }
        }
    }
}

和:

private void placeMarker(LatLng coordinates, String name){
    map.addMarker(new MarkerOptions()
        .title(name)
        .icon(BitMapDescriptorFactory.fromResource(R.drawable.gpsmap))
        .position(coordinates)
        .flat(true)
        .rotation(90));
    Log.d("PLACE", name+" added to map");
}

显然我的代码是正确的,但在运行时它只显示一个(或两个)标记。我检查了Log 消息并且正在调用该函数,但没有出现标记。此外,其中一个标记出现在未指定的位置(顺便说一下对应于坐标数组的第一个值)

这是 Eclipse 中的运行时错误吗?我该如何解决这个问题?

【问题讨论】:

  • 我不确定它是否与您的问题有关,但您正在 ui 线程中创建一个 for/loop。使用AsyncTask并在发布进度中传递MarkerOption(添加需要在ui线程中完成)

标签: android google-maps-markers android-maps-v2


【解决方案1】:
    map.clear(); // to erase previous markers
    new AsyncTask<String, MarkerOptions, Void>() {

        private void placeMarker(LatLng coordinates, String name) {
            publishProgress(new MarkerOptions()
                .title(name)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.gpsmap))
                .position(coordinates)
                .flat(true)
                .rotation(90));
            Log.d("PLACE", name + " added to map");
        }

        @Override
        protected Void doInBackground(String... params) {

            String[] zones = getResources().getStringArray(R.array.zonas); // array of place names
            String[] coord = getResources().getStringArray(R.array.coordinates); // array of place coordinates (placed in the same order)
            String[] route = params[0].split(", "); // split the different places of the route description

            for (int i=0; i<route.length; i++) {
                for (int j=0; j<zones.length; j++) {
                    if (route[i].equals(zones[j])) {
                        LatLng latLng = getCoordinates(coord[j]); // call function to get coordinates from String
                        placeMarker(latLng, zones[j]);
                    }
                }
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(MarkerOptions... markers) {
            map.addMarker(markers[0]);
        }
    }.execute(description);

【讨论】:

  • 使用这个我得到一个致命的例外:你不能在 UI 线程之外添加标记。添加任何标记/多边形/折线/圆不能在 UI 线程之外完成
【解决方案2】:

我终于通过在 UI 线程中运行 for 循环而不是使用 AsyncTask 来解决它

......
route = descriptionRoute.split(", ");
coordinates = getCoordinates(coord);
String[] zonas = getResources().getStringArray(R.array.array_zonas_madrid);
String[] coord = getResources().getStringArray(R.array.array_coordinates);
for(int i=0; i<route.length; i++){
    for(int j=0; j<zonas.length; j++){
        if(route[i].equals(zonas[j])){
            LatLng latLng = getCoordinates(coord[j]);
            placeMarker(latLng, zonas[j]);
        }
    }
}
....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 2014-06-12
    • 2015-08-14
    • 2012-12-31
    • 2013-03-10
    • 2017-07-11
    相关资源
    最近更新 更多