gpt4 book ai didi

ios - 谷歌地图 iOS SDK : Distance between east and west points on the map

转载 作者:可可西里 更新时间:2023-11-01 04:16:09 24 4
gpt4 key购买 nike

如何计算 map 东点和西点之间的距离(以米为单位)?假设用户改变了滚动 map 的位置,然后我用 mapView:didChangeCameraPosition: 委托(delegate)方法捕捉​​到移动,但我不知道如何计算距离。

最佳答案

这是一个辅助函数,可用于计算两个坐标之间的距离(以米为单位):

double getDistanceMetresBetweenLocationCoordinates(
CLLocationCoordinate2D coord1,
CLLocationCoordinate2D coord2)
{
CLLocation* location1 =
[[CLLocation alloc]
initWithLatitude: coord1.latitude
longitude: coord1.longitude];
CLLocation* location2 =
[[CLLocation alloc]
initWithLatitude: coord2.latitude
longitude: coord2.longitude];

return [location1 distanceFromLocation: location2];
}

更新:

这取决于您所说的 map 的“东”和“西”是什么意思,因为 map 可能会旋转或倾斜。即使它没有倾斜或旋转,由于地球的曲率, map 的顶部和底部之间的宽度也会以米为单位不同(靠近赤道的边缘会比边缘更宽以米为单位)距离赤道较远)。放大得越多,差异就越小。

但是假设您想要获取 map 左下角和右下角之间的距离(以米为单位),您可以使用以下方法:

GMSMapView* mapView = ...;

CLLocationCoordinate2D bottomLeftCoord =
mapView.projection.visibleRegion.nearLeft;
CLLocationCoordinate2D bottomRightCoord =
mapView.projection.visibleRegion.nearRight;
double distanceMetres = getDistanceMetresBetweenLocationCoordinates(
bottomLeftCoord, bottomRightCoord);

如果你想考虑旋转/倾斜,你可以将可见区域包裹在 GMSCoordinateBounds 中,然后计算东南角和西南角之间的距离,例如:

GMSMapView* mapView = ...;

GMSCoordinateBounds* bounds = [[GMSCoordinateBounds alloc]
initWithRegion: mapView.projection.visibleRegion];
CLLocationCoordinate2D southWest = bounds.southWest;
CLLocationCoordinate2D southEast = CLLocationCoordinate2DMake(
bounds.southWest.latitude, bounds.northEast.longitude);
double distanceMetres = getDistanceMetresBetweenLocationCoordinates(
southWest, southEast);

但是,这可能比屏幕上实际可见的区域更宽,因为 GMSCoordinateBounds 将可见区域的四边形包裹在轴对齐的边界框中。

另一种选择是在 map View 框架的每一侧选择点,然后使用 map View 的投影将这些点转换为纬度/经度,例如:

GMSMapView* mapView = ...;

CGPoint middleLeftPoint = CGPointMake(
0, mapView.frame.size.height / 2);
CGPoint middleRightPoint = CGPointMake(
mapView.frame.size.width, mapView.frame.size.height / 2);

CLLocationCoordinate2D middleLeftCoord =
[mapView.projection coordinateForPoint: middleLeftPoint];
CLLocationCoordinate2D middleRightCoord =
[mapView.projection coordinateForPoint: middleRightPoint];

double distanceMetres = getDistanceMetresBetweenLocationCoordinates(
middleLeftCoord, middleRightCoord);

关于ios - 谷歌地图 iOS SDK : Distance between east and west points on the map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16852564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com