[英]iOS SDK Mapview Annotation callout redraw error
I'm trying to add the distance from the user's position to a selected annotation's subtitle in a mapview.我正在尝试将用户 position 的距离添加到地图视图中选定注释的副标题。 The mechanics of it are working, but the actual callout gets messed up the first time it's displayed.
它的机制是有效的,但实际的标注在第一次显示时会被弄乱。 There appears to be a redraw problem.
似乎存在重绘问题。
Subsequent taps on the pin show the correct layout.随后点击图钉会显示正确的布局。
Here's the relevant code:这是相关代码:
// called when selecting annotations // 选择注解时调用
- (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)]];
} I've tried calling [view setNeedsDisplay], but to no avail.我试过调用 [view setNeedsDisplay],但无济于事。
Thanks in advance for your help.在此先感谢您的帮助。
Here's the solution I finally came up with.这是我最终想出的解决方案。 It seems to work.
它似乎工作。
I edited out the duplicate code from the didSelectAnnotationView method, above, and came up with:我从上面的 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];
}
}
You should set your subtitle in another place than didSelectAnnotationView.您应该将副标题设置在 didSelectAnnotationView 之外的其他位置。 Actually all annotationViews should have their title and subtitle set before they are returned by the mapView:viewForAnnotation: method.
实际上,在 mapView:viewForAnnotation: 方法返回之前,所有的 annotationViews 都应该设置好它们的标题和副标题。
The fact that you set a long subtitle certainly explains that the callout is not the right size.您设置长字幕的事实肯定说明标注的大小不正确。 The size must be calculated before the didSelectAnnotationView is called.
必须在调用 didSelectAnnotationView 之前计算大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.