Reputation: 14435
How can we add an object to a marker in the new Google Maps Android API v2?
So if we click on the InfoWindow
, we can do something with the object?
public void addSpotOnMap(Spot spot) {
getMap().addMarker(new MarkerOptions()
.position(new LatLng(spot.getParseGeoPoint().getLatitude(), spot.getParseGeoPoint().getLongitude()))
.title(spot.getName())
.snippet(spot.getCategory())
.draggable(false));
}
This sets the location, title and snippet for the object. But I want to be able to go to another activity about this specific Spot-object if I click on the InfoWindow
Upvotes: 25
Views: 22478
Reputation: 1038
You can bind an object within the marker after adding it on the map like:
MarkerOptions markerOptions = new MarkerOptions().position(YOUR_LANG_LAT).title(YOUR_TITLE);
Marker addedMarker = mMap.addMarker(markerOptions);
CustomObject obj = new CustomObject();
addedMarker.setTag(obj);
Then on map click for instance you can retrieve your object as:
mMap.setOnMarkerClickListener(marker -> {
CustomObject obj = (CustomObject) marker.getTag();
return false;
});
Upvotes: 0
Reputation: 21452
Try android-maps-extensions its library where you can add object to mark by setdata method and retrieve data call method getData
Object getData() and setData(Object) on Marker, Circle, GroundOverlay, Polygon, Polyline or TileOverlay
from Gradle You may use any version of Google Play Services
dependencies {
compile 'com.androidmapsextensions:android-maps-extensions:2.2.0'
compile 'com.google.android.gms:play-services-maps:8.1.0'
}
Upvotes: 1
Reputation: 3022
As of Play Services v9.4.0, you can now set any object directly upon a marker!
To save the data:
Marker marker = getMap().addMarker(new MarkerOptions()
.position(lat, lng)
.title(spot.getName());
CustomObject myData = new CustomObject();
marker.setTag(myData);
Then to retreive your data:
CustomObject myRestoredData = (CustomObject)marker.getTag(myData);
For more infomration on marker data, here are the docs.
Upvotes: 24
Reputation: 14435
Not really sure if this is the correct answer, but take a look at this blogpost I made about this problem:
Since Marker
is final
, it's this can easily be solved by linking the data with the Marker
in a Map
, for example HashMap<Marker, YourDataClass>
or HashMap<Marker, String>
Here's a tutorial that explains all: http://bon-app-etit.blogspot.be/2012/12/add-informationobject-to-marker-in.html
Upvotes: 38