ios - mapView viewForOverlay 从未调用过

标签 ios objective-c ios7 mkmapview mkoverlay

所以我想显示我的应用程序的用户在 MKMapView 上行走的位置,我使用以下代码收集数据:

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    
    // calc. distance walked
    CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
    self.totalMetters += meters;
    [[self labelDistance] setText:[self formatDistanceIntoString:self.totalMetters]];
    
   //  create another annotation
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    annotation.coordinate = newLocation.coordinate;
            
    // Also add to our map so we can remove old values later
    [self.locations addObject:annotation];
    
    // Remove values if the array is too big
    while (self.locations.count > 100)
    {
        annotation = [self.locations objectAtIndex:0];
        [self.locations removeObjectAtIndex:0];
        
        // Also remove from the map
        [self.map removeAnnotation:annotation];
    }
完成后,我调用我的绘图方法:
[self drawRoute];
其中包含以下内容:
- (void)drawRoute {
    
    NSLog(@"drawRoute");
    NSInteger pointsCount = self.locations.count;
    
    CLLocationCoordinate2D pointsToUse[pointsCount];
    
    for(int i = 0; i < pointsCount; i++) {
        MKPointAnnotation *an = [self.locations objectAtIndex:i];
        pointsToUse[i] = CLLocationCoordinate2DMake(an.coordinate.latitude,an.coordinate.latitude);
    }
    
    MKPolyline *myPolyline = [MKPolyline polylineWithCoordinates:pointsToUse count:pointsCount];
    
    [self.map addOverlay:myPolyline];
}
最后我的mapView代表 :
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
    NSLog(@"route");
    if ([overlay isKindOfClass:MKPolyline.class]) {
        MKPolylineView *lineView = [[MKPolylineView alloc] initWithOverlay:overlay];
        lineView.strokeColor = [UIColor greenColor];
        
        return lineView;
    }
    return nil;
}
显然我的 Controller 是 MKMapView委托(delegate)符合
@interface NewWalkViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate> 
mapViewStoryboard链接到 Controller (导出和委托(delegate))
我使用“自行车道”调试工具,有输出:

2014-01-25 20:27:30.132 The walking dog[2963:70b] new location : 37.330435

2014-01-25 20:27:30.133 The walking dog[2963:70b] drawRoute


正如我所见,从未调用过绘制叠加层的方法,而且我不知道如何修复它。

最佳答案

主要问题是在 drawRoute ,这条线正在通过latitude两个参数到CLLocationCoordinate2DMake :

pointsToUse[i] = CLLocationCoordinate2DMake
                     (an.coordinate.latitude,an.coordinate.latitude);

这导致在世界的不同地方绘制线,而不是实际 an.coordinate 的位置。是。例如,如果 an.coordinate是 37,-122(旧金山附近的某个地方),而是在 37,37(土耳其南部的某个地方)画线。

由于您实际上并未将 map 定位在错误的位置(您正在寻找“正确”位置的线),viewForOverlay永远不会被调用,因为 map 只在覆盖层可能可见时才会调用它。

将该行更改为:
pointsToUse[i] = CLLocationCoordinate2DMake
                     (an.coordinate.latitude,an.coordinate.longitude);

或者简单地说:
pointsToUse[i] = an.coordinate;

正如 James Frost 在评论中提到的那样,从 iOS 7 开始,您应该使用 rendererForOverlay而不是 viewForOverlay虽然 map View 仍会调用 viewForOverlay在 iOS 7 中如果 rendererForOverlay尚未实现。虽然这不会阻止您的叠加层在当前情况下显示,但您应该实现新的委托(delegate)方法以及旧的委托(delegate)方法(如果 iOS 7 应用程序也将在 iOS 6 或更早版本上运行)。

另一个重要但不相关的问题是您不必要地创建了多个重叠的叠加层。在 drawRoute ,由于您正在创建的叠加层包含所有位置,因此您应该在添加新叠加层之前删除所有现有的叠加层。否则, map 以位置 0 到 1 的覆盖图、位置 0 到位置 2 的覆盖图、位置 0 到位置 3 的覆盖图等结束。之前的覆盖图不明显可见,因为它们具有相同的颜色和线条宽度。在 addOverlay 之前行,放:
[self.map removeOverlays:self.map.overlays];

关于ios - mapView viewForOverlay 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21355274/

相关文章:

iphone - 如何在 iOS 6 中为 UIActivityViewController 设置收件人?

ios - UIModalTransitionStylePartialCurl 在 iOS 8 上消失

ios - 使用 commitEditingStyle 时删除按钮仍然显示(cf gif)

ios - 如何在 xamarin.ios 中将图库中的图像字节数组显示为图像?

ios - LongpressGesture 结束不会关闭 View

ios - UICollectionView 类引用委托(delegate)属性

iOS 7 UIViewController(带导航栏)不插入 UIScrollView

objective-c - 以编程方式创建 UITextFields

ios - 导航时保持计时器在另一个页面上运行

objective-c - 使应用程序启动并保持横向模式