| Issue 4650: | Add Tag / Identifier to Markers | |
| 101 people starred this issue and may be notified of changes. | Back to list |
There seems to be a lot of oversights in this new API. One of the ones I just noticed is that there is no way to cross-reference a Marker with a dataset. For example, I have an array of records, and there is a marker for each record. When I get the callback for marker selection, I have no way of knowing what record is associated with the selected marker to do work with. Normally, I would solve this by subclassing Marker and add the appropriate fields, such as a unique record identifier. However, because of the decision to go with a factory style pattern (passing MarkerOptions to the map), I as the developer have no control over the type of class that is instantiated. This goes for TileOverlays and Polygons as well. Please add a way to either provide our own Marker (TileOverlay, Polygon) subclasses or at least give us an Object field that we can use as needed.
Dec 6, 2012
#1
[email protected]
Dec 6, 2012
I have actually decided to use an LruCache subclass instead of a HashMap so I can limit how many markers /records are attached at a time. While HashMap's are a workaround, when your data objects are heavy in terms of memory, having to keep them around for this purpose is just a bad idea.
Dec 6, 2012
I never said you have to keep all of your data :) You can have a HashMap<Marker, Long> and load the data for the given Marker using its associated long identifier only when it is clicked.
Dec 6, 2012
=) Yes that is true and I realized after I said it. Still, having to do this ourselves because of poor API design choices is just sad. =(
Dec 6, 2012
I agree. Moreover HashMap<Marker, Long> involves autoboxing overhead.
Dec 11, 2012
related problem: http://stackoverflow.com/questions/13762150/how-to-persist-marker-from-google-api-android-v2/13816635
Dec 11, 2012
Agreed, having an Object field on Marker would make things a lot easier. Kind of like what we do with ListAdapter and view.getTag()
Dec 17, 2012
If you go the HashMap route it is better to use Marker.getId rather than the Marker pointer as the Id survives a restart. Unfortunately there is no corresponding setId- the Id is auto-generated and appears to be 'm'+marker.count
Jan 1, 2013
Agreed, a setTag() and getTag() would be so useful! We need to make a lot of workarounds for now...
Jan 4, 2013
We also need this badly. :)
Jan 4, 2013
(No comment was entered for this change.)
Labels:
Internal-7671654
Jan 8, 2013
(No comment was entered for this change.)
Status:
Acknowledged
Feb 1, 2013
As MarkerOptions implements Parcelable (thus are saved and restored) I guess that the API should be something like: - void setTag(Parcelable p) - Parcelable getTag() or maybe better: - void setData(Bundle b) - Bundle getData()
Feb 1, 2013
It seems the GoogleMap doesn't retain the Marker object it returns after calling addMarker(), so the workaround of keeping a HashMap<Marker, ?>, I found it was necessary to also keep a HashMap<LatLng, ?>. After some period of time (not sure the criteria), the GoogleMap passes a different Marker instance which happens to have the same data but is a different object, which causes the HashMap<Marker, ?> lookup to fail. There doesn't seem to be a callback for when the Marker object is replaced, so it's not possible to update your HashMap<Marker, ?> accordingly. The Marker's position LatLng's latitude and longitude attributes seem to gain arbitrary precision, so to look up against the HashMap<LatLng, ?>, you must first construct a new LatLng with rounded values from the Marker's position. Hopefully your original precision is known and not subject to change! Hopefully there will be an update with a Tag feature soon. It is very sorely needed.
Feb 1, 2013
There is a getTag() function which you can use to populate HashMaps but there is no way to set it- It's an arbitary 'M999'- The letter M followed by the Marker's position in the internal array.
Feb 1, 2013
@16, What object has the getTag() function? As far as I can tell, the Marker object does not.
Feb 1, 2013
Apologies, it's getId() and it is part of the Marker class.
Feb 28, 2013
The getId() method is good solution, becouse you can store id's of added markers and eventually map them to your objects. The problems comes, when you want to remove particular marker from the map, there is no getMarkerById(String gMarkerId) method in the GoogleMap class :-(
Feb 28, 2013
@15: I do not agree with you. The HashMap<Marker, ?> perfectly works because it retains a reference on the Marker. Moreover you don't have to care about Marker actually being different because HashMap internally uses hashCode() and equals(Object). These methods works perfectly even if the Marker is different but the underlying state is the same. @19: Using a HashMap<Marker, ?> should solve your problems.
Feb 28, 2013
@20: In the December release of the v2 SDK, I noticed sometimes my OnMarkerClickListener couldn't find a value for the passed Marker in the HashMap<Marker, ?>, which received every Marker added to the map unconditionally. I suspect there must be some internal state in the Marker which is changing, and causing a different hashcode to return.
Feb 28, 2013
To clarify, the problem where the Marker cannot be matched in a HashMap<Marker, ?> only seemed to happen when there were many Markers (often 100 or more) on the map, and only after panning and zooming around different areas of the map (moving the Markers in and out of view) and periodically tapping Marker objects. After doing that for several minutes, the HashMap<Marker, ?> was no longer able to get a value for some (though not all) of the Marker objects passed into the OnMarkerClickListener.
Mar 18, 2013
While waiting for this to be implemented in GM Android API v2 you may want to try a small library project of mine called Android Maps Extensions which adds getData and setData to Markers and other objects. https://code.google.com/p/android-maps-extensions/
May 22, 2013
Hi Carlton, That sounds like a bug. If you're able to reproduce it consistently, could you please file a bug report? Thanks, Chris
Jul 3, 2013
Hello all. can any Project Member tell us which is the status of the issue. we really need add at least just a reference to other Object. Methods setTag() and getTag() will work fine if you don´t really want let us extend Marker. Thank you a lot. Alvaro
Jul 6, 2013
Sure would be nice if the internal components of custom InfoWindow objects were able to have listeners, instead of the whole view being turned into an image. If you were to touch a button inside a marker's info window on the actual Google Maps app, then why hasn't that functionality been offered in the Google Play services?
Aug 1, 2013
Hi, I have noticed the same problem than Carlton on #21 #22, although for me it happened after opening another activity (on marker click) and going back to the map. I also noticed that: - it doesn't seem to happen the first time you click a marker - by clicking several times in a row on the same marker, I could get my object back even if it seemed to be lost at first Tested on Android 2.3.3
Aug 1, 2013
#27, Pierre: Are you able to reproduce this consistently? If so, could you please file a bug report? It would be most helpful! #26 (miguelmora), that's completely unrelated to this feature request. Please see (and star) Issue 5035.
Nov 13, 2013
There is getId function associated with marker if use this marker.getId().substring(1) you will get the position you can use this to retrieve the data from arraylist or any other datatype for that position .
Mar 31, 2014
I would like to extend Marker, because then we may be able to get additional data like tag or others in methods like onMarkerClick(Marker arg0) and onInfoWindowClick(Marker arg0) if it's not a security issue. This would simplify my code and speed up my app. 😀
Apr 6, 2016
Issue 5019 has been merged into this issue. |
|
| ► Sign in to add a comment |