我想在地图标记被选中时更改地图标记的图标。
我在Fragment类中实现了GoogleMap.OnMarkerClickListener类。
public class MapFragment extends SupportMapFragment implements GoogleMap.OnMarkerClickListener{
....
@Override
public boolean onMarkerClick(Marker marker) {
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.placemarker_pressed));
return false;
}
}
单击时更改标记图标。如果未选中,如何将其重新设置为原始图标?
答案 0 :(得分:9)
还实施 GoogleMap.OnMapClickListener 界面并创建一个私有变量来存储当前选定的标记:
private Marker mSelectedMarker;
然后:
@Override
public boolean onMarkerClick(Marker marker) {
if (null != mSelectedMarker) {
mSelectedMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.placemarker));
}
mSelectedMarker = marker;
mSelectedMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.placemarker_pressed));
return false;
}
@Override
public boolean onMapClick(LatLng latLng) {
if(null != mSelectedMarker) {
mSelectedMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.placemarker));
}
mSelectedMarker = null;
}