2013-05-31 48 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 

「返回兩個座標之間的大圓距離,以米爲單位,在地球上 這是在球體上的兩個座標之間的最短距離這兩個座標必須。有效。」

相關問題