Reputation: 674
I am developing an app in which first time when app starts it fetch all the locations in terms of latitude and longitude from my server and show them on the map in terms of markers. After that my app starts receiving coordinates through pushing mechanism for which i implement Google Cloud Messaging GCM. So now what i want the coordinate which my app receive through GCM it first check whether the marker with same latitude and longitude already present on the map or not if yes then remove that marker from the map. Please guide me how can i achieve this.
Here is my code when first time my app start and fetch coordinates from the server through service.
user = json.getJSONArray(TAG_GPS);
String Latitude, Longitude, Type;
LatLng latLngGps;
int a=user.length();
for(int i=0;i<a;i++){
JSONObject c=user.getJSONObject(i);
Latitude=c.getString(TAG_LAT);
Longitude=c.getString(TAG_LONG);
ID=c.getString(TAG_ID);
mGoogleMap .addMarker(new MarkerOptions().position(latLngGps).title("A").icon(BitmapDescriptorFactory.fromResource(R.drawable.red)));
}
And this is how i am getting coordinate through GCM
json = new JSONObject(message);
String lat = json.getString("lati");
String lon = json.getString("longi");
Double l1,l2;
l1 = Double.parseDouble(lat);
l2 = Double.parseDouble(lon);
LatLng newLatLong = new LatLng(l1, l2);
//Here i want to check where marker with same position already present on map or not if yes delete that maker otherwise plot a marker on map
LatLng newLatLong = new LatLng(l1, l2);
map.addMarker(new MarkerOptions().position(newLatLong)
.title("GCM"));
Upvotes: 1
Views: 3070
Reputation: 1413
try to add the marker like this,Marker m1;
m1 = map.addMarker(new MarkerOptions().position(newLatLong)
.title("GCM"));
then check it's visible or not marker,
if(m1.isvisible())
{
//do something
}
get position by marker , use like this
LatLng position = marker.getPosition();
//compare the latitude and longitude value and add the marker before you check the position values
for(int i=0;i<arr_lat.size();i++)
{
if(arr_lat.get(i) == lat_value)
if(arr_lng.get(i) == lng_value)
//same values don't add the marker and remove marker.
else
//add the lat_value and lng_value positon in arraylist ,add the marker in map
}
Upvotes: 0
Reputation: 23655
You can simply build a Set<LatLng>
and store the initial marker locations there. Then, when you receive new locations via GCM, just check if your set already contains newLatLong
. If so, you can delete it, otherwise create the marker for the map and also add the newLatLong
to your set.
However, and depending on your data sources, you probably might end with LatLngs that are "almost" equal but not quite. If that's an issue, instead of simply checking if set "contains" the new LatLng, you may want to do some more sophisticated checks whether the new LatLng is "almost equal" to any of the LatLngs in the Set.
Upvotes: 1