我正在玩谷歌地图(android studio),并试图限制单个用户可以固定的标记数量。我在网上找不到任何例子,我唯一能找到的就是删除当前标记(下面的代码)。
我的目标是允许用户仅固定3个标记。
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng arg0) {
//if there is a marker already this if condition removes it
if (marker != null) {
marker.remove();
}
marker = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_02))
.position(
new LatLng(arg0.latitude,
arg0.longitude))
.draggable(true).visible(true));
}
});
答案 0 :(得分:3)
int marker_count=0;
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng arg0) {
if(marker_count<3){
//if there is a marker already this if condition removes it
if (marker != null) {
marker.remove();
marker_count=marker_count-1;
}
marker_count=marker_count+1;
marker = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_02))
.position(
new LatLng(arg0.latitude,
arg0.longitude))
.draggable(true).visible(true));
}}
else{
//toast a message
}
});
答案 1 :(得分:0)
稍微编辑了Shvet的code。在固定3个标记后,用户无法固定第四个,并且他会收到一条快速消息。
int markerCount = 0; //marker counter
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng arg0) {
if (markerCount < 3) {
markerCount = markerCount+ 1;
marker = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
.position(new LatLng(arg0.latitude, arg0.longitude))
.draggable(true)
.visible(true));
} else {
Toast.makeText(getApplicationContext(), "Only 3 markers allowed!",
Toast.LENGTH_LONG).show();
}
}
}