谷歌地图iOS SDK:地图上东西点之间的距离
如何计算地图的东点和西点之间的距离(以米为单位)?谷歌地图iOS SDK:地图上东西点之间的距离,ios,google-maps-sdk-ios,Ios,Google Maps Sdk Ios,如何计算地图的东点和西点之间的距离(以米为单位)? 假设用户更改了滚动地图的位置,然后我使用mapView:didChangeCameraPosition:delegate方法捕捉到了移动,但我不知道如何计算距离 这里有一个辅助函数,可用于计算两个坐标之间的距离(以米为单位): double getDistanceMetresBetweenLocationCoordinates( CLLocationCoordinate2D coord1, CLLocationCoordina
假设用户更改了滚动地图的位置,然后我使用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];
}
更新:
这取决于地图的“东”和“西”是什么意思,因为地图可能会旋转或倾斜。即使它没有倾斜或旋转,由于地球的曲率,地图的顶部和底部之间的宽度将以米为单位有所不同(靠近赤道的边缘将比远离赤道的边缘宽米)。放大的越多,差别就越小
但是,假设您想获得地图左下角和右下角之间的距离(以米为单位),您可以使用:
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);
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);
但是,这可能比屏幕上实际可见的区域更宽,因为GMSCoordinateBounds
将可见区域的四边形包裹在轴对齐的边界框中
另一个选项是选择地图视图框架两侧的点,然后使用地图视图的投影将这些点转换为lat/lon,例如:
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);
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);
这是计算两点之间距离的代码
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
NSString *urlString =@"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);
}];
GoogleMaps iOS SDK包含一系列的功能 您需要此功能:
GMSGeometryDistance (CLLocationCoordinate2D from, CLLocationCoordinate2D to) -> CLLocationDistance
返回地球上两个坐标之间的大圆距离,单位为米。
这是球体上两个坐标之间的最短距离。两个坐标都必须有效。“+1-因此,通过NE位置和SW位置,就可以得到它们之间的对角线距离。。。如果你想看看地图改变了多少,从DidChangeCamera获取旧中心和新中心位置2)获取旧中心和新中心是不够的,因为缩放可能会发生变化。我添加了一个更新,其中包含有关如何计算它的更多信息-这取决于你所指的东和西。不用担心:)顺便说一句,我添加了你建议的代码修复,谢谢-不知道为什么其他人拒绝了它。