2012-04-21 13 views
1

我有下面的代碼中心對用戶的當前位置,當按鈕被按下的定位:的iOS的MKMapView setCenterCoordinate不兌現的確切座標

- (void)locate 
{ 
    if (self.curLocation) { 
     [self.googleMapView setCenterCoordinate:self.curLocation.coordinate animated:YES]; 
     NSLog(@"setting center (%f,%f), but got (%f,%f)", self.curLocation.coordinate.latitude, self.curLocation.coordinate.longitude, self.googleMapView.centerCoordinate.latitude, self.googleMapView.centerCoordinate.longitude); 
    } 
} 

中的NSLog的輸出「設置中心(37.785834, -122.406417),但得到(37.785826,-122.406406)「

這意味着setCenterCoordinate移動到近似座標而不是指示的座標。這是非常不方便的,因爲當我實現縮放級別時,放大相同的位置然後放大(因爲setRegion也改變了中心座標)將遠離舊的位置。這是違反直覺的,Android平臺API不會表現出這種不規律的行爲。

有人可以解釋爲什麼setCenterCoordinate沒有去確切的座標,如果可能的話,任何方式來確保縮小out-and-in將有相同的中心。謝謝。

+0

您是否正在更改MKMapView的框架? – 2012-04-21 12:43:54

+0

我不這麼認爲......我剝離了所有其他與mapview相關的代碼,這應該是唯一剩下的東西。 – 2012-04-21 12:59:31

回答

0

setCenterCoordinate中的最後一個參數是「動畫」。您將其設置爲YES,因此可能需要一秒左右才能到達正確的位置。由於這發生在另一個線程上,NSLog行在地圖到達目標座標之前發生。在閱讀中心之前等一兩秒鐘,或通過屏幕上的按鈕激活您的NSLog語句,以確保在地圖停止移動後執行此操作。

+0

嘗試了你的建議。仍然給我(37.785826,-122.406406)。 – 2012-04-22 07:37:28

+0

我查看了這兩個座標,它們看起來距離不到2米。鑑於手機內置GPS的準確性,您可能會發現該移動與您的應用無關。 – Craig 2012-04-23 00:06:55

+0

嗨克雷格,GPS與我的問題無關。我明確要求該中心設置爲某一點(37.785834,-122.406417),但不受尊敬。我必須對抗這種行爲的原因是,當setCenterCoordinate漂移時,在街道X上縮小(setRegion),然後放大回來會將我置於不同的街道Y上,儘管我從未改變過中心座標。 – 2012-04-23 01:20:36

4

您可以將原始的「良好」中心座標保存在ivar中,然後在每次放大時使用該座標。這樣它總是應該返回到它開始的位置。我假設你正在放大按鈕或調用方法。

中心座標似乎不可靠。放大時它變得更加可靠,但它似乎永遠不會完全符合您的要求。即使使用由regionThatFits:返回的中心座標也不會給出完全匹配的結果。您可以使用-mapView:regionDidChangeAnimated:來實驗:

- (void)viewDidAppear:(BOOL)animated 
{ 
    MKCoordinateSpan span = MKCoordinateSpanMake(0.0001f, 0.0001f); 
    CLLocationCoordinate2D coordinate = {35.67106,139.764909}; 
    MKCoordinateRegion region = {coordinate, span}; 

    MKCoordinateRegion regionThatFits = [self.mapView regionThatFits:region]; 
    NSLog(@"Fit Region %f %f", regionThatFits.center.latitude, regionThatFits.center.longitude); 

    [self.mapView setRegion:regionThatFits animated:YES]; 
// [self.mapView setRegion:region animated:YES]; 
// [self.mapView setCenterCoordinate:coordinate animated:NO]; 
} 

- (void)mapView:(MKMapView *)myMapView regionDidChangeAnimated:(BOOL)animated 
{ 
    NSLog(@"Center: %f %f", myMapView.region.center.latitude,myMapView.region.center.longitude); 
} 
+0

所以我應該保存(37.785826,-122.406406),座標mapview實際上是居中,而不是我的座標,以確保回到它開始的位置? – 2012-04-23 22:22:08

+0

是的,除了像'CLLocationCoordinate2D centerCoordinate'之類的東西,並且每次地圖縮放時,都將其設置爲中心座標(結合您需要縮放地圖的任何區域)。 – 2012-04-23 23:54:16