2010-10-31 100 views
1

我需要使用實時刷新跟蹤用戶當前位置 我有兩個解決方案的功能。MKMapView註釋位置更新問題

  • (無效)的LocationManager:(CLLocationManager *)經理didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

IFDEF Variant_1

if(m_currentLocation) 
    [m_Map removeAnnotation:m_currentLocation]; 
else 
    m_currentLocation = [MKPlacemark alloc]; 
[m_currentLocation initWithCoordinate:newLocation.coordinate addressDictionary:nil]; 
[m_Map addAnnotation:m_currentLocation]; 
[m_Map setCenterCoordinate:m_currentLocation.coordinate animated:YES]; 

別的// Variant_2

if(m_currentLocation == nil) 
{ 
m_currentLocation = [MKPlacemark alloc]; 
[m_currentLocation initWithCoordinate:newLocation.coordinate addressDictionary:nil]; 
[m_Map addAnnotation:m_currentLocation]; 

}else 
{ 
[m_currentLocation initWithCoordinate:newLocation.coordinate addressDictionary:nil]; 
//[m_currentLocation setCoordinate:newLocation.coordinate]; 
} 
[m_Map setCenterCoordinate:m_currentLocation.coordinate animated:YES]; 

endif

}

Variant_1做工不錯,但是當你快速移動的位置在地圖上閃爍唱歌。 Variant_2不閃爍但不移動位置但是移動地圖。 問題在哪裏? Thant你。

回答

2

在Variant_1中,它可能會閃爍,因爲您正在執行removeAnnotation,然後是addAnnotation而不是僅修改現有註釋的座標。

在Variant_2中,initWithCoordinate 返回帶有這些座標的新MKPlacemark對象。它不更新您調用方法的對象的屬性。

如果您改用setCoordinate行,會發生什麼?

另一個問題是爲什麼不使用MKMapView的內置功能來顯示當前的用戶位置?一開始就做m_Map.showsUserLocation = YES;。無論如何你都不需要CLLocationManager來獲取用戶的當前位置。

我想你仍然需要到中心在地圖上使用的地圖視圖的委託方法之一用戶的當前位置:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES]; 
}