Vaibhav Agarwal Vaibhav Agarwal - 3 months ago 41
JSON Question

Google Maps android: Live update position of multiple markers

I am trying to create a map showing position of specified persons on

Google Map
. I have set up a web server with MySQL database. Whenever the android app runs, it stores the current location of the user in the database. After that, whenever the
is called
, the app fetches the location data of (say, 10 persons) in proximity range of (say, 50Km) to the user location. Using these location coordinates, I have added multiple markers on the map. Now I am facing following issues:

  1. When the location data of 10 persons changes, new markers are being created instead of original marker changing its position.

  2. When the location data of any of the 10 persons is deleted from the database, the corresponding marker remains on the map.

Note: I am using JSON to fetch data from server and parsed the JSON data in an
to create markers.

Here is how I am parsing JSON:

protected void onHandleIntent(Intent intent) {

if (intent != null) {

String json_result = intent.getExtras().getString("JSON Data");
try {
JSONObject jsonObject = new JSONObject(json_result);
JSONArray jsonArray = jsonObject.getJSONArray("location data");

int counter = 0;
String name, lat, lng;
double dist;
int arrayLength;

while (counter < jsonArray.length()) {

JSONObject object = jsonArray.getJSONObject(counter);
name = object.getString("name");
lat = object.getString("lat");
lng = object.getString("lng");
dist = object.getDouble("distance");
arrayLength = jsonArray.length();

Intent jsonDataIntent = new Intent();
jsonDataIntent.putExtra(Constants.STRING_NAME, name);
jsonDataIntent.putExtra(Constants.STRING_LAT, lat);
jsonDataIntent.putExtra(Constants.STRING_LNG, lng);
jsonDataIntent.putExtra(Constants.STRING_DIST, dist);
jsonDataIntent.putExtra(Constants.STRING_LENGTH, arrayLength);
jsonDataIntent.putExtra(Constants.STRING_ID, counter);


} catch (JSONException e) {


And here is how I have used parsed JSON data to create markers:

private class ParseJSONDataReceiver extends BroadcastReceiver {

Marker usersMarker;
double lat, lng, dist;
int numberOfConnections, id;
HashMap<Marker,Integer> hashMap = new HashMap<>();

public void onReceive(Context context, Intent intent) {

if (intent != null) {

String name = intent.getExtras().getString(ParseJSONDataService.Constants.STRING_NAME);
lat = Double.parseDouble(intent.getExtras().getString(ParseJSONDataService.Constants.STRING_LAT));
lng = Double.parseDouble(intent.getExtras().getString(ParseJSONDataService.Constants.STRING_LNG));
dist = intent.getExtras().getDouble(ParseJSONDataService.Constants.STRING_DIST);
numberOfConnections = intent.getExtras().getInt(ParseJSONDataService.Constants.STRING_LENGTH);
id = intent.getExtras().getInt(ParseJSONDataService.Constants.STRING_ID) + 1;

usersMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Name: " + name)
.snippet(id + ": Distance: " + dist + " Km")




Can anyone help me resolve above issues????


To update the position of your existing markers you need to change your code tu use a HashMap<Integer, Marker> instead of a HashMap<Marker, Integer> so you can query your map by the user id:

HashMap<Integer, Marker> hashMap = new HashMap<>();

Then, you add our markers like this (updating the existing ones):

if (hashMap.containsKey(id)) {
    Marker marker = hashMap.get(id);
    marker.setPosition(new LatLng(lat, lng)); // Update your marker
} else {
    usersMarker = mMap.addMarker(new MarkerOptions()
            .position(new LatLng(lat, lng))
            .title("Name: " + name)
            .snippet(id + ": Distance: " + dist + " Km")

    hashMap.put(id, usersMarker);

If you want to delete a user you need to also delete its marker from the map and from the HashMap:

Marker marker = hashMap.get(id);
if (marker!= null) {