简体   繁体   English

如何根据标记位置设置Google地图的缩放级别

[英]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.

相关问题 如何根据 React Native 中的英里设置 google map 中的缩放级别? - How to set zoom level in google map according to miles in react native? 如何在标记上单击动画在最大缩放级别上设置Google地图 - how to set google map on max zoom level with animation on marker click in android 如何在Android的Google地图中根据距地面的高度(例如1KM)设置缩放级别? - How can i set zoom level according to height from ground level (eg.1KM) in google map in android? 如何根据地图的缩放级别显示标记 - how to show markers according to zoom level of map 如何在Android中动态设置谷歌地图上的缩放级别? - How set zoom level on google map dynamically in android? 修复Google-Map中两个Marker之间的缩放级别 - Fix zoom level between two Marker in Google-Map 缩放后Google地图标记的大小也会发生变化 - Google map marker size change upon zoom Level change Android - 如何在Android应用中使用特定位置,缩放级别和标记启动Google地图意图 - Android - How to launch Google map intent in android app with certain location, zoom level and marker 如何调整缩放级别以适应边界,然后在标记偏移中调整中心贴图? - How to adjust zoom level to fit boundary and then center map in marker offset? 如何在Google Map中设置几个特定位置? - How to set several specific locations in Google Map?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM