Pin未显示在Mapkit-obj-c上



我是ios开发的新手。我尝试使用mapkit在地图上添加pin注释。然而,在尝试了最基本的方法后,它仍然没有出现,无论是简单的pin,还是自定义的annotationview。没有错误消息,只是大头针不会出现在地图上。这是我的代码:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
NSLog(@"Custom Annotations start.");
// Custom Annotations.
CLLocationCoordinate2D bryanParkCoordinates = CLLocationCoordinate2DMake(37.785834, -122.406417);
MyCustomAnnotation *bryanParkAnnotation = [[MyCustomAnnotation alloc]initWithTitle:@"Bryant Park" Location:bryanParkCoordinates];
[self.mapView addAnnotation:bryanParkAnnotation];
// Simple pin.
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:bryanParkCoordinates];
[annotation setTitle:@"Title"];
[self.mapView addAnnotation:annotation];
// Another try.
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(37.330713, -121.894348);
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = {coord, span};
MKPointAnnotation *newannotation = [[MKPointAnnotation alloc] init];
[newannotation setCoordinate:coord];
[self.mapView setRegion:region];
[self.mapView addAnnotation:annotation];
// On your location.
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = bryanParkCoordinates;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
[self.mapView addAnnotation:point];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MyCustomAnnotation class]])
{
MyCustomAnnotation *myLocation = (MyCustomAnnotation *)annotation;
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MyCustomAnnotation"];
if(annotationView == nil)
annotationView = myLocation.annotationView;
else
annotationView.annotation = annotation;
return annotationView;
}
else{
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"DETAILPIN_ID"];
[pinView setAnimatesDrop:YES];
[pinView setCanShowCallout:NO];
return pinView;
}
}

如果我只想在地图上画一个简单的别针,只需添加

// Simple pin.
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:bryanParkCoordinates];
[annotation setTitle:@"Title"];
[self.mapView addAnnotation:annotation];

应该足够吗?还有其他我可能错过的东西吗?喜欢在"viewDidLoad"中添加其他委托或添加那些简单代码以外的其他内容吗?非常感谢!

管理注释视图

您应该实现这个委托方法,并为注释创建一个视图并返回它

基本上,当你添加一个注释时,这个委托方法会被调用,你必须在这个委托方法中为该注释创建视图

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
NSLog(@"Custom Annotations start.");
// Custom Annotations.
CLLocationCoordinate2D bryanParkCoordinates = CLLocationCoordinate2DMake(37.785834, -122.406417);
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(37.330713, -121.894348);
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = {coord, span};
[self.mapView setRegion:region];
[self.mapView addAnnotation:annotation];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = bryanParkCoordinates;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
[self.mapView addAnnotation:point];
}

尝试以下代码:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if (annotation == mapView.userLocation) return nil;
static NSString* Identifier = @"PinAnnotationIdentifier";
MKPinAnnotationView* pinView;
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:Identifier];
if (pinView == nil) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:Identifier];
pinView.canShowCallout = YES;
return pinView;
}
pinView.annotation = annotation;
return pinView;
}

不要忘记将<MKMapViewDelegate>添加到您的@interface部分。

@interface YourViewController () <MKMapViewDelegate>

最新更新