![](/img/trans.png)
[英]How to toggle replace MKMapView's user location blue dot to an image?
[英]how to change user current location blue dot into custom image in MKMapView
我需要使用一些自定义图像来更改用户的当前位置蓝点注释。 如何实现呢?
这是我的示例代码:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if (annotation == mapView.userLocation){
static NSString* AnnotationIdentifier = @"user";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
customPinView.image = [UIImage imageNamed:@"user.png"];
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
return customPinView;
}
但是,当我启动该应用程序时,它显示红色标记。
将此代码放在viewForAnnotation:方法中
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString* AnnotationIdentifier = @"Annotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView) {
MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
if (annotation == mapView.userLocation){
customPinView.image = [UIImage imageNamed:@"myPin.png"];
}
else{
customPinView.image = [UIImage imageNamed:@"myPin1.png"];
}
customPinView.canShowCallout = YES;
customPinView.animatesDrop = NO;
return customPinView;
} else {
pinView.annotation = annotation;
}
return pinView;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.