首页 > 解决方案 > Mapkit 上的图钉未显示

问题描述

我试图在地图上添加一个简单的图钉,但不知何故,无论我添加哪种图钉,它都没有显示出来。我的代码有问题吗?

我的代码:

#define METERS_MILE 1609.344
#define METERS_FEET 3.28084

#import "ViewController.h"
#import "../Jetfire_websocket/JFRWebSocket.h"
#import "MyCustomAnnotation.h"

@interface ViewController ()
<CLLocationManagerDelegate>
@end

@interface ViewController ()<JFRWebSocketDelegate>

@property(nonatomic, strong)JFRWebSocket *socket;

@end

@interface ViewController () <MKMapViewDelegate>
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.mapView.delegate = self;

    // Location services.
    [[self mapView] setShowsUserLocation:YES];

    self.locationManager = [[CLLocationManager alloc] init];

    [[self locationManager] setDelegate:self];


    // Need to setup the location manager with permission in iOS versions later than ios8.
    if ([[self locationManager] respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [[self locationManager] requestWhenInUseAuthorization];
    }

    [[self locationManager] setDesiredAccuracy:kCLLocationAccuracyBest];
    [[self locationManager] startUpdatingLocation];

    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:newannotation];

    // 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];

    // Geocoding
    self.searchResult.text = @"default text";
    [self.searchButton setTitle:@"Search Location Name" forState:(UIControlStateNormal)];


    // websocket connection initialization.
    self.socket = [[JFRWebSocket alloc] initWithURL:[NSURL URLWithString:@"ws://localhost:9002"] protocols:@[@"chat",@"superchat"]];
    self.socket.delegate = self;
    [self.socket connect];

    // Firebase initialization.
    self.ref = [[FIRDatabase database] reference];
}

-(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;
//        //return nil;
//    }
    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;
}

我在这里遵循了在地图上添加图钉的简单解决方案:快速将单个图钉添加到 MKMapView? 但它仍然没有出现。

标签: iosobjective-cmapkit

解决方案


通过删除对自定义注释的引用,我能够让您的代码正常工作。因此,仅使用MKPointAnnotation该类就(可能)解决了冲突。
下面的代码在viewDidLoad

CLLocationCoordinate2D aCoord = CLLocationCoordinate2DMake(37.785834, -122.406417);
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = aCoord;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = {aCoord, span};
[self.mapView setRegion:region];


CLLocationCoordinate2D bCoord = CLLocationCoordinate2DMake(37.330713, -121.894348);
MKPointAnnotation *anotherPoint = [[MKPointAnnotation alloc] init];
anotherPoint.coordinate = bCoord;
anotherPoint.title = @"Title?";
anotherPoint.subtitle = @"Subtitle!!!";

[self.mapView addAnnotation:point];
[self.mapView addAnnotation:anotherPoint];

推荐阅读