更新:我通过添加prevIoUsMarker对象解决了性能问题.因此,只有先前点击的标记将被删除并替换为默认图标.但是,当我单击标记时,信息窗口仍未显示.
我有一个地图视图并在其上设置了一些标记.我想要的是当我点击一个标记时,它将其图标更改为一个不同的图标,当我点击另一个标记时,前一个标记的图标应该更改为其原始图标.
@Override public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped. LatLng markerPos=marker.getPosition(); String markerLocationName=marker.getTitle(); String markerSubCategoryName=marker.getSnippet(); marker.remove(); MarkerOptions markerOptions = new MarkerOptions().position(markerPos) .title(markerLocationName) .snippet(markerSubCategoryName) .icon(BitmapDescriptorFactory.fromResource(R.drawable.new_icon));// Changing marker icon mMap.addMarker(markerOptions); Log.d("marker","change marker icon"); // can open a dialog window here return false; }
因此,如果我单击2个标记,我将显示2个新图标,同时我想要的只是当前单击的标记更改其图标.
所以我也做了类似的事情,增加了2行代码.它成功地做了我想要的但它有一些缺点(见下文).
@Override public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped. mMap.clear(); populateAllMarkersOnMap();//repopulate markers on map LatLng markerPos=marker.getPosition(); String markerLocationName=marker.getTitle(); String markerSubCategoryName=marker.getSnippet(); marker.remove(); //remove the current clicked marker MarkerOptions markerOptions = new MarkerOptions().position(markerPos) .title(markerLocationName) .snippet(markerSubCategoryName) .icon(BitmapDescriptorFactory.fromResource(R.drawable.new_icon));// Changing marker icon mMap.addMarker(markerOptions); //add marker with new icon into map return false; }
缺点是1 /它“禁用”信息窗口(同样的事情也发生在第一种方式). 2 /它清除地图上的所有标记并再次设置所有标记.想象一下,我有100个标记,如果每次点击都会出现性能问题吗?
populateAllMarkersOnMap()目前可以像这样简单:
private void populateAllMarkersOnMap(){ setMarker(latA1,lonA1,"A1","A1.1"); setMarker(latA2,lonA2,"A2","A2.1"); // ... (100 times or populated via a loop) };
那么当我点击新标记时,有没有办法让以前点击的标记将其图标更改回默认值?为我的英语道歉,如果你认为我应该为我的问题添加另一个标题,请帮忙.
解决方法
最后,我找到了最好,最简单的方法.我创建了一个prevIoUsMarker对象并存储了当前单击的标记:
@Override public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped. if(prevIoUsMarker!=null){ prevIoUsMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.dot_icon)); } marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ct_icon)); prevIoUsMarker=marker; //Now the clicked marker becomes prevIoUsMarker return false; }