是的,您可以为用户位置提供自定义视图。
不幸的是,实施起来比应有的要困难,因为即使viewForAnnotation 委托方法的文档 http://developer.apple.com/library/ios/#DOCUMENTATION/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html#//apple_ref/doc/uid/TP40008204-CH3-SW11声称如果注释类是,您可以只提供自己的视图MKUserLocation
,自定义视图不会继续随用户位置移动。事实上,当返回自定义视图时MKUserLocation
,地图视图完全停止更新用户位置(地图视图的didUpdateUserLocation
委托方法不再触发)。我相信这是一个错误。
解决方法是使用CLLocationManager
和自定义注释...
确保showsUserLocation
is NO
或在地图视图上取消选中。
声明属性CLLocationManager
以及使用自定义类的自定义注释,该自定义类实现了MKAnnotation
协议(或者你可以只使用通用的MKPointAnnotation
class).
In viewDidLoad
或其他一些适当的地方,创建CLLocationManager
,设置其delegate
并打电话startUpdatingLocation
.
In the 地点经理的didUpdateToLocation委托方法 http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html#//apple_ref/doc/uid/TP40007124-CH3-SW3 (not地图视图的didUpdateUserLocation
委托方法),创建或更新您的自定义注释:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (myUserLocAnnot == nil)
{
self.myUserLocAnnot = [[[MyUserLocClass alloc] init] autorelease];
//remove the autorelease if using ARC
myUserLocAnnot.title = @"You are here";
myUserLocAnnot.coordinate = newLocation.coordinate;
[mapView addAnnotation:myUserLocAnnot];
}
else
{
myUserLocAnnot.coordinate = newLocation.coordinate;
}
}
最后,在地图视图中viewForAnnotation
委托方法中,如果注释是您的自定义用户位置注释,您将返回自定义注释视图。