MKMapView不会添加任何注释

发布于 2024-12-02 09:24:18 字数 1396 浏览 0 评论 0原文

我有一个 MKMapView,其委托在 Interface Builder 中设置,并且是其视图控制器的保留属性(也连接在 Interface Builder 中)。是的,我确信它们已正确连接。我试图向mapView添加注释,但是mapView:viewForAnnotation:没有被调用,并且在使用[self.map addAnnotation:annot]添加所有注释后,打印mapView的注释数组的计数产生零。我不知道是什么原因造成的,因为我的 MapPerson 对象使用只读坐标属性实现 MKAnnotation 协议。这是我针对该业务的代码...

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

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

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@end

及其实现...

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

我试图在服务类的委托方法中创建这些注释,该服务类从服务器获取坐标到包含 mapView 的视图控制器。我尝试将该代码移动到一个辅助方法,我确保在主线程上调用该方法,但没有任何反应。

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

我用Google搜索了SO,并查看了几个不同的教程,这些教程与我的代码没有什么不同,除了名称之外。我在mapView:viewForAnnotation:中放置了一个断点,并且仅针对用户的位置注释调用一次。 mapPeople 的数量为 5。他们的坐标设置正确。有什么想法吗?

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, readonly) 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深巷少女 2024-12-09 09:24:18

后端使用 MongoDB,它颠倒了纬度/经度。因此,注释被添加到地图坐标平面上不存在的位置。我觉得自己像个白痴,没有早点注意到这一点。感谢您的回答!

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!

你げ笑在眉眼 2024-12-09 09:24:18

您没有收到有关 MapPerson 的警告吗?我相信文档中说,如果您想实现 MKAnnotation,您还需要提供标题和副标题。

您能否提供填充 mapPeople 的代码?

编辑:重新阅读文档,它说只有当您计划创建可选注释时才需要标题和副标题。

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.

手心的温暖 2024-12-09 09:24:18

您是否检查过您的 ppeople 是否为 nil

Have you checked if your p or people is nil?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文