选择具体注释
我循环遍历 SQLite 结果并向地图添加注释
while(sqlite3_step(statement) == SQLITE_ROW) {
double latitude = sqlite3_column_double(statement, 0);
double longitude = sqlite3_column_double(statement, 1);
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
[point setCoordinate:(location)];
[point setTitle:venuename];
[point setSubtitle:@"hello"];
[mapView addAnnotation:point];
[point release];
}
效果很好。现在,假设我想选择第三个注释,我
id<MKAnnotation> myAnnotation = [mapView.annotations objectAtIndex:2];
[mapView selectAnnotation:myAnnotation animated:NO];
确实这样做了,但是,注释似乎是按随机顺序添加到地图中的,而不是根据我的循环顺序。因此“objectAtIndex:2”始终是不同的注释,而不是第二个数据库结果。
有没有办法在将注释添加到地图之前为注释提供一些唯一的 ID,然后我可以用它来选择它们?
I'm looping through SQLite results and add annotations to a map
while(sqlite3_step(statement) == SQLITE_ROW) {
double latitude = sqlite3_column_double(statement, 0);
double longitude = sqlite3_column_double(statement, 1);
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
[point setCoordinate:(location)];
[point setTitle:venuename];
[point setSubtitle:@"hello"];
[mapView addAnnotation:point];
[point release];
}
Works great. Now, lets say I want to Select the 3nd Annotation, I do
id<MKAnnotation> myAnnotation = [mapView.annotations objectAtIndex:2];
[mapView selectAnnotation:myAnnotation animated:NO];
However, it seems annotations are added to the map in random order, and not according to the order of my loop. So the "objectAtIndex:2" is always a different annotation, and not the second Database result.
Is there a way how I can give an annotation some unique ID before I add them to the map, which I can then use to select them ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注释可以是您想要的任何自定义对象,只要它符合 MKAnnotation 协议 (参见此处)
因此,如果您需要注释有一个 ID,您可以在自定义注释对象的代码中为其声明一个属性。
An annotation can be any custom object that you want, provide it conforms to the MKAnnotation Protocol (see here)
So if you need your annotations to have an ID, you can declare a property for it in the code for the custom annotation object.