2010-10-31 88 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]; 
}