我正在尝试显示图像而不是源位置的默认红色针脚和目标位置的另一个图像。
- (void) sourceMarker:(MKPlacemark * ) homeLoc {
MKAnnotationView *point = [[MKAnnotationView alloc] init];
MKPointAnnotation *ml = point.annotation;
ml.coordinate = homeLoc.coordinate;
ml.title = @"Home";
point.image = [UIImage imageNamed:@"home.png"];
point.canShowCallout = YES;
[self.mapView addAnnotation:ml];
}
答案 0 :(得分:1)
正如Apple doc Annotating Maps所建议的那样。一个人必须创建自定义注释类,请参考以下代码。
MyAnnotation.h
@interface MyAnnotation : NSObject<MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSString *type;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *type;
@end
MyAnnotation.m
@implementation MyAnnotation
@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
@synthesize type;
@end
yourViewControll.m - 你在哪里展示地图。
创建注释并设置type属性
MyAnnotation *Ann = [[MyAnnotation alloc] init];
offAnn.coordinate = offLocation.coordinate;
offAnn.title = @"My Office";
offAnn.type = @"Office";
[_mapView removeAnnotation: offAnn];
[_mapView addAnnotation: offAnn];
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"viewForAnnotation Called");
if ([annotation isKindOfClass:[MyAnnotation class]])
{
MyAnnotation *myAnn=(MyAnnotation *)annotation;
MKAnnotationView *pinView = (MKAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"MyAnnotation"];
if (!pinView)
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyAnnotation"];
//pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.calloutOffset = CGPointMake(0, 4);
} else {
pinView.annotation = annotation;
}
if([myAnn.type isEqual: @"Office"]) {
pinView.image = [UIImage imageNamed:@"office.png"];
}
return pinView;
}
return nil;
}
答案 1 :(得分:0)
(MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *annotationViewID;
MKAnnotationView *annotationView;
annotationViewID = @"annotationViewID";
annotationView = (MKAnnotationView *)[self dequeueReusableAnnotationViewWithIdentifier:annotationViewID];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewID] ;
}
annotationView.image = [UIImage imageNamed:@"patientpin_l.png"];
// Add annotation image here
annotationView.annotation = annotation;
return annotationView;
}