0

我正在构建一个 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<HashMap<String, String>> 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.
}
4

2 回答 2

1

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

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

于 2013-03-13T15:50:27.623 回答
0

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

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

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

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

这种方式的一个可能的警告是,hashCode 可能会碰撞 2 个标记。我不知道这有多大可能,这将是需要研究/测试的东西。

于 2013-12-12T17:45:10.510 回答