[英]How to set zoom level of a Google map according to marker locations
I have placed two markers on a Google map. 我在Google地图上放置了两个标记。 One is in the USA and other one is somewhere in Asia.
一个在美国,另一个在亚洲。 Google only gives zoom levels 1, 2, 3, 4, 5, 6, etc.
Google仅提供缩放级别1,2,3,4,5,6等。
I don't want to hard-code the zoom level. 我不想硬编码缩放级别。 How can I display all markers depending upon distance?
如何根据距离显示所有标记? Ie 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: 您需要将这些标记包含在边界框中,然后使用下面的LatLngBounds对象在其上设置相机:
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 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}});'
我从大约8个月前开始在我的一个项目中使用GMap2 API,看起来我只需要调用'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.
on,以及map div,以使其显示两个位置之间的路径,并适当调整缩放级别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.