android - 如何计算给定坐标列表的 GEO 中位数并提出 Google map View 的缩放级别

标签 android ios google-maps-android-api-2 google-maps-sdk-ios

我遇到过这样的情况,我应该确定给定坐标列表的中位数并设置 GoogleMap 的相机缩放级别(以便所有坐标在 map 加载时显示在单个 View 中,用户不需要缩放 map )。

我按照以下方法确定给定坐标的中位数。

Convert lat/lon to Cartesian coordinates for each location.
X = cos(lat) * cos(lon)
Y = cos(lat) * sin(lon)
Z = sin(lat)

Compute average x, y and z coordinates.
x = (x1 + x2 + ... + xn) / n
y = (y1 + y2 + ... + yn) / n
z = (z1 + z2 + ... + zn) / n

Convert average x, y, z coordinate to latitude and longitude.
Lon = atan2(y, x)
Hyp = sqrt(x * x + y * y)
Lat = atan2(z, hyp)

引用:Calculate the center point of multiple latitude/longitude coordinate pairs

现在我想设置 Google map 的相机缩放值,以便在单个 View 中覆盖所有坐标。

例如:

如果确定坐标列表出现在加利福尼亚和佛罗里达,则加载 Google map 时应显示整个美国。

如果确定坐标列表存在于加利福尼亚和德克萨斯之间,则在设置 GoogleMap 的缩放值时应仅显示美国 map 的那部分。

我正在 Andriod 应用程序和 iOS 中实现它。

让我知道我们如何才能做到这一点。

最佳答案

iOS SDK 具有类似的功能。您可以创建一个 GMSCoordinateBounds然后用 GMSCameraUpdate:fitBounds 创建一个新相机并调用animateWithCameraUpdate:在 mapView 对象上,传入新相机。

关于android - 如何计算给定坐标列表的 GEO 中位数并提出 Google map View 的缩放级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17050439/

相关文章:

android - 如果 project.properties 说它是自动生成的,我该如何编辑?

java - OnTouchListener 没有被解雇

iphone - 无法从 UIViewController 加载 xib 文件

ios - 显示来自 url 的图像数组的问题

Android - 谷歌地图 API key 不起作用

Android map v2 缩放以显示所有标记

android - 在应用程序之间共享代码

android - 有没有办法从APK文件中获取源代码?

iphone - Selenium iOS 驱动程序

android - 为什么 GoogleMap.animateCamera() 在 onGlobalLayout() 之后崩溃?