我正在开发一个应用程序,当应用程序第一次启动时,它会从我的服务器获取所有经纬度位置,并根据标记在 map 上显示它们。之后,我的应用程序开始通过推送机制接收坐标,我为此实现了 Google Cloud Messaging GCM。所以现在我想要我的应用程序通过 GCM 接收的坐标,它首先检查 map 上是否已经存在具有相同纬度和经度的标记,如果是,则从 map 上删除该标记。请指导我如何实现这一目标。
这是我的应用程序第一次启动并通过服务从服务器获取坐标时的代码。
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)));
}
这就是我通过 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"));
最佳答案
您可以简单地构建一个 Set<LatLng>
并将初始标记位置存储在那里。然后,当您通过 GCM 收到新位置时,只需检查您的集合是否已包含 newLatLong
.如果是这样,您可以将其删除,否则为 map 创建标记并添加 newLatLong
到你的集合。
但是,根据您的数据源,您可能会以“几乎”相等但不完全相等的 LatLng 结尾。如果这是一个问题,而不是简单地检查集合是否“包含”新的 LatLng,您可能想要做一些更复杂的检查新的 LatLng 是否“几乎等于”集合中的任何 LatLng。
关于android - 如何检查 Google map v2 中已存在具有相同地理坐标的标记?安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27498513/