Android Google Maps Uniquely identify marker onInfoWindowClick - android

Hi I'm using google android maps v2 and adding a bunch of markers to the map however I need to add additional data to the marker objects for recall later in the onInfoWindowClick function.
I currently have a MarkerManager singleton class which maintains a concurrent list of venues however inside the onInfoWindowClick function I need to recall this via some form of key from the MarkerManager.
It appears that markers are static final and can't be extended so I'm a bit stuck on how to do this.
thanks,
Andy

You can create a HashMap:
Map<Marker, YourCustomObject> theMap;
Then add the markers as keys:
Marker m = mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.whatever)).position(new LatLng(mLat, mLong)));
theMap.put(m, yourCustomObjectInstance);
Finally in your Info Window provider, you can retrieve your object via the marker supplied to the getInfoWindow function`:
mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
YourCustomObject yourCustomObjectInstance = theMap.get(marker);
}

Related

ClusterManager onMarkerClickListener for non-clustered markers

I am using Google Maps in Android with about 800 markers that I want to cluster and 80 markers that I do not want to cluster.
For the markers that I want to cluster, I add to the clusterManager using
mClusterManager.addItem(annotation);
For the markers that I do not want to cluster, I add them directly to the markerCollection
Marker marker = mClusterManager.getMarkerCollection().addMarker(annotation.getMarkerOptions());
Here annoation refers to an instance of AirMapMarker which is a custom class that implements ClusterItem
annotation contains a custom information such as identification string that I need to use when user taps on the marker.
The problem is, I cannot register onClickListener for both the cluster and the mapMarker. If I do, only map's onMarkerClickListener fires.
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
public boolean onMarkerClick(Marker marker) {
// Handle marker click fires correctly.
}
}
mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<AirMapMarker>(){
public boolean onClusterItemClick(AirMapMarker marker) {
// Doesn't Fire
}
}
Now the problem is, inside onMarkerClick, I need access to identification which is part of AirMapMarker, in the case if the marker click happened on a single unclustered marker. I could not figure out a way to map Marker to AirMapMarker inside onMarkerClick.
I also tried completely removing map.setOnMarkerClickListener and rely only on mClusterManager.setOnClusterItemClickListener. However, now the problem is that when the marker click happens on markers that are not part of the cluster, onClusterItemClick fires with parameter null. This is because when adding marker to the map, you provide markerOptions and not ClusterItem object.
There is option inside ClusterManager to turn off clustering but this happens globally and not for a particular markers.
Anyone help me out or give me suggestions on what I should try next?
I don't know if I understood your question right. But here is a possible solution.
The markers you want to cluster, you can add them to the ClusterManager.
clusterManager.addItem(yourItem);
The ones you don't want to cluster, you can add them directly to your GoogleMap object.
map.addMarker
Inside your MarkerRenderer you can override
protected void onClusterItemRendered(YourClusterItem clusterItem, Marker marker)
and map yourClusterItem to your Marker if needed. Or you can set a tag to your marker indicating it's a clusterItem.
In public boolean onMarkerClick(Marker marker)
you can check your tag (or your map) to have a different behaviour for each kind of marker.

Multiple InfoWindowAdatper's

I have a map in which I am creating different types of markers. I cannot assign an info window adapter to a marker (gee wouldn't that be nice), I can only assign on InfoWindowAdapter for the entire map (at least I think).
My problem is that I want to show a different type of info widow depending on what I clicked. Id the only way to set one InfoWindowAdapter that will handle creating the correct type of info window based on the marker that I am passed?
Am I missing something easy?
When you add a marker to the map, you are receiving back an ID, which uniquely identifies your marker.
You can create an instance of your InfoWindowAdapter immediately after you add the marker and put it in a map, which keeps the ID as key and your InfoWindowAdapter as value.
Marker marker = map.addMarker(options);
// Create your special infoWindowAdapter for this marker
// ...
adapterMap.put(marker.getId(), youSpecialInfoWindowAdapter);
In the one central InfoWindowAdapter, which you register at the map, you can just use the ID of the marker to get the specific InfoWindowAdapter and delegate to the methods of that.
Access to the map can e.g. be provided in the constructor of the InfoWindowAdapter (to avoid global or static variables):
class CentralInfoWindowAdapter implements InfoWindowAdapter {
Map<String, GoogleMap.InfoWindowAdapter> adapterMap;
public CentralInfoWindowAdapter(
Map<String, GoogleMap.InfoWindowAdapter> adapterMap) {
this.adapterMap = adapterMap;
}
#Override
public View getInfoContents(Marker marker) {
InfoWindowAdapter adapter = adapterMap.get(marker.getId());
return adapter.getInfoContents(marker);
}
#Override
public View getInfoWindow(Marker marker) {
InfoWindowAdapter adapter = adapterMap.get(marker.getId());
return adapter.getInfoWindow(marker);
}
}
You can vary this principle of course. If you have only a few different InfoWindowAdapters depending on the "type" of the marker, you may put an enumeration into the map, which identifies the type of your marker and lets you decide, which kind of real InfoWindowAdapter to use inside the central InfoWindowAdapter, or you may still put instances of your special InfoWindowAdapter into the map, but use the same instance for the same type of marker.
if i am right you want to show a different window adapter on each marker?.. if yes you can add a tag on each marker then inside one of the two infowindow function either infowindow() or infocontents() checks the marker tag and add the appropriate layout.

Google maps api v2, on marker click to open a fragment on the map

What I want to realise:
I want my markers, that are clickable, to open a fragment on top of the map when I'm in their proximity.
This fragment will list some text and pictures.
I want this fragment to overlay only a part of the googlemap and to be closeable.
I already have some basic info in the 'snippet'-thingy that opens one click.
I also have a function that calculates the distance from the middle of the screen to a location and puts it in the snippet.
some code I have:
simLocatie.setLatitude((googlemap.getCameraPosition().target).latitude);
simLocatie.setLongitude((googlemap.getCameraPosition().target).longitude);
googlemap.addMarker(new MarkerOptions()
.title(een.getName())
.position(new LatLng(een.getLatitude(),een.getLongitude()))
.snippet("distance:"+simLocatie.distance(een))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_monument)));
Thanks in advance!
if I didn't explain myself good enough please ask for more
implement OnMarkerClickListener
and use this method: (it will be called when u click on a marker)
#Override
public boolean onMarkerClick(Marker marker) {
return false;
}
inside this function open your fragment.
http://developer.android.com/guide/components/fragments.html
I suppose that you have unique data for every marker on your map.
When you fetch markers data from internet or locally, you should save them into HashMap.
HashMap<Marker, String> map=new HashMap<Marker, String>();
for(MarkerInfo t : fetchedMarkerArray){
/* add your markers to map, and everything what you need*/
}
When you need to access to data you can easily do that by ( like Daan said you should implement OnMarkerClickListener):
onMarkerClick(Marker marker){
MarkerInfo m = (MarkerInfo)map.get(marker);
//show your fragment with data
}
If you have any additional questions, feel free to ask.

how to remove MarkerOptions objects in google api v2?

I added some Markers in my map and now I want to remove them. Is there any method that I can use to remove my Marker? I used MarkerOptions class to build my Markers and add() method of GoogleMap to add them. And I have a reference of all of my Markers in a List.
I really need your help.
After adding the marker it is possible to obtain its reference:
Marker marker = map.addMarker(..);
Marker class has remove method:
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Marker#remove
Removes this marker from the map. After a marker has been removed, the behavior of all its methods is undefined.
marker.remove()
It will remove all the markers. use this one to remove particular one
for(Marker m : marker)
m.remove();

Google maps API v2 find Marker by id

You can simply add Markers with the new V2 Google maps API on Android. Because the Markers are recreated on configuration change or on save instance, you have to reference them via its ids. The question is how to remove from map Marker with particular id?
My use case is to add Markers to the map, store its id's with mapping to real objects. Then user removes one of this real objects, so I find the Marker id and want to remove Marker from the map and the only way I know about is to have Marker object and call remove() on it.
The documentation is wrong about recreating markers on configuration change and it's actually good for us it is wrong there.
I have also came across similar situation in doing map clustering, where i need to remove the marker when it is added to an cluster.
The solution which i used is that, i am holding the reference to the markers when they are being created and added into the map and store the marker in a Map (String - Marker) ,where key(String) would be an auto-generated marker id, and value would be the marker object.
Now,you can get the reference to the marker object by its id and call remove() on that marker.
I hope this will be helpful to you.
SImply try the following:
private Marker myMarker;
myMarker = getMap().addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!"));
now the marker you want to remove you can call the
myMarker.remove();

Categories

Resources