How to set zoom level of a Google map according to marker locations
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.
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);
}
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
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.
精彩评论