How to save information of markers of Map v2 so that it can be retrieve on marker click

与世无争的帅哥 提交于 2020-01-23 03:19:12

问题


I am making an app which has map v2 and I am displaying multiple pins successfully. I have followed an tutorial which was just displaying the pin/markers on map v2.

Through service I am getting information of Marker showing the Hotels location on map . The service is sending me the information which include Hotel Name, Location , Food , Services , rating and Latitude and longitude.so let me explain what I have done so far

what I am doing is getting each hotel specification in my asynctask and in this I am calling the following method to add marker

addMarker(Double.parseDouble(latitude), Double.parseDouble(longitude));

addMarker Function is as follows :

public void addMarker(Double Lat, Double Long) {

 if (null != googleMap) {
           MyMapFragment.this.googleMap.addMarker(new MarkerOptions()
                            .position(new LatLng(Lat, Long))
                            .title("You")
                            .draggable(true)


            );
}
}

this is good as it is showing the markers successfully but I know this is not the right thing to do as I wanted each marker to show its own other specific information , so for doing this I know I have to save the information of markers but I don't know how to do it

**What I want **

What I want is to save each pin/marker record against it and on click of that marker open its information in a new window or activity so How can I achieve this , Please share me an code to get the idea or please redirect me to an easy code I do know about the demo project on github But I am not able to understand it as it is complex for me . Please help


回答1:


Unfortunately you cannot add metadata to your markers. What you need to do instead - is to save markers as you add them to some data structure in form of map (<Marker, Model>), so you can retrieve model based on marker.

Let's assume you have a Model class which defines Hotel information. In this case, you can have a map of objects to keep track of your markers:

Map<Marker, Model> markerMap = new HashMap<Marker, Model>();

Now as you add markers, just put them in this map:

public void addMarker(Model hotelModel) {

    if (null != googleMap) {
        Marker hotelMarker = MyMapFragment.this.googleMap.addMarker(new MarkerOptions()
                        .position(new LatLng(hotelModel.getLat(), hotelModel.getLon()))
                        .title(hotelModel.getTitle())
        );

        markerMap.add(hotelMarker, hotelModel);
    }
}

Now when marker is clicked, you can get hotel model based on the marker object:

@Override
public boolean onMarkerClick (Marker marker) {
    Model hotelModel = markerMap.get(marker);
    if(hotelModel != null) {
        Log.d("test", "Hotel "+hotelModel.getTitle()+" is clicked"); 
    }
}


来源:https://stackoverflow.com/questions/31630807/how-to-save-information-of-markers-of-map-v2-so-that-it-can-be-retrieve-on-marke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!