我正在尝试添加从用户位置到地图视图中选定注释的副标题的距离。它的机制是有效的,但实际标注在第一次显示时就搞砸了。似乎有重绘问题。
随后点击引脚显示正确的布局。
以下是相关代码:
// 选择注解时调用
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
MKPointAnnotation *selectedAnnotation = view.annotation;
//attempt to add distance on annotation
CLLocation *pointALocation = [[CLLocation alloc]
initWithLatitude:selectedAnnotation.coordinate.latitude
longitude:selectedAnnotation.coordinate.longitude];
float distanceMeters = [pointALocation distanceFromLocation:locationManager.location];
//for sending info to detail
myPinTitle = selectedAnnotation.title;
[selectedAnnotation setSubtitle:[NSString stringWithFormat:@"%.2f miles away", (distanceMeters / 1609.344)]];
我试过调用 [view setNeedsDisplay],但无济于事。
在此先感谢您的帮助。
有效的解决方案
这是我最终想出的解决方案。它似乎工作。
我从上面的 didSelectAnnotationView 方法中编辑了重复的代码,并想出了:
//called when user location changes
- (void)updatePinsDistance
{
for (int x=0; x< [[mapView annotations]count]; x++) {
MKPointAnnotation *thisPin =[[mapView annotations] objectAtIndex:x];
//attempt to add distance on annotation
CLLocation *pointALocation = [[CLLocation alloc]
initWithLatitude:thisPin.coordinate.latitude
longitude:thisPin.coordinate.longitude];
float distanceMeters = [pointALocation distanceFromLocation:locationManager.location];
NSString *distanceMiles = [NSString stringWithFormat:@"%.2f miles from you",
(distanceMeters / 1609.344)];
[thisPin setSubtitle:distanceMiles];
}
}