【问题标题】:How can I associate an object with a Google Maps marker?如何将对象与 Google 地图标记相关联?
【发布时间】:2013-03-13 15:37:20
【问题描述】:

我正在构建一个 Android GPS 应用程序,该应用程序根据用户当前位置显示最近的地点。我正在使用 MapFragment,并且在 AsyncTask 中像这样在地图上放置标记:

protected void onPostExecute(Void result) { 
    super.onPostExecute(result);

    for(int i = 0; i < list.size(); i++)
    {
        HashMap<String,String> location = new HashMap<String,String>();
        location = list.get(i);

        String type = location.get("type");
        int marker = getTypeDrawable(type);

        LatLng position = new LatLng(Float.parseFloat(location.get("lat")), Float.parseFloat(location.get("long")));
         map.addMarker(new MarkerOptions()
        .position(position)
        .title(location.get("name"))
        .snippet(location.get("distance") + location.get("type"))                                  
        .icon(BitmapDescriptorFactory.fromResource(marker)));
        System.out.println(list.get(i));
    }

    Toast.makeText(MainActivity.this, "Finished retrieving nearby locations",
            Toast.LENGTH_SHORT).show();         
}

这是我的全局变量列表,其中包含最近位置的所有信息:ArrayList&lt;HashMap&lt;String, String&gt;&gt; list;

我的计划是在我单击标记时显示一个弹出对话框,就像地理缓存应用程序“C:Geo”一样。我的问题是,我不知道如何在对话框中显示该标记的信息。如您所见,我正在使用map.addMarker() 方法,但我无法将该位置与该标记相关联。

我希望制作一个这样的弹出对话框:

@Override
public boolean onMarkerClick(Marker marker) {
 //Get location associated with marker. 
//Fill popup dialog with information associated 
//with location and then invoke the dialog.
}

【问题讨论】:

    标签: android object location google-maps-markers google-maps-android-api-2


    【解决方案1】:

    您需要选择能够唯一标识标记和位置的内容。也许这是您在标记上设置的标题(您的位置中会有一些相应的字符串)或 Lat/Lng 组合。当用户点击标记时,您将在 onMarkerClick(Marker marker) 方法中收到该标记,您从标记中读取标题或纬度/经度并从您的位置查找相应的位置。例如,您可以遍历您的位置 HashMap 并检查该位置是否与您的标记具有相同的 lat/lng,然后您就有了与您的标记相对应的位置。

    当然有更有效的方法,但你明白了。

    【讨论】:

    • 是的,我想过,但它似乎非常有效
    • 不必如此,为您保存两个字符串的位置创建一个小的 POJO,并在上面的类中创建一个 HashMap 然后您可以直接查找该位置使用 LatLng 键。
    • 嗯,这两个字符串应该是什么?我试试看
    • 我不知道你的位置到底长什么样,我想你有一些名字、一些文字、一个 ID、Lat 和 Lon 等等。我的意思是要有一个 HashMap,其中包含 1. 一个识别键和 2. 您的位置对象,这样您就可以在点击标记时轻松查找该对象。
    • OK,所以如果先做 marker.getId() 然后再做 hashmap.put(markerID, locationObject); ?
    【解决方案2】:

    鉴于您所需方法的格式,我认为您可以使用标记的哈希码作为访问位置的键。我自己没有尝试过,但我的以下想法应该有效(并且更有效率)。 首先,您必须将添加标记的方式更改为:

    Marker locationMarker = map.addMarker(new MarkerOptions()
        .position(position)
        .title(location.get("name"))
        .snippet(location.get("distance") + location.get("type"))                                  
        .icon(BitmapDescriptorFactory.fromResource(marker)));
    

    这确保您可以访问添加的标记对象。然后,将HashMapInteger 作为键,将Location 作为结果对象。使用标记的哈希码的键存储位置,您可以通过以下方式访问:marker.hashCode();

    我通常使用getTag()/setTag()方法来保存对象引用,但不幸的是Marker类没有标签:(

    对这种方式的一个可能的警告是,hashCode 可能会因 2 个标记而发生冲突。我不知道这种可能性有多大,这需要调查/测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多