简体   繁体   English

Android Google Maps添加了多个标记,无法绑定onInfoWindowClick方法

[英]Android Google maps adding multiple markers, cannot bind onInfoWindowClick method

I am currently trying to add multiple markers to my Android application. 我目前正在尝试向我的Android应用程序添加多个标记。 This works just perfectly. 这非常完美。 The only thing I am getting stuck at is the fact that I cannot bind multiple "onInfoWindowClick" on multiple markers. 我唯一遇到的困扰是我无法在多个标记上绑定多个“ onInfoWindowClick”。

For instance, if I have like: 例如,如果我有:

                for (int i = 0; i < randomList; i++) {

                        MarkerOptions marker = new MarkerOptions().position(latlng).title(MainActivity.list.get(i).aMessage);

                            // adding marker
                            googleMap.addMarker(marker);



                        googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                            @Override
                            public void onInfoWindowClick(Marker marker) {
                                // Do something onclick
                            }
                        });

                }

This would result in a infowindowclick that works, for each marker, but I always get the same data back inside that "// Do something onclick", this is because the last marker is getting set to this event. 这将导致对每个标记都起作用的infowindowclick,但是我总是在“ // Do on onclick”中获得相同的数据,这是因为最后一个标记已设置为该事件。

What is my procedure to attach this event to multiple markers? 如何将此事件附加到多个标记的程序是什么?

Create a new Marker on every loop and execute the method: showInfoWindow() ; 在每个循环上创建一个新的Marker并执行以下方法: showInfoWindow() ;

 for (int i = 0; i < randomList; i++) {

                      //  MarkerOptions marker = new MarkerOptions().position(latlng).title(MainActivity.list.get(i).aMessage);   
                     // adding marker
                     //    googleMap.addMarker(marker);    
googleMap.addMarker(new MarkerOptions().position(latlng).title(MainActivity.list.get(i).aMessage)).showInfoWindow();



                        googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                            @Override
                            public void onInfoWindowClick(Marker marker) {
                                // Do something onclick
                            }
                        });

                }

by the way you are setting the same latlng value! 顺便说一下,您要设置相同的latlng值!

You should not be calling setOnInfoWindowClickListener() inside a loop, this just needs to be called once, and it will apply to all Markers. 您不应该在循环内调用setOnInfoWindowClickListener() ,只需调用一次即可,它将应用于所有Marker。

The Marker object that gets passed into onInfoWindowClick(Marker marker) will always be the Marker that was just clicked. 传递给onInfoWindowClick(Marker marker)的Marker对象将始终是刚刚单击的Marker。

So, take that out of the loop. 因此,将其带出循环。 Next, in order to figure out what Marker was just clicked, you could get the title of the Marker, and loop through your list until you find the list item that has a aMessage value that corresponds to the Marker's title. 接下来,为了找出刚刚单击的标记,您可以获取标记的标题,并循环浏览列表,直到找到具有与标记标题相对应的aMessage值的列表项。

Note you could also identify that current marker by location by calling marker.getPosition(); 注意,您还可以通过调用marker.getPosition();按位置识别当前标记marker.getPosition(); .

       googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                //get location of Marker that was just clicked
                LatLng latLon = marker.getPosition();

                // get the title of Marker that was just clicked
                String title = marker.getTitle();

                //find item in the list that corresponds to currently clicked Marker
                for (int i = 0; i < MainActivity.this.list.size(); i++){
                    if (title.equals(MainActivity.this.list.get(i).aMessage)){
                        //found current list item corresponding to
                        //the Marker that was just clicked!
                    }
                }
            }
        });

        for (int i = 0; i < randomList; i++) {

            MarkerOptions marker = new MarkerOptions().position(latlng).title(MainActivity.this.list.get(i).aMessage);
            // adding marker
            googleMap.addMarker(marker);

        }

I answered a similar question here , you may find that helpful as well. 在这里回答了类似的问题,您可能也会发现它也很有帮助。

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

相关问题 Android-Google地图中的多个标记 - Android - Multiple markers in google maps 使用ArrayList在Google Maps上添加多个标记 - Adding multiple markers on Google Maps with ArrayList Android Google Maps OnInfoWindowClick保留单击标记中的图标 - Android Google Maps OnInfoWindowClick keep icon from clicked marker 使用 Java 在 Android Studio 中的 Google 地图上从 Firestore 数据库添加多个标记 - Adding Multiple Markers from Firestore Database on Google Maps in Android Studio using Java 添加多个标记并在点击时启动新活动-GOOGLE MAPS-Android - Adding multiple markers and launching into new activity on click -GOOGLE MAPS-Android 从 android Java 中的凌空休息向谷歌地图 API 添加多个标记 - Adding multiple markers to google maps API from volley repose in android Java 使用数组列表将标记添加到Google地图。 Android的 - Adding markers to the Google maps using array list. Android Android Google在添加彩色标记时会映射巨大的滞后 - Android google maps huge lag when adding colored markers 如果有多个标记,则oninfowindowclick仅与标记信息一起使用 - oninfowindowclick used only with only marker info in case of multiple markers Google地图显示Firebase中的多个标记 - Google maps show multiple markers from firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM