0

I tried to add a simple pin on map but somehow it didn't show up no matter what kind of pin I added. Is there something wrong with my code?

My code:

#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;
}

I followed the simple solution for adding a pin on map here: Quickly adding single pin to MKMapView? But it still didn't show up.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Skylar
  • 127
  • 8

1 Answers1

0

I was able to get your code to work by erasing the references to the custom annotation. So, there is a conflict that is (possibly) resolved by just using the MKPointAnnotation class.
The code below worked in 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];
ericl
  • 321
  • 1
  • 7