【发布时间】:2011-07-04 03:37:59
【问题描述】:
所以我把头发拉到这个上面。我一直在追踪我的应用程序中的一个错误。最初,我试图从数据库中加载长/纬度坐标并执行 for 循环,将每个注释添加到地图视图中。这看起来很简单,但是由于某种原因,当我尝试在循环中添加注释时,它最后只会显示 1 个注释。
所以,使用下面的代码,我决定尝试简单地向 MKMapView 添加两个注释,以确保我可以首先做到这一点,但它不起作用!
[NewAnnotation setLongitude:-104.6200448];
[NewAnnotation setLongitude:50.4908343];
NewAnnotation *newAnnotation = [[NewAnnotation alloc] init];
[self.mapAnnotations insertObject:newAnnotation atIndex:0];
[newAnnotation release];
[CelebsAnnotation setLongitude:-90.6200448];
[CelebsAnnotation setLatitude:51.4908343];
CelebsAnnotation *celebsAnnotation = [[CelebsAnnotation alloc] init];
[self.mapAnnotations insertObject:celebsAnnotation atIndex:1];
[celebsAnnotation release];
[self.mapView addAnnotations:mapAnnotations];
如果我只将其中一个添加到 mapAnnotations 数组中,注释会显示在它们的正确位置(如果我只添加 CelebsAnnotation,我必须将索引调整为 0),但是当我尝试添加两者时,它们会显示在地图上的同一位置!?关于为什么会发生这种情况的任何想法?我很困惑和沮丧..
【问题讨论】:
-
您似乎将
setLongitude和setLatitude作为类方法。他们在做什么?你是如何定义init方法的? -
我没有定义 init 方法,实际上现在我查看了我的自定义注释类,我在 .h 中没有坐标属性,所以我很困惑为什么它甚至甚至使用一个注释。我将进一步研究如何正确实现自定义注释类,因为这可能是我的问题。
-
-(void)setLongitude:(double)lon { Clongitude = lon; }
-
Clongitude是什么?它是静态变量吗?如何实现longitude实例方法? -
Clongtitude 是一个双精度值,我不确定您要询问的是哪种经度实例方法。我认为只需要@property(非原子,只读)CLLocationCoordinate2D 坐标;我还实现了这个函数来设置坐标 - (CLLocationCoordinate2D)coordinate; { CLLocationCoordinate2D 坐标; theCoordinate.latitude = 纬度; theCoordinate.longitude = 经度;返回坐标; }
标签: iphone objective-c mkmapview