如何計算地圖東點和西點之間的距離(以米爲單位)? 假設用戶改變了滾動地圖的位置,然後我用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)得到舊的中心和新的中心是不夠的,因爲縮放可能已經改變 –