【问题标题】:How to check marker with same geo coordinates already exists in Google maps v2? Android如何检查具有相同地理坐标的标记已存在于谷歌地图 v2 中?安卓
【发布时间】:2015-02-14 09:40:24
【问题描述】:

我正在开发一个应用程序,当应用程序第一次启动时,它会从我的服务器获取所有纬度和经度位置,并根据标记在地图上显示它们。之后,我的应用程序开始通过推送机制接收坐标,我为此实现了 Google Cloud Messaging GCM。所以现在我想要我的应用程序通过 GCM 接收到的坐标,它首先检查地图上是否已经存在具有相同纬度和经度的标记,如果是,然后从地图中删除该标记。请指导我如何实现这一目标。

这是我的应用程序第一次启动并通过服务从服务器获取坐标时的代码。

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"));

【问题讨论】:

    标签: android google-maps google-maps-markers google-maps-android-api-2 google-cloud-messaging


    【解决方案1】:

    您可以简单地构建一个Set&lt;LatLng&gt; 并将初始标记位置存储在那里。然后,当您通过 GCM 收到新位置时,只需检查您的集合是否已包含 newLatLong。如果是这样,您可以将其删除,否则为地图创建标记并将newLatLong 添加到您的集合中。

    但是,根据您的数据源,您可能会以“几乎”相等但不完全相等的 LatLng 结尾。如果这是一个问题,而不是简单地检查集合是否“包含”新的 LatLng,您可能想要做一些更复杂的检查新的 LatLng 是否“几乎等于”集合中的任何 LatLng。

    【讨论】:

      【解决方案2】:

      尝试像这样添加标记,Marker m1;

      m1 = map.addMarker(new MarkerOptions().position(newLatLong)
                                .title("GCM"));
      

      然后检查它是否可见标记,

      if(m1.isvisible())
      {
        //do something
      }
      

      通过标记获取位置,像这样使用

      LatLng position = marker.getPosition();
      

      //比较纬度和经度值并在检查位置值之前添加标记

      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
      
      }
      

      【讨论】:

      • 我想检查/比较从 GCM 收到的坐标与地图上已经存在的标记的位置。我不认为 Isvisible() 可以完成我的要求。
      • 我认为..如果您将所有标记设置为变量..那么您可以获得标记的“检查/比较”状态。
      • 您只需在 arraylist 中添加 latlng 值,检查/比较位置,如果可用,删除标记或不添加标记
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 2014-09-15
      相关资源
      最近更新 更多