问题
I added a lot of markers in google maps v2 android, I use a hashtable to store the id of each marker, and the results I could make customwindow and so on, but the problem is I want to delete / move the marker based on the id.
This my code to create a marker, I parse data from json
private Hashtable<String, String> markers;
markers = new Hashtable<String, String>();
private void createMarker(String result) throws JSONException{
jObject = new JSONObject(result);
JSONArray menuitemArray = jObject.getJSONArray("data");
for (int i = 0; i < menuitemArray.length(); i++) {
id=menuitemArray.getJSONObject(i).getString("id").trim();
ttl=menuitemArray.getJSONObject(i).getString("user").trim();
snip=menuitemArray.getJSONObject(i).getString("desc").trim();
lat=menuitemArray.getJSONObject(i).getString("latitude").trim();
lng=menuitemArray.getJSONObject(i).getString("longitude").trim();
Marker mk = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble(lat),Double.parseDouble(lng)))
.title(ttl)
.snippet(snipq)
.icon(BitmapDescriptorFactory.fromBitmap(BmFinal)));
markers.put(mk.getId(), id); //here i add info to hastable marker
}
}
I want to move marker in this void
private void moveMarker(String Id){
//what should i do in here ???
}
how to I achieve that? thanks..
回答1:
I can't comment but you might want to try this; Remove the marker first then create another in the new place you want but with the same characteristics.
As example (Just so you get the idea):
public void moveMarker(String Id) {
markers.get(Id).remove(); // this removes the marker
Marker newMk = googleMap.googleMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble(newlat),Double.parseDouble(newlng))) // new coordonates
.title(ttl)
.snippet(snipq)
.icon(BitmapDescriptorFactory.fromBitmap(BmFinal))); // same details
// now add to your map & collection
markers.put(newMk.getId(), Id);
}
来源:https://stackoverflow.com/questions/26694538/how-to-move-single-marker-in-google-maps-v2-android