在地图视图上添加自定义的锚点
你想在地图上添加一些依你自己的定义的图片的形式来显示锚点,而不是 iOS SDK 自 带的那种形式。
通过 UIImage 引入一个你自定义的图片,然后把它赋值给 MKAnnotationView 的 image
这个属性上去,以一个锚点的形式返回到你的地图视图上
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKAnnotationView *result = nil;
if ([annotation isKindOfClass:[MyAnnotation class]] == NO)
{
return result;
}
if ([mapView isEqual:self.myMapView] == NO)
{
return result;
}
MyAnnotation *senderAnnotation = (MyAnnotation *) annotation;
NSString *pinReusableIdentifier = [MyAnnotation reusableIdentifierforPinColor:senderAnnotation.pinColor];
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinReusableIdentifier];
if (annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:senderAnnotation reuseIdentifier:pinReusableIdentifier];
[annotationView setCanShowCallout:YES];
}
UIImage *pinImage = [UIImage imageNamed:@"Default.png"];
if (pinImage != nil)
{
annotationView.image = pinImage;
}
result = annotationView;
return result;
}
MKMapView 实例对象的协议类必须要实现 MKMapViewDelegate 这个协议,并且要实 现 mapView:viewForAnnotation 这个方法。这个方法将会返回一个 MKAnnotationView 的实 例对象,任何这个类的子类都有一个叫做 image 的属性,我们可以通过给这个属性设置我们 自定义的图片从而能够在地图上展示
评论