简体   繁体   English

如果有多个标记,则oninfowindowclick仅与标记信息一起使用

[英]oninfowindowclick used only with only marker info in case of multiple markers

I've made a loop to create multiple markers , but the onInfoWindowClickListener works for all the markers with one parameters 我创建了一个循环来创建多个标记,但是onInfoWindowClickListener可以使用一个参数来处理所有标记

 for ( i = 0; i < MyArrListx.size(); i++) {

        n = Double.parseDouble(MyArrListx.get(i));
        e = Double.parseDouble(MyArrListy.get(i));
      final String phone = (MyArrListphonenumber.get(i));
         map.addMarker(new MarkerOptions().position(new LatLng(n, e)) .title("Click to Call Transporter !!").snippet( phone )
              .position(new LatLng(n, e)))
                .setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
               map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {


                if (marker.getSnippet().equals(phone)) // if marker source is clicked
                    dialContactPhone(phone);


            }
        });

It calls the number of the first marker snippet only, with all the marker in time that each snippet contains a different number. 它仅调用第一个标记代码段的编号,所有标记在时间上每个标记段都包含一个不同的编号。

Take the call to setOnInfoWindowClickListener() out of the loop, there is only one InfoWindowClickListener for the map, and it is used for all Markers. setOnInfoWindowClickListener()的调用从循环中取出,该地图只有一个InfoWindowClickListener,并且它用于所有Marker。

Then, just get the phone number from the snippet in the OnInfoWindowClickListener . 然后,只需从OnInfoWindowClickListener的代码段中获取电话号码OnInfoWindowClickListener

This should be sufficient for your needs: 这应该足以满足您的需求:

for ( i = 0; i < MyArrListx.size(); i++) {
  n = Double.parseDouble(MyArrListx.get(i));
  e = Double.parseDouble(MyArrListy.get(i));
  String phone = (MyArrListphonenumber.get(i));
  map.addMarker(new MarkerOptions().position(new LatLng(n, e)) 
                .title("Click to Call Transporter !!").snippet( phone )
                .position(new LatLng(n, e)))
                .setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
}



map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {

            //Just get the phone number stored in the snippet, and dial
            String phone = marker.getSnippet();
            dialContactPhone(phone);
        }
    });

Note, if you need to do something more complicated than just what you can store in the Title and Snippet, then use a HashMap to store your data, and use the Marker ID as the keys of the HashMap, see here for more info: https://stackoverflow.com/a/30602617/4409409 请注意,如果您需要做的事情不只是可以存储在Title和Snippet中,还可以使用HashMap来存储数据,并使用Marker ID作为HashMap的键,请参见此处以获取更多信息: https ://stackoverflow.com/a/30602617/4409409

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

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