如何将谷歌地图Android API v2标记链接到一个对象上[英] How to link Google Maps Android API v2 Marker to an object

本文是小编为大家收集整理的关于如何将谷歌地图Android API v2标记链接到一个对象上的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在地图中动态添加了一个未固定数量的标记,它们每个标记都与我的Poco类的一个实例有关.

我需要链接它们,因此当用户单击其中一个标记时,我在自定义InfowDINDOW中显示其余数据.

您建议什么?

ps:我每次用户盘或放大地图时都会添加新标记,我担心超载该应用程序.非可见标记是否处置了?

推荐答案

我建议使用hashmap或类似的东西.当您迭代对象列表并为其创建标记时,还将标记添加到列表中,使用对象的ID作为键,将标记作为值:

private HashMap<Integer, Marker> markerMap = new HashMap<Integer, Marker>();

...

for(MarkerObject obj : this.markerObjects)
{
     //If the marker isn't already being displayed
     if(!markerMap.containsKey(obj.getId()))
     {
         //Add the Marker to the Map and keep track of it 
         this.markerMap.put(obj.getId(), this.mMap.addMarker(getMarkerForObject(obj)));
     }
}

然后,您可以使用OnInFowWindowClickListener在地图中找到点击标记的对象ID,并使用相应的数据进行操作,例如打开带有详细信息的新活动.

其他推荐答案

我知道这篇文章是旧的,但是如果您使用的是Android Studio中的预制映射活动

在设置地图中

  private void setUpMap() {


    Map<String,someObject>markerInfoList = new HashMap<String,someObject>();

  // get the marker Id as String
       String id =  mMap.addMarker(new MarkerOptions().position(new LatLng(/*set Latitude*/,  /*setLongitude*/).title("Marker")).getId();
       //add the marker ID to Map this way you are not holding on to  GoogleMap object
        markerInfoList.put(id,mapppedHouses.get(i));     
}

然后在:

  private void setUpMapIfNeeded() {
  ///...
 if (mMap != null) {
   //if a marker is clicked
   mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
                    someObject = markerInfoList.get(marker.getId());
                }
            });
  }
 }

本文地址:https://www.itbaoku.cn/post/102600.html

问题描述

I'm adding dynamically a non-fixed amount of markers in a map, which each of them are related to one instance of my POCO class.

I need to link them so when the user clicks on one of the markers, I show the rest of the data inside the custom InfoWindow.

What do you suggest?

PS: I add new markers everytime the user pans or zooms the map and I worried about overloading the app. Are the non visible markers disposed?

推荐答案

I suggest using a HashMap or something similar. As you iterate over your list of objects and create markers for them, also add the Marker to a list, using the ID of the object as the key, and the marker as the value:

private HashMap<Integer, Marker> markerMap = new HashMap<Integer, Marker>();

...

for(MarkerObject obj : this.markerObjects)
{
     //If the marker isn't already being displayed
     if(!markerMap.containsKey(obj.getId()))
     {
         //Add the Marker to the Map and keep track of it 
         this.markerMap.put(obj.getId(), this.mMap.addMarker(getMarkerForObject(obj)));
     }
}

Then you can use a OnInfoWindowClickListener to find the object id of the tapped marker in your Map and do something with the corresponding data, like open a new activity with details.

其他推荐答案

I know this post is old, but if you are using the prefab map Activity in Android studio

In the set up map

  private void setUpMap() {


    Map<String,someObject>markerInfoList = new HashMap<String,someObject>();

  // get the marker Id as String
       String id =  mMap.addMarker(new MarkerOptions().position(new LatLng(/*set Latitude*/,  /*setLongitude*/).title("Marker")).getId();
       //add the marker ID to Map this way you are not holding on to  GoogleMap object
        markerInfoList.put(id,mapppedHouses.get(i));     
}

Then in the :

  private void setUpMapIfNeeded() {
  ///...
 if (mMap != null) {
   //if a marker is clicked
   mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
                    someObject = markerInfoList.get(marker.getId());
                }
            });
  }
 }