开发者

MKMapView won't add any annotations

开发者 https://www.devze.com 2023-04-01 19:24 出处:网络
I have an MKMapView whose delegate is set in Interface Builder and which is a retained property (hooked up in Interface Builder as well) of its view controller. Yes, I am sure they are hooked up prope

I have an MKMapView whose delegate is set in Interface Builder and which is a retained property (hooked up in Interface Builder as well) of its view controller. Yes, I am sure they are hooked up properly. I am trying to add annotations to the mapView, but mapView: viewForAnnotation: is not called, and after adding all of the annotations with [self.map addAnnotation:annot], printing the count of the mapView's annotations array yields zero. I have no idea what is causing this because my MapPerson object implements the MKAnnotation protocol with a readonly coordinate property. Here is my code for that business...

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapPerson : NSObject <MKAnnotation> {
  CLLocationCoordinate2D _coo;
}

@property (nonatomic, readon开发者_运维知识库ly) CLLocationCoordinate2D coordinate;

@end

And its implementation...

#import "MapPerson.h"

@implementation MapPerson

@synthesize coordinate=_coo;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (id)initWithCoordinate:(CLLocationCoordinate2D)loc {
  self = [super init];
  if (self) {
    _coo = loc;
  }
  return self;
}

I am trying to create these annotations in a delegate method from a service class which fetches coordinates from a server to the view controller containing the mapView. I have tried moving that code to a helper method which I make sure to call on the main thread, but nothing happens.

- (void)refreshMap {
  for (MapPerson *p in mapPeople) {
    [self.map addAnnotation:p];
  }
  NSLog(@"Mapview has %i annotations.", self.map.annotations.count);
  // is always 1 for the user's location
}

I have Googled and SO's and looked at several different tutorials which do not differ from my code except in names. I put a breakpoint in mapView: viewForAnnotation:, and it is only called once for the user's location annotation. The count of mapPeople is 5. Their coordinates are being set correctly. Any ideas?


The backend is using MongoDB which had reversed latitude/longitude. Thus, the annotations were added to a location that doesn't exist on the coordinate plane of the map. I feel like an idiot for not noticing this sooner. Thanks for the answers!


Are you not getting warnings regarding MapPerson? I believe it says in the documentation that if you want to implement MKAnnotation you need to provide a title and subtitle as well.

Can you possibly provide the code where you populate mapPeople?

EDIT: Re-read documentation, it says title and subtitle are only expected of you if you plan on creating selectable annotations.


Have you checked if your p or people is nil?

0

精彩评论

暂无评论...
验证码 换一张
取 消