問題描述
我有一個地圖視圖,其中注釋的坐標不斷更新,但是當我使用 setCoordinate 時,注釋不會移動.如何刷新注釋以反映它們的坐標?
I have a mapview where the annotation's coordinates are constantly being updated but when I use setCoordinate, the annotation does not move. How do I refresh the annotations to reflect their coordinates?
- (void)updateUnits {
PFQuery *query = [PFQuery queryWithClassName:@"devices"];
[query whereKeyExists:@"location"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *row in objects) {
PFGeoPoint *geoPoint = [row objectForKey:@"location"];
CLLocationCoordinate2D coord = { geoPoint.latitude, geoPoint.longitude };
for (id<MKAnnotation> ann in mapView.annotations) {
if ([ann.title isEqualToString:[row objectForKey:@"deviceid"]]) {
[ann setCoordinate:coord];
NSLog(@"latitude is: %f" , coord.latitude);
NSLog(@"Is called");
break;
}
else {
//[self.mapView removeAnnotation:ann];
}
}
}
}
else {
}
}];
}
推薦答案
更新(反映解決方案):
Updated (to reflect the solution):
擁有自己的自定義注釋并實現 setCoordinate 和/或合成坐標可能會導致問題.
Having your own custom annotations and implementing setCoordinate and/or synthesizing coordinate may cause issues.
來源:http://developer.apple.com/庫/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html
以前的解決方案:
您可以簡單地刪除所有注釋,然后重新添加它們.
You can simply remove all of the annotations and then re-add them.
[mapView removeAnnotations:[mapView.annotations]];
[mapView addAnnotations:(NSArray *)];
或刪除它們并一一重新添加:
or remove them and re-add them one by one:
for (id<MKAnnotation> annotation in mapView.annotations)
{
[mapView removeAnnotation:annotation];
// change coordinates etc
[mapView addAnnotation:annotation];
}
這篇關于iOS在mapview上刷新注釋的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!