使用'为#39;添加多个标记时,Android Google Maps v2:出错环

时间:2014-03-24 15:51:07

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

我需要使用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中的运行时错误吗?我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

    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);

答案 1 :(得分:0)

我终于通过在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]);
        }
    }
}
....