4

(基於評論:)MKCoordinateRegion距離

OK,我會試着問其他的方式......我怎樣才能得到這個圈子覆蓋的邊界座標經修訂的課題:

enter image description here


(原始的問題:)

我有我的iPhone應用程序奇怪的問題。我有MKCoordinateRegion,其中心座標的緯度爲:51.509980,經度:-0.133700。我已經使用MKCoordinateRegionMakeWithDistance方法,並將距離設置爲16093.44米(10英里)。

我想獲得這個區域的邊界點,因此我有這樣的代碼:

MKCoordinateRegion region2 = self.myMapView.region; 

float minLat = region2.center.latitude - (region2.span.latitudeDelta/2.0); 
float maxLat = region2.center.latitude + (region2.span.latitudeDelta/2.0); 

float minLong = region2.center.longitude - (region2.span.longitudeDelta/2.0); 
float maxLong = region2.center.longitude + (region2.span.longitudeDelta/2.0); 

,我發現這個網站,我的測試http://boulter.com/gps/distance/其計算兩個座標之間的距離。當我進入一個FROM座標:51.509980和經度:-0.133700(倫敦),並協調:

2011-11-26 01:15:42.830 NearMeTest[3911:11603] MinLAT 51.334381 and MaxLAT 51.684814 
2011-11-26 01:15:42.830 NearMeTest[3911:11603] MinLONG -0.352936 and MaxLONG 0.086517 

我拿到這兩個座標之間的距離是15.40英里,而不是預期的有10英里。

截圖:

enter image description here

爲什麼這樣的差異?當我試圖做同樣的事情,但從不同的中心座標(東京,紐約)結果是正確的10英里。

感謝您的回覆

回答

2

(修訂答基於評論:)

如果你的意思是你想要的那個圈覆蓋的邊界矩形的座標,使用重疊的boundingMapRect屬性:

//"theCircle" is the MKCircle overlay object 

CLLocationCoordinate2D topLeftCoord = 
    MKCoordinateForMapPoint(theCircle.boundingMapRect.origin); 

MKMapPoint bottomRightMapPoint = MKMapPointMake (
    MKMapRectGetMaxX(theCircle.boundingMapRect), 
    MKMapRectGetMaxY(theCircle.boundingMapRect)); 

CLLocationCoordinate2D bottomRightCoord = 
    MKCoordinateForMapPoint(bottomRightMapPoint); 


(原始回答:)

首先,當您在地圖視圖上調用setRegion時,地圖視圖將幾乎總是修改您請求的區域,以使其適合地圖視圖。此調整基於地圖視圖的形狀以及是否可以在其固定縮放級別之一正確顯示請求的跨度。

例如,如果您的地圖視圖不是正方形,並且您要求雙向10英里的跨度,則至少有一個跨度肯定會進行調整。即使您要求根據視圖的比例設置跨度,但如果地圖視圖無法在縮放級別顯示切片(或者如果您沒有拍攝地球的切片曲率考慮)。

接着,latitudeDeltalongitudeDelta定義區域的整個高度和寬度(未中心座標的距離)。

因此,您在屏幕截圖中的測試不能與跨度三角洲進行比較。在屏幕截圖中,您正在計算從中心座標到最小緯度和經度(左下角)的距離,但跨度增量一直從右到左和從下到上。 (因此,你會認爲從中心到角落的距離應該小於三角洲 - 不要超過三角洲,它會縮短,但由於上述原因,三角洲也會增加到10多。)

最後,拿到角球座標(左下和右上),這可能是一個更準確的方式來做到這一點:

CLLocationCoordinate2D bottomLeftCoord = 
    [myMapView convertPoint:CGPointMake(0, myMapView.frame.size.height) 
       toCoordinateFromView:myMapView]; 

CLLocationCoordinate2D topRightCoord = 
    [myMapView convertPoint:CGPointMake(myMapView.frame.size.width, 0) 
       toCoordinateFromView:myMapView]; 
+0

OK,我會試着問其他辦法...我怎麼能得到這個圈子的邊界座標:HTTP://img39.imageshack .us/img39/6346/screenshot20111127at113.png覆蓋??? – pprochazka72

+0

查看修訂後的答案。 – Anna

0

在你的Web表單的截圖,你因爲你測量的線(中心到MinLAT/MinLONG)是對角線並延伸到圓半徑之外,所以得到不同的距離。

如果關注@ AnnaKarenina的回答,那麼您可以通過將每個CLLocationCoordinate2D轉換爲CLLocation來獲得以米爲單位的距離。

這裏是你如何得到英里半徑:

CLLocation * centerLeftLocation = [[CLLocation alloc] 
            initWithLatitude:centerCoord.latitude 
            longitude:topLeftCoord.longitude]; 
CLLocation * centerLocation = [[CLLocation alloc] 
           initWithLatitude:centerCoord.latitude 
           longitude:centerCoord.longitude]; 

CLLocationDistance distanceInMeters = [centerLeftLocation distanceFromLocation:centerLocation]; 

float distanceInMiles = distanceInMeters/1609.344f; 

[centerLeftLocation release]; 
[centerLocation release];