2013-05-31 47 views

回答

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

+1 - 因此通过NE位置和SW位置,您将获得这些位置之间的对角线距离...如果您想查看地图有多少变化,请从didChangeCamera获取旧中心和新中心位置 –

+0

1)但是我如何获得东部和西部点? –

+0

2)得到旧的中心和新的中心是不够的,因为缩放可能已经改变 –

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 

“返回两个坐标之间的大圆距离,以米为单位,在地球上 这是在球体上的两个坐标之间的最短距离这两个坐标必须。有效。”

相关问题