【发布时间】: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