get other values from marker

You cannot add anymore data to the marker, unless you all put it in the snippet.

So if you want to add more data to your marker, you will have to store it in an external variable which is linked to your marker.

Each made marker has an unique ID.

You can use this information to save any additional data mapped to this ID in a separate variable.

Example:

Create a variable you can access:

 /**
  * Create your variable where you store all your data mapped to the marker ID, 
  * make it accessible where you want.
  * The key of this hashmap is your marker ID, the value is another Map with extra data
  */
 HashMap<String, HashMap> extraMarkerInfo = new HashMap<String, HashMap>();

OPTION 1 Change your marker code so you add extra data to this variable

for (HashMap<String, String> hashMap : contactList) {
     // Create your marker like normal, nothing changes
     Marker marker = map.addMarker(new MarkerOptions()
         .position(new LatLng(Double.valueOf(hashMap.get(TAG_LAT)) , Double.valueOf(hashMap.get(TAG_LNG))))
         .title(hashMap.get(TAG_ADDRESS))
         .snippet(hashMap.get(TAG_NAME)));

     // When you created the marker you store the extra data from your JSON in another variable (HashMap for example)
     HashMap<String, String> data = new HashMap<String, String>();

     data.put(TAG_Location,hashMap.get(TAG_Location));
     data.put(TAG_Company,hashMap.get(TAG_Company));
     data.put(TAG_PLACE,hashMap.get(TAG_PLACE));
     data.put(TAG_POSTAL,hashMap.get(TAG_POSTAL));
     data.put(TAG_CITY,hashMap.get(TAG_CITY));
     data.put(TAG_MONDAY,hashMap.get(TAG_MONDAY));
     data.put(TAG_TUESDAY,hashMap.get(TAG_TUESDAY));
     data.put(TAG_WEDNESDAY,hashMap.get(TAG_WEDNESDAY));
     data.put(TAG_THURSDAY,hashMap.get(TAG_THURSDAY));
     data.put(TAG_FRIDAY,hashMap.get(TAG_FRIDAY));
     data.put(TAG_SATURDAY,hashMap.get(TAG_SATURDAY));
     data.put(TAG_SUNDAY,hashMap.get(TAG_SUNDAY));
     data.put(TAG_TYPE,hashMap.get(TAG_TYPE));
     data.put(TAG_NOCAR,hashMap.get(TAG_NOCAR));

     // Save this marker data in your previously made HashMap mapped to the marker ID. So you can get it back based on the marker ID
     extraMarkerInfo.put(marker.getId(),data);

}

EASY OPTION 2 Just save the entire map in the external variable

for (HashMap<String, String> hashMap : contactList) {
     // Create your marker like normal, nothing changes
     Marker marker = map.addMarker(new MarkerOptions()
         .position(new LatLng(Double.valueOf(hashMap.get(TAG_LAT)) , Double.valueOf(hashMap.get(TAG_LNG))))
         .title(hashMap.get(TAG_ADDRESS))
         .snippet(hashMap.get(TAG_NAME)));

     // Just save the entire json hashmap into the external variable
     extraMarkerInfo.put(marker.getId(),hashMap);

}

Now you can access that extraMarkerInfo whenever you want, you know the Marker id by using getID().

@Override
public void onInfoWindowClick(Marker marker) {

    String title = marker.getTitle();
    String snippet = marker.getSnippet(); 

    // Get extra data with marker ID
    HashMap<String, String> marker_data = extraMarkerInfo.get(marker.getId());

    // Getting the data from Map
    String location = marker_data.get(TAG_Location);
    String company = marker_data.get(TAG_Company);
    String place = marker_data.get(TAG_PLACE);
    //.. etc etc
}

This is just a small example using HashMaps, but you can use different solutions whatever you prefer.


A really nice and easy way of doing this is - whenever you add a marker to your map, make a reference to it in something like HashMap<Marker, <WhateverDataYouNeed> extraMarkerData

Then whenever you want its data, all you have to do is call extraMarkerData.get(<yourMarker>) to return it. simples.

Thanks to this kind chap who wrote a tutorial, I found it to be quite extensive for what I needed but it provided the simple idea:

http://bon-app-etit.blogspot.be/2012/12/add-informationobject-to-marker-in.html

My extra data looks like this:

private HashMap<Marker,HashMap<String,String>> markerUserData;

Then whenever I add a marker to my map, I make sure I make a reference to its data using the marker as a key

Marker marker = map.addMarker(new MarkerOptions()
        .position(new LatLng(<lat>,<lng>)
        .title(<markerTitle>)
        .snippet(<markerSnippet>)
);

markerUserData.put(marker,<myExtraMarkerDataHashMap>);    //add reference to marker data

And since I have my own class to handle OnInfoWindowClickListener, I've made a public getter

public HashMap<String,String> getDataforMarker(Marker marker){
    return extraMarkerData.get(marker);
}

OnInfoWindowClickListener:

@Override
public void onInfoWindowClick(Marker marker) {
    Log.i("SFCT","Data for marker = "+activity.getDataforMarker(marker));
}

Tags:

Android

Marker