我正在尋找一些幫助完成基於當前位置註釋和我設置的註釋在MKMapView上設置區域的一些代碼。將MKMapView區域設置爲以兩個註釋爲中心
我想計算兩者之間的距離並設置兩者之間的中心,然後縮小以便兩者都可以看到。它似乎在我的模擬器中工作正常,但不幸的是userLocation.coordinate固定到Apple總部。當我在設備上測試時,我看到了奇怪的行爲。如果兩個註釋在相同的緯度上有些水平,通常會縮小並設置合適的區域,但如果垂直距離較大,則不會正確縮小。
我用代碼中發現here,和編輯一點點適合我的需要:
是困惑我CLLocationCoordinate2D southWest = mapView.userLocation.coordinate;
CLLocationCoordinate2D northEast = southWest;
southWest.latitude = MIN(southWest.latitude, annotation.coordinate.latitude);
southWest.longitude = MIN(southWest.longitude, annotation.coordinate.longitude);
northEast.latitude = MAX(northEast.latitude, annotation.coordinate.latitude);
northEast.longitude = MAX(northEast.longitude, annotation.coordinate.longitude);
CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
// This is a diag distance (if you wanted tighter you could do NE-NW or NE-SE)
CLLocationDistance meters = [locSouthWest distanceFromLocation:locNorthEast];
MKCoordinateRegion region;
region.center.latitude = (southWest.latitude + northEast.latitude)/2.0;
region.center.longitude = (southWest.longitude + northEast.longitude)/2.0;
region.span.latitudeDelta = meters/111319.5;
region.span.longitudeDelta = 0.0;
MKCoordinateRegion savedRegion = [mapView regionThatFits:region];
[mapView setRegion:savedRegion animated:YES];
[locSouthWest release];
[locNorthEast release];
一件事是,他說:northEast = southWest
......
在此先感謝誰誰得到了一些幫助,輸入:)
完美!德爾塔是我能看到的問題。非常感謝! – runmad 2010-10-29 14:47:34
不幸的是,這個頁面似乎沒有迴應。有沒有人有頁面的緩存版本?謝謝! – jowie 2011-08-21 22:17:34
@jowie我能夠使用回機器找到頁面的緩存版本。我希望這有幫助。 http://web.archive.org/web/20100612181520/http://codisllc。com/blog/zoom-mkmapview-to-fit-annotations – avelis 2013-04-04 21:00:52