2011-11-03 21 views
5

iPhoneアプリの場合MKMapView の2点間の距離はどのように計算できますか?MkMapviewで2点間の距離を計算するにはどうすればよいですか?

最初の点は、マップビュー内の表示可能なマップの中心点になります。

2番目のポイントは、マップビューの表示可能な矩形の角のいずれかです(ここでは、たとえば左上の点を取っています)。

enter image description here

私はメートルで、この距離を計算します。それをどうすれば実現できますか?

私の目標は、可視のマップ矩形の比率をMKMapviewで計算することです。

答えて

29

あなたはして中央の緯度/経度を取得することができます:LOC1およびLOC2は両方CLLocationのOBJSです

convertPoint:toCoordinateFromView: 

CLLocationDistance dist = [loc1 distanceFromLocation:loc2]; 

これらの2つのヒントを参考にしてください。あなたには、いくつかのコードが必要な場合、私は

+0

感謝を伝えることができます私はどのように緯度の長いやコーナーポイントの座標(2番目のポイント)を取得する私は中心点の座標を得ることができる(第1ポイント) – ios

+0

[この回答](http://stackoverflow.com/questions/2081753/getting-the- bounds-of-an-mkmapview/2082247#2082247)はコーナーポイントの緯度/経度を取得する方法を示しますts。 – progrmr

+0

私はgoogleとチェックします。 mapKitを使用して距離を見つけることが正しくありません。 –

1

:-)知っていることはここでは、指名手配の距離を計算することができる方法です。

// You first have to get the corner point and convert it to a coordinate 
MKMapRect mapRect = self.mapView.visibleMapRect; 
MKMapPoint cornerPointNW = MKMapPointMake(mapRect.origin.x, mapRect.origin.y); 
CLLocationCoordinate2D cornerCoordinate = MKCoordinateForMapPoint(cornerPointNW); 

// Then get the center coordinate of the mapView (just a shortcut for convenience) 
CLLocationCoordinate2D centerCoordinate = self.mapView.centerCoordinate 

// And then calculate the distance 
CLLocationDistance distance = [cornerCoordinate distanceFromLocation:centerCoordinate]; 
+7

centerCoordinateとcornerCoordinateは、distanceFromLocationが機能するにはCLLocation型である必要があります – Daniel

0

uは、返信用スウィフト3+

let distance: CLLocationDistance = location1.distance(from: location2) 
関連する問題