我正在尝试将我的图钉颜色更改为紫色,但当我这样做时我失去了标题。代码是:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBarHidden=YES;
//init the location manager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager requestWhenInUseAuthorization];
self.mapView.showsUserLocation=YES;
self.userGeoPoint=self.message[@"userLocation"];
self.pinView = [[MKPointAnnotation alloc] init];
self.pinView.title=self.message[@"fromUser"];
self.coord = CLLocationCoordinate2DMake(self.userGeoPoint.latitude, self.userGeoPoint.longitude);
self.pinView.coordinate=self.coord;
//use a slight delay for more dramtic zooming
[self performSelector:@selector(addPin) withObject:nil afterDelay:0.5];
}
-(void)addPin{
[self.mapView addAnnotation:self.pinView];
[self.mapView selectAnnotation:self.pinView animated:YES];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coord, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotationPoint
{
if ([annotationPoint isKindOfClass:[MKUserLocation class]])//keep the user as default
return nil;
static NSString *annotationIdentifier = @"annotationIdentifier";
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotationPoint reuseIdentifier:annotationIdentifier];
pinView.pinColor = MKPinAnnotationColorPurple;
//now we can throw an image in there
return pinView;
}
我尝试为 MKPinAnnotation 设置标题属性,但没有一个。无论如何我可以解决这个问题吗?
最佳答案
在viewForAnnotation
中,需要将canShowCallout
设置为YES
(默认为NO
):
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.canShowCallout = YES;
几个不相关的点:
- 看起来您有一个名为
pinView
的属性,类型为MKPointAnnotation
,您正在使用它在viewDidLoad
中创建注释对象。然后在viewForAnnotation
中,您有一个名为pinView
的MKPinAnnotationView
类型的局部变量。这里虽然没有命名上的冲突,但是却造成并暗示了一些概念上的困惑。MKPointAnnotation
是一个注释 model 类,而MKPinAnnotationView
是一个注释 view 类——它们是完全不同的东西。例如,将注释属性命名为pin
或userAnnotation
会更好。 viewForAnnotation
中的注释://now we can throw an image in there
似乎暗示此时您可以在 View 中设置自定义图像。不建议在
MKPinAnnotationView
中设置自定义图像,因为该类旨在以三种颜色之一显示默认图钉图像。要使用自定义图像,请改为创建普通的MKAnnotationView
。
关于ios - 尝试更改 MKPointAnnotation 颜色但丢失标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29315590/