简体   繁体   English

如何知道在 Google Maps v2 for Android 上点击了哪个标记?

[英]How to know which Marker was clicked on Google Maps v2 for Android?

I am making an application with a Google Maps on it, for Android.我正在为 Android 制作一个带有谷歌地图的应用程序。 I have plenty of Markers on my screen and I am preparing customizable balloon for each marker when they are clicked.我的屏幕上有很多标记,我正在为每个标记准备可自定义的气球,当它们被点击时。 This means, I have information which is different according to the marker which was clicked.这意味着,根据点击的标记,我有不同的信息。

I set up the contents of the View of the marker with setInfoWindowAdapter and then I override the method getInfoContents.我使用 setInfoWindowAdapter 设置标记视图的内容,然后覆盖方法 getInfoContents。

The problem is: This method is the general implementation of the contents of the Info Window, but each marker shall show its own information.问题是:这个方法是Info Window内容的一般实现,但是每个marker都要显示自己的信息。 So, as far as I understand, I have to somehow detect on getInfoContents(Marker marker) which of the markers have been clicked, in order to load from my data structures the necessary info to present on the info window.因此,据我所知,我必须以某种方式在 getInfoContents(Marker marker) 上检测哪些标记已被单击,以便从我的数据结构加载必要的信息以显示在信息窗口上。 The question is: How do I identify what entity the clicked Marker 'marker' represents?问题是:我如何识别点击的标记“标记”代表什么实体? I mean, having just the object Marker on getInfoContents which was triggered to show the info window, how can I detect which is the proper information to display?我的意思是,只有 getInfoContents 上的对象 Marker 被触发以显示信息窗口,我如何检测要显示的正确信息? I though about comparing the string Title by using marker.getTitle(), but this obliges me to display a Title on the info window, which I do not want.我想通过使用marker.getTitle() 来比较字符串Title,但这迫使我在信息窗口上显示一个标题,这是我不想要的。 There's also a marker.getId(), but such ID is generated by the API and I can't control it还有一个marker.getId(),但是这个ID是API生成的,我控制不了

Any ideas?有任何想法吗?

In reply to:回复:

The question is: How do I identify what entity the clicked Marker 'marker' represents?问题是:我如何识别点击的标记“标记”代表什么实体? [...] There's also a marker.getId(), but such ID is generated by the API and I can't control it [...] 还有一个marker.getId(),但是这个ID是API生成的,我控制不了

You can control it.你可以控制它。 The marker is returned by addMarker(), so you can get its ID and store it.标记由 addMarker() 返回,因此您可以获取其 ID 并存储它。 Here's the code:这是代码:

Map <String, Integer> markers = new HashMap<String, Integer>();

... ...

MarkerOptions mo = new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker))
.position(new LatLng(mLat, mLon))
.flat(true) 
.snippet("Click here for details")
.rotation(0)
.title(title);

When you add the marker to the map, store its ID on the container将标记添加到地图时,将其 ID 存储在容器中

MyClass myObject = new MyClass(lat, lon); // The class that you are rendering on the map with Markers, for example "Monument"
Marker mkr = map.addMarker(mo);
markers.put(mkr.getId(), myObject.getId()); 

Then when the marker is clicked, you can recover the id of "myObject" like this然后当点击标记时,您可以像这样恢复“myObject”的id

map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
    public void onInfoWindowClick(Marker marker) {
        int id = markers.get(marker.getId());
        // Now id contains which Monument (or your preferred class) was clicked                         
    }
});

There is a better option, and this is what google suggests:有一个更好的选择,这就是谷歌建议的:

Tag : An Object associated with the marker.标签:与标记关联的Object For example, the Object can contain data about what the marker represents.例如, Object可以包含有关标记代表什么的数据。 This is easier than storing a separate Map<Marker, Object> .这比存储单独的Map<Marker, Object>更容易。 As another example, you can associate a String ID corresponding to the ID from a data set.再举一个例子,您可以将一个String ID 与数据集中的 ID 相关联。 Google Maps Android API neither reads nor writes this property. Google Maps Android API 既不读取也不写入此属性。

Source 来源

So you can do something like this:所以你可以做这样的事情:

for (ListItem it: mList) {
    Marker mk = mMap.addMarker(new MarkerOptions().position(it.getLatLng()).title(it.getName()).snippet(it.getAddress()));
    mk.setTag(it.getID());
}

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        Integer id = (Integer) marker.getTag();
        return false;
    }
});

You are not obligated to show title when you have it set, so you can use that and as long as you return a View from getInfoContents and not setText on any subview of that returned View with title value.您没有义务显示标题,当你有它设置,使您可以根据返回使用,并且只要ViewgetInfoContents ,而不是setText对返回的任何子视图View与标题值。

Depending on how and if you already keep references to all markers, there are alternatives, eg if you had List<Marker> policeMarkers and List<Marker> badGuysMarkers you can use a conditional if (policeMarkers.contains(marker)) { ... } else { ... } .根据您是否以及是否已经保留对所有标记的引用,还有其他选择,例如,如果您有List<Marker> policeMarkersList<Marker> badGuysMarkers您可以使用条件if (policeMarkers.contains(marker)) { ... } else { ... }

You can also keep a Map<Marker, YourMarkerRelatedDataModel> allMarkers and do YourMarkerRelatedDataModel model = allMarkers.get(marker);您还可以保留Map<Marker, YourMarkerRelatedDataModel> allMarkers并执行YourMarkerRelatedDataModel model = allMarkers.get(marker); and use that value to differentiate.并使用该值进行区分。

Finally you can use Android Maps Extensions , which adds functions like Marker.setData(Object) and Object Marker.getData() to keep your model close to your markers and not create things like Map<Marker, Model> .最后,您可以使用Android Maps Extensions ,它添加了诸如Marker.setData(Object)Object Marker.getData()类的函数,以使您的模型靠近标记,而不是创建诸如Map<Marker, Model>

try this code:试试这个代码:

@Override
    public void onInfoWindowClick(final Marker marker) 
    {
        // TODO Auto-generated method stub
       if (marker.getTitle().equalsIgnoreCase("Start")) {
        Toast.makeText(showMaps.this, "You have click on start -->",
                Toast.LENGTH_LONG).show();
        Log.e("marker.getPosition()-->", "" + marker.getPosition());
      }
    }
  map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                public void onInfoWindowClick(Marker marker) {

                    double lat=marker.getPosition().latitude;
                    double lng=marker.getPosition().longitude;


                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM