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

前端 未结 5 2135
挽巷
挽巷 2020-12-09 20:15

I am making an application with a Google Maps on it, for Android. I have plenty of Markers on my screen and I am preparing customizable balloon for each marker when they are

相关标签:
5条回答
  • 2020-12-09 20:29

    There is a better option, and this is what google suggests:

    Tag : An Object associated with the marker. For example, the Object can contain data about what the marker represents. This is easier than storing a separate Map<Marker, Object>. As another example, you can associate a String ID corresponding to the ID from a data set. Google Maps Android API neither reads nor writes this property.

    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;
        }
    });
    
    0 讨论(0)
  • 2020-12-09 20:31

    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

    You can control it. The marker is returned by addMarker(), so you can get its ID and store it. 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

    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

    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                         
        }
    });
    
    0 讨论(0)
  • 2020-12-09 20:32

    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.

    Depending on how and if you already keep references to all markers, there are alternatives, e.g. if you had List<Marker> policeMarkers and List<Marker> badGuysMarkers you can use a conditional if (policeMarkers.contains(marker)) { ... } else { ... }.

    You can also keep a Map<Marker, YourMarkerRelatedDataModel> allMarkers and do 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>.

    0 讨论(0)
  • 2020-12-09 20:43
      map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                    public void onInfoWindowClick(Marker marker) {
    
                        double lat=marker.getPosition().latitude;
                        double lng=marker.getPosition().longitude;
    
    
                    }
                });
    
    0 讨论(0)
  • 2020-12-09 20:44

    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());
          }
        }
    
    0 讨论(0)
提交回复
热议问题