Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to focus google marker by marker title?

i want to focus on marker by giving title right now i am focusing by using lat and lng

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89454, 75.82607), 15.0f));

So is there anyway i could focus on particular marker by Title name of the marker

like image 370
Mohit Kamra Avatar asked Sep 20 '16 08:09

Mohit Kamra


1 Answers

You can store your markers in a Map<String Marker> using the title as the key:

private Map<String, Marker> markers = new HashMap<>();

When you add your markers to the map, you need to also add them to the hashmap:

String markerTitle = "My Marker";
LatLng markerPosition = new LatLng(26.89454, 75.82607);
Marker marker = mMap.addMarker(new MarkerOptions().position(markerPosition).title(markerTitle));
markers.put(markerTitle, marker); // Add the marker to the hashmap using it's title as the key

Then you can center your marker querying the hashmap:

private void centerMarker(String title) {
    Marker marker = markers.get(title);
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(marker.getPosition(), 15f));
}
like image 138
antonio Avatar answered Oct 26 '22 00:10

antonio