如何计算地图东点和西点之间的距离(以米为单位)? 假设用户改变了滚动地图的位置,然后我用mapView:didChangeCameraPosition:delegate方法来捕捉移动,但是我不知道如何计算距离。Google Maps iOS SDK:地图上东西点之间的距离
5
A
回答
9
这里只是一个辅助功能,可用于计算两个坐标之间以米为单位的距离:
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];
}
UPDATE:
这取决于你的“东方”和“西方”是什么意思因为地图可能会旋转或倾斜。即使它没有倾斜或旋转,由于地球曲率的原因,地图顶部和底部之间的地图宽度将以米为单位不同(靠近赤道的边缘的宽度将以米为单位)距离赤道更远)。差距会越小,放大得越多。
但是假如你想获得米的距离之间的左下方和地图的右下角,你可以这样做:
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
WR在一个轴对齐的边界框中可见区域的四边形。
另一种选择是选择点在地图视图上的框架的每一侧,然后使用地图视图的投影到这些转换成纬度/经度,例如:
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);
0
下面有用于寻找距离的代码两点之间
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
NSString *urlString [email protected]"https://maps.googleapis.com/maps/api/directions/json";
NSDictionary *dictParameters = @{@"origin" : [NSString stringWithFormat:@"%@",_sourceAdd], @"destination" : [NSString stringWithFormat:@"%@",_destinationAdd], @"mode" : @"driving", @"key":@"AIzaSyD9cWTQkAxemELVXTNUCALOmzlDv5b9Dhg"};
[manager GET:urlString parameters:dictParameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *arr=responseObject[@"routes"][0][@"legs"];
NSMutableArray *loc=[[NSMutableArray alloc]init];
NSString *dis,*dur;
loc=[[arr valueForKey:@"distance"]valueForKey:@"text"];
dis=loc[0];
loc=[[arr valueForKey:@"duration"]valueForKey:@"text"];
dur=loc[0];
UIAlertView *av=[[UIAlertView alloc]initWithTitle:@"Route Info" message:[NSString stringWithFormat:@"Distance:%@ \nDuration:%@",dis,dur] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
[av show];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
2
Google Maps iOS SDK包含一堆geometry helpers。
你要这个功能:
GMSGeometryDistance (CLLocationCoordinate2D from, CLLocationCoordinate2D to) -> CLLocationDistance
“返回两个坐标之间的大圆距离,以米为单位,在地球上 这是在球体上的两个坐标之间的最短距离这两个坐标必须。有效。”
相关问题
- 1. 计算iOS上地图上2点之间的距离
- 2. 两点之间的距离。东西+南北
- 3. 谷歌地图:指定路线上两点之间的距离
- 4. 计算Google Maps for Ionic 2中两点之间的距离
- 5. 使用Google Maps API查找2个点之间的距离
- 6. 获取两个地理点之间的距离 - 离线地图
- 7. 如何使用Google地图来确定两个地点之间的距离?
- 8. 从地图上的一点到另一点之间的距离
- 9. Android地图上2个地点之间的距离
- 10. 在两个地点之间的道路上获得距离
- 11. Google Maps API:与航点的总距离
- 12. 地址之间的距离
- 13. Google Maps API来计算出发地和目的地之间的确切距离
- 14. 计算没有Google地图的地址之间的距离
- 15. 在iOS上使用Google Maps SDK保存我的地点
- 16. 如何显示地图上两点之间的距离
- 17. 查找地图上线条和点之间的距离
- 18. 两个地址之间的直线距离 - Google地图API
- 19. 两点之间的测地距离
- 20. 两个地理点之间的距离?
- 21. 道路距离,两地之间的实际距离
- 22. 两点之间的距离谷歌地图a是undefined api v3
- 23. Google Maps API距离计算
- 24. 点之间的距离
- 25. 无法计算谷歌地图中两点之间的距离
- 26. 有没有任何API来确定Google Maps的2个点之间的距离?
- 27. 计算LatLng与LatLng之间的距离(或圆圈中的点数) - Google Maps v3
- 28. 点之间的相等距离高图
- 29. 如何使用Google Maps API计算两个地址之间的驾驶距离?
- 30. 如何通过Google Maps Javascript API3获得2点之间的距离?
+1 - 因此通过NE位置和SW位置,您将获得这些位置之间的对角线距离...如果您想查看地图有多少变化,请从didChangeCamera获取旧中心和新中心位置 –
1)但是我如何获得东部和西部点? –
2)得到旧的中心和新的中心是不够的,因为缩放可能已经改变 –