简体   繁体   English

Android:在给定点的情况下设置Google地图缩放和位置的算法

[英]Android: Algorithm to set zoom and location of Google map given a sets a points

I have an ArrayList, each of these LatLng is a marker on my map The list can have anywhere from 0 to 1 to n points. 我有一个ArrayList,每个LatLng都是我的地图上的标记。该列表可以包含0到1到n个点。 They may or may not be distant within each others. 它们彼此之间可能遥远,也可能不遥远。

Can someone help me, given a height ("h"dp) and a width ("w"dp) of the map area, determine: 给定地图区域的高度(“ h” dp)和宽度(“ w” dp),有人可以帮助我确定:

  1. the "center" of this set 这个集合的“中心”
  2. the appropriate zoom level (z) of this set, so that all markers are visible within the frame of size "h" by "w", but a (z+1) zoom level will not show all the points. 适当的缩放级别(z),以便在大小为“ h”乘“ w”的帧中可见所有标记,但是(z + 1)缩放级别不会显示所有点。

To find the center of a set of LatLng points: 要找到一组LatLng点的中心,请执行以下操作:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng point: pointSet) {
   builder.include( point);
}
LatLngBounds bounds = builder.build();
LatLng centerPoint = bounds.getCenter();


As for zooming, the simplest way would be to call map.animate: 至于缩放,最简单的方法是调用map.animate:

mapObj.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, PADDING_VALUE));

Which is guaranteed to set the map to the greatest zoom level that contains the bounds input (plus padding). 确保将地图设置为包含边界输入(加上填充)的最大缩放级别。


And if you're still interested in knowing the required zoom level: 如果您仍然对知道所需的缩放级别感兴趣:

float zoomLevel = mapObj.getCameraPosition().zoom; // call after(!) animateTo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mapbox Android SDK 4.0.0+ - 如何在给定位置设置缩放和中心地图? - Mapbox Android SDK 4.0.0+ - how to set zoom & center map on given location? 用于在Android上计算Google地图路线上的点的算法 - Algorithm for calculating points on the google map route for android 放大google map android - zoom in google map android 在Android中保存地图缩放和位置 - Save map zoom and location in Android 设置时间以更新Google Map Android中的位置 - Set time to update location in Google Map Android Android:Google地图当前位置和其他标记点 - Android:Google map current location and other marker points 在Map Overylay(Android 2.1)上给出一组点绘制空多边形 - Drawing an empty polygon given a set of points on a Map Overylay (Android 2.1) 我的 Android 手机上未完全加载 Google 地图,但显示了我当前的位置按钮和放大/缩小按钮 - Google map is not fully loaded on my android phone but my current location button and zoom in/zoom out button are showing 如何在Android中动态设置谷歌地图上的缩放级别? - How set zoom level on google map dynamically in android? 在Android上的Google Map应用中设置自定义缩放按钮意外停止 - To set custom zoom buttons in Google Map app on android stopped Unexpectedly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM