How to save v2 map markers information so that it can be found when a marker is clicked

I am making an application with map v2 and I am showing multiple contacts successfully. I followed a tutorial that just showed pin / markers in v2 map.

Through the service, I receive information about the Marker, showing the location of the hotels on the map. The service sends me information that includes the hotel name, location, food, services, rating, and latitude and longitude. Let me explain what I have done so far.

what i am doing is getting each hotel spec in my asynthet and in that i call the following add marker method

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

      

addMarker Function looks like this:

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 shows the markers successfully, but I know this is the wrong thing as I wanted each marker to show its own other specific information, so for this I know I need to store information about but I don't know how do it.

** What I want **

What I want is to save each entry / marker entry against it and click on that marker, opening my info in a new window or in an action. How can I achieve this. Please share me the code to get this idea, or please redirect me to the lightweight code I know about the demo project on github. But I cannot understand it because it is difficult for me. Please, help

+3


source to share


1 answer


Unfortunately, you cannot add metadata to your markers. Instead, you need to store the markers by adding them to some data structure as a map ( <Marker, Model>

) so that you can get a marker based model.

Suppose you have a class Model

that defines hotel information. In this case, you can have a sitemap to track your markers:

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

      

Now when you add markers, just place them on 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 that the marker is clicked, you can get the 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"); 
    }
}

      

+3


source







All Articles