Distinguish different markers in Maps API v2 (unique identifiers)

I have a custom class ArrayList. There are about 10 objects in the list, each of which has elements such as Title, Snippet, LatLng. I successfully added all 10 to the Map using my custom class functions such as getTitle, getSnippet, getLatLng, etc.

Now, when I click on the info window (marker), I want to be able to somehow KNOW which object of my user class this marker corresponds to.

For example, if I click on the McDonald market, I want to know which element from my ArrayList array belongs to this marker.

I looked at MarkerOptions, and there seems to be nothing there that I can use to identify the corresponding custom object.

If the question is too confusing, let me make it simple:

ArrayList<CustomObj> objects = blah map.addMarker(new MarkerOptions().position(new LatLng( Double.parseDouble(result.get(i).getCompanyLatLng() .split(",")[0]), Double.parseDouble(result .get(i).getCompanyLatLng().split(",")[1]))) .title(result.get(i).getCompanyName()) .snippet(result.get(i).getCompanyType()) .icon(BitmapDescriptorFactory .fromResource(R.drawable.pin))); 

Now, when clicked, I move on to the next page. The next page should know that the WHICH object was clicked so that I can send other data to this page (for example, the URLs of the images that need to be uploaded, etc.).

How to add a unique integer or any identifier to my marker?

+6
source share
5 answers

One correct way is to use Map<Marker, CustomObj> , which stores all the markers:

 Marker marker = map.addMarker(...); map.put(marker, result.get(i)); 

and in onInfoWindowClick :

 CustomObj obj = map.get(marker); 

Another attempt is to use Android Maps Extensions , which add the getData and setData method to the Marker , so you can assign your CustomObj after creating the marker and receiving it in any callback.

+6
source

I used snippet text to store a unique identifier. If you need a fragment, this will be for the pop-up window, and you can just make your own (by searching for the object from the identifier) ​​so that you do not miss it, but you will definitely miss the unique identifier for identifying objects.

To find the desired object, I just iterate over them:

 for(SomeObject anObject : someObjects) { if(marker.getSnippet().equalsIgnoreCase(anObject.getID())) { //you got at match } } 
+1
source

According to the discussion at the following link, Marker.equals and Marker.hashCode sometimes do not work. For example, when a card is tinted or enlarged, or it is renewed / renewed.

Add tag / id to markers

Thus, the token identifier is a better key than the token itself for the HashMap.

 Map<String, MyObject> markerMap = new HashMap<String, MyObject>(); Marker marker = map.addMarker(...); MyObject myObject = new MyObject(); markerMap.put(marker.getId(), myObject); 
+1
source

I think using latitude and longitude this can be done

 map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(final Marker marker) { LatLng pos = marker.getPosition(); int arryListPosition = getArrayListPosition(pos); return true; } }); 

and the getArrayListPosition method is equal

 private int getArrayListPosition(LatLng pos) { for (int i = 0; i < result.size(); i++) { if (pos.latitude == result.get(i).getCompanyLatLng().split(",")[0]) { if (pos.longitude == result.get(i).getCompanyLatLng() .split(",")[1]) return i; } } return 0; } 

This method will return you the position of your ArrayList , who Marker you clicked. and you can get data from this position.

Hope this helps ...

0
source

I'm 4 years late, but I really wonder why no one talked about marker.setTag and marker.getTag .

As written in google API docs,

This is easier than storing a separate Map<Marker, Object>

They introduced setTag and getTag to avoid using Map<...> . You can use it as follows.

 Marker marker = map.addMarker(...); marker.setTag(result.get(i)); //Store your marker information here. //To fetch your object... map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { CustomObj obj = (CustomObj)marker.getTag(); //Your click handle event // Pass the obj to another activity and you are done return false; } }); 
0
source

Source: https://habr.com/ru/post/945682/


All Articles