-1

I am using pusher to receiver location from another device. in my map i wan to display multiple markers dynamically from the pusher data.

I have tried multiple solutions; but none helped to achieve what i needed, tried this post

Here is my code

JSONObject jsonObject1 = new JSONObject(DataFromPusher);
JSONArray jsonArray = jsonObject1.getJSONArray("Userlocation");
    for (int i =0; i < jsonArray.length(); i++)
    {
        jsonObject = jsonArray.getJSONObject(i);
        lat = jsonObject.getString("Userlatitude");
        lng = jsonObject.getString("Userlongitude");
        cor = new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));
        runOnUiThread(new Runnable() {
        @Override
        public void run() {
        if (marker != null){
        marker.remove();
        }
        marker = mMap.addMarker(new MarkerOptions().position(cor).title(lat+ "  " +lng)
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
    }
});
}

Pusher Response :

{
    "Userlocation": [
        {
        "Userlatitude": "12.9325340",
        "Userlongitude": "77.5450430"
        },
        {
        "Userlatitude": "12.9325350",
        "Userlongitude": "77.5450440"
        }
    ]
}
Android
  • 1,420
  • 4
  • 13
  • 23
Beta_Dev
  • 11
  • 4
  • Those two points differ by 0.1553 meters (or 6 inches) so most likely there are 2 markers - one on top of another. But more importantly, your code actually removes the previous marker - so yes you'll only ever see one marker. –  Jun 26 '19 at 11:44
  • @Andy Yes i was thinking the same, it might be overlapping but i am not sure. The reason behind removing the marker is. i want to show only the current location which is received from the pusher data. – Beta_Dev Jun 26 '19 at 12:38

1 Answers1

-1

try this

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject;
                try {
                    jsonObject = jsonArray.getJSONObject(i);

                    String name = jsonObject.getString("name");
                    String lat = jsonObject.getString("lat");
                    String lng = jsonObject.getString("lng");

                    final MarkerOptions markerOptions = new MarkerOptions();
                    markerOptions.position(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));
                    markerOptions.snippet(name);
                    Marker marker = googleMapWhole.addMarker(markerOptions);
                    markerList.add(marker);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Rahul Singh
  • 320
  • 4
  • 7