引脚未在Mapkit上显示-obj-c

时间:2018-07-23 07:02:01

标签: ios objective-c mapkit

我是ios开发人员的新手。我尝试使用mapkit在地图上添加图钉注释。但是,在尝试了最基本的方法之后,它还是没有显示出来,要么是简单的图钉,要么是自定义的注解视图。没有错误消息,只是图钉不会显示在地图上。 这是我的代码:

- (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”中添加这些简单代码以外的其他方式一样?非常感谢!

2 个答案:

答案 0 :(得分:0)

管理注释视图

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

基本上,当您添加注释时,将调用此委托方法,并且您必须在此委托方法中为该注释创建视图

- (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];
}

答案 1 :(得分:0)

尝试以下代码:

something

不要忘记将- (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