3

I have placed two markers on a Google map. One is in the USA and other one is somewhere in Asia. Google only gives zoom levels 1, 2, 3, 4, 5, 6, etc.

I don't want to hard-code the zoom level. How can I display all markers depending upon distance? I.e. if two items are very close to each other or in the same city, a close zoom level should be used, and if they're far apart, then maximum zoom level should be used.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278
  • Are you talking about Google Maps API on the web or about the MapView class used in Android to display Google maps? – Flo Dec 01 '10 at 08:56
  • this is the answer you are asking for it is in this link I have this issue too but solved. http://stackoverflow.com/questions/5114710/android-setting-zoom-level-in-google-maps-to-include-all-marker-points – Talha Mar 14 '11 at 13:06

3 Answers3

1

You need to include those markers into a bounding box, then use the LatLngBounds object below to set the camera on it:

private void addMarkers() {
    MarkerOptions markerOptions = new MarkerOptions();
    LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();

    //add the marker locations that you'd like to display on the map
    boundsBuilder.include(new LatLng(lat1, lng1));
    boundsBuilder.include(new LatLng(lat2, lng2));

    final LatLngBounds bounds = boundsBuilder.build();

    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
    map.animateCamera(cameraUpdate);
}
Flavius
  • 447
  • 3
  • 9
1

You can specify the zoom level on location and with zoom level

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37,
            -121), 6)); 

https://developers.google.com/maps/documentation/android-api/views

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
skryshtafovych
  • 572
  • 4
  • 16
0

What are you using to generate your maps? I'm making use of the GMap2 API in a project of mine from about 8 months back, and it looks like I just had to call 'GDirections(map{{div}});' on, well, the map div in order to get it to display a route between two locations, and adjust the level of zoom appropriately.

Laereom
  • 601
  • 7
  • 12