如何在点击侦听器的标记中删除标记?
Posted
技术标签:
【中文标题】如何在点击侦听器的标记中删除标记?【英文标题】:How to remove a marker in marker on click listener? 【发布时间】:2016-01-01 18:57:57 【问题描述】:我的应用程序使用谷歌地图并修复了一组名为mark
和marker
的标记,但是当我点击marker
时,它需要检查mark
是否为空,如果@ 987654325@ 为空,则可以添加 mark
,否则删除以前的 mark
并添加最新的 mark
。
mMap.setOnMarkerClickListener(new OnMarkerClickListener()
@Override
public boolean onMarkerClick(Marker arg0)
// TODO Auto-generated method stub
regi=arg0.getTitle().toString();
Log.e("aaa", ""+regi);
JSONPost jsonpost= new JSONPost();
ja=jsonpost.datewise(regi);
Log.e("Home_details..", "" + ja);
//new DownloadJSON2().execute();
try
arraylist11 = new ArrayList<HashMap<String, String>>();
arraylist12 = new ArrayList<HashMap<String, String>>();
jsonarray = ja.getJSONArray("Routeinbus");
for (int i = 0; i <jsonarray.length(); i++)
HashMap<String, String> map = new HashMap<String, String>();
json = jsonarray.getJSONObject(i);
Log.e("G>>>>>>>>>>>", "" + json);
// Retrive JSON Objects
// map.put("flatID", jsonobject.getString("flatID"));
map.put("FromLat", json.getString("FromLat"));
map.put("FromLong", json.getString("FromLong"));
sourcelat = json.getDouble("FromLat");
sourcelong=json.getDouble("FromLong");
source=new LatLng(sourcelat, sourcelong);
// map.put("FlatImage",
// jsonobject.getString("FlatImage"));
map.put("Fromaddress", json.getString("Fromaddress"));
map.put("ToLat", json.getString("ToLat"));
map.put("ToLong", json.getString("ToLong"));
routeid=json.getString("RouteID");
destname=json.getString("Toaddress");
destlat2=json.getDouble("ToLat");
destlong2=json.getDouble("ToLong");
destination2=new LatLng(destlat2, destlong2);
jsonarray1 = json.getJSONArray("Routes");
Log.d("Hbbbbbbbbbbbbbbb", "" + jsonarray1);
for (int j = 0; j <jsonarray1.length(); j++)
jsonobject1 = jsonarray1.getJSONObject(j);
jsonarray2=jsonobject1.getJSONArray("stages");
Log.d("jsonarray2", "" + jsonarray2);
for(int k=0;k<jsonarray2.length();k++)
jsonobject2 =jsonarray2.getJSONObject(k);
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("Lat",jsonobject2.getString("Lat"));
Log.d("Hbbbbbbbbbbbbbbb", "" + jsonobject2.getString("Lat"));
map1.put("Long",jsonobject2.getString("Long"));
map1.put("StopName", jsonobject2.getString("StopName"));
Log.d("Hbbbbbbbbbbbbbbb", "" + jsonobject2.getString("Long"));
//map1.put("LiveLongitude",jsonobject1.getString("LiveLongitude"));
//Log.d("Hbbbbbbbbbbbbbbb", "" + jsonobject1.getString("LiveLongitude"));
arraylist12.add(map1);
Log.e("arraylist12", ""+arraylist12);
//marker=mMap.addMarker(new MarkerOptions().position(destination2).icon(BitmapDescriptorFactory .fromResource(R.drawable.bustour)));
if(mark!=null)
mark.remove();
for (int m = 0; m < arraylist12.size(); m++)
final LatLng position = new LatLng(Double.parseDouble(arraylist12.get(m).get("Lat")),
Double.parseDouble(arraylist12.get(m).get( "Long")));
Log.e("position", ""+position);
String stopname = arraylist12.get(m).get("StopName");
final MarkerOptions options = new MarkerOptions().position(position);
//mMap.addMarker(options);
mark=mMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory .fromResource(R.drawable.bustour)).title(stopname));
arraylist11.add(map);
Log.e("arraylist2",""+arraylist11);
catch (Exception e)
String result = "Error";
return false;
);
【问题讨论】:
【参考方案1】:我们需要创建一个新的标记对象,然后在添加一个新的标记之前,删除之前的标记
Marker marker;
MAP.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener()
@Override
public void onMapLongClick(LatLng arg0)
if (marker != null)
marker.remove();
marker = MAP.addMarker(new MarkerOptions()
.position(
new LatLng(arg0.latitude,
arg0.longitude))
.draggable(true).visible(true));
);
对 OnMapClick 执行相同操作
MAP.setOnMapClickListener(new OnMapClickListener()
@Override
public void onMapClick(LatLng point)
// TODO Auto-generated method stub
if (marker != null)
marker.remove();
marker = MAP.addMarker(new MarkerOptions()
.position(currentPosition)
.snippet(
"Lat:" + location.getLatitude() + "Lng:"
+ location.getLongitude())
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("ME"));
Log.e("lat", "" + point);
);
【讨论】:
以上是关于如何在点击侦听器的标记中删除标记?的主要内容,如果未能解决你的问题,请参考以下文章