Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maps API v2 with different marker actions

Tags:

I'm trying to port my application to the new Google Maps API v2, but i'm having trouble when interacting with markers.

My context: I have a map showing buses and buses stop. With the old library I had my own ItemizedOverlay for bus stops and another one for buses. Each one had a different OnTapListener (actually I use an external library to show balloons), so when the user taps the bus marker nothing happens, but when he taps the bus stop marker an activity with its information is opened. Also, in my ItemizedOverlay I mapped each marker with its bus stop object of the model.

Now with the new API I have 2 main problems:

  • You set a listener with setOnInfoWindowClickListener() or setOnMarkerClickListener() methods of your GoogleMap object. In other words, you can only set one listener for the whole maps, so I can't differentiate between taps on bus stops or buses.
  • The onClick method of both markers receive only the Marker object, from where I can't get too much information like the bus stop number to open its activity (and I hope there is a better way than parsing the title String! O_O)

I think these two problems could be resolved using different subclases of Marker, but it has no constructor and you get its reference when adding it to the map, so I don't know if it's possible to use a custom implementation.

The API is new so there isn't much information on the web about similar problems. I've been trying to figure out how to solve this, but I got nothing. Does anyone know a possible solution to this?

Thanks.

EDIT: A screenshot from my current application's map with two types of markers: A screenshot from my current application's map with two types of marker

like image 560
Sloy Avatar asked Dec 04 '12 23:12

Sloy


People also ask

How do you customize a marker in maps?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.

How many markers can Google Maps API handle?

2048 characters in URL is just under 100 GeoCode values. So, again no more than 100 markers.


2 Answers

I have run into this problem as well. My solution was:

private Map<Marker, MyModel> markerMap = new HashMap<>(); private GoogleMap mMap;  private void doMarkers(){     MarkerOptions opt = new MarkerOptions();     //Fill out opt from MyModel     Marker marker = mMap.addMarker(opt);     markerMap.put(marker, myModel); } 

and then in the OnMarkerClickListener callback, pull your model out of the HashMap using the clicked marker. There is also a method Marker.getId() which for some reason returns a string. I don't understand why you can't specify an int id when you make the marker, or why you can't access the marker object before you add it to the map.

UPDATE: After almost 4 years Google have added a method Marker.setTag(Object tag) to associate arbitrary data with a marker.

like image 139
rockgecko Avatar answered Oct 13 '22 23:10

rockgecko


Ok here is a solution which I decided to use and AFAIK should work for any situation:

private HashMap<String, MyModel> markers= new HashMap<String, MyModel>();   MyModel item = ... MarkerOptions markerOptions = ... markers.put(mMap.addMarker(markerOptions).getId(), item);    @Override public void onInfoWindowClick(Marker marker) {    MyModel mapItem = (MyModel) markers.get(marker.getId());.    ... } 
like image 41
martinpelant Avatar answered Oct 14 '22 00:10

martinpelant