【问题标题】:MKMapView - rendererForOverlay not calledMKMapView - 未调用 rendererForOverlay
【发布时间】:2013-09-20 00:12:37
【问题描述】:

我最近开始学习objectiveC并开始在iOS6中开发一个应用程序。

现在,我正在尝试将其转换为 iOS7 并面临 MKMap 的问题。

在 iOS6 中,我使用的是 viewForOverlay。

在 iOS7 中,我将其更改为 renderForOverlay。但是,我的应用程序没有调用 mapView:rendererForOverlay。下面是我的代码。感谢您的帮助。

- (void) drawPolyline:(NSArray *)locations
{
    [mapView setDelegate:self];
    ...
    ...

    self.polyline = [MKPolyline polylineWithCoordinates:locationCoordinate2DArray count:numberOfLocations];
    free(locationCoordinate2DArray);
    [mapView addOverlay:self.polyline];
    [mapView setNeedsDisplay];
}

- (MKOverlayRenderer*)mapView:(MKMapView*)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineRenderer* lineView = [[MKPolylineRenderer alloc] initWithPolyline:self.polyline];
    lineView.strokeColor = [UIColor blueColor];
    lineView.lineWidth = 7;
    return lineView;
}

【问题讨论】:

  • 程序没有调用 mapView:rendererForOverlay

标签: ios mkmapview mkpolyline


【解决方案1】:

我假设您确实通过 @interface 语句在头文件中声明了 MKMapViewDelegate 委托:

但是,您是否在 viewDidLoad(或您认为合适的地方)方法中分配了委托?

self.mapView.delegate = self;

【讨论】:

  • 是的,我在头文件中有委托... @interface MapTrackerViewController : UIViewController { MKMapView *mapView; ... } 我在 drawPolyline 方法中分配了委托([mapView setDelegate:self];)。请在我的原始帖子中找到代码。它在iOS6中工作。在 iOS7 中,他们删除了 viewforOverlay 方法。现在,我正在尝试实现 rendererForOverlay。但它没有被调用。
【解决方案2】:

正如其他人所说,请确保在添加之前设置您的 delegate。使用addOverlay:level: 方法,因为addOverlay: 将被弃用(根据标题中的注释)。

我的问题是愚蠢的。我错误地为多边形的点切换了纬度和经度。如果它们仍然没有出现,请务必仔细检查。

您也可以尝试在多边形上记录 pointCount 以确保它们设置正确。

相关,这是在 Swift 中的操作方法:

// Get your coordinates from somewhere as an [CLLocationCoordinate2D] array
// If you already have them, make a local mutable copy
var coordinates = [CLLocationCoordinate2D]()

// Create your polygon
let polygon = MKPolygon(coordinates: &coordinates, count: coordinates.count)

希望这可以为您节省一些时间!

【讨论】:

  • 谢谢!这里同样愚蠢的问题。我在切换经纬度...!
  • 天哪!我做了同样的事情。呃,我生命中只有 2 个小时。感谢您的帮助。
【解决方案3】:

好的,我遇到了同样的问题,终于找到了案例。 我们必须使用[MKMapView addOverlay: level:] 而不是[MKMapView addOverlay:]。 它触发rendererForOverlay 而不是委托的viewForOverlay。 希望这对 iOS 7 爱好者有所帮助!

【讨论】:

  • 好吧,我还有另一个案例,我重写了 MKMapView,它中继了所有的委托方法,例如 mapView: viewForOverlay: 而​​且我不得不添加另一个 mapView 的中继:rendererForOverlay:
【解决方案4】:

如果locationCoordinate2DArray,mapView:rendererForOverlay 中只有一个点,则不会被调用。

【讨论】:

    【解决方案5】:

    我刚刚用这种方法完成了我的实验,我发现只有 nib-file 放置了 MKMapView@property (weak, nonatomic) IBOutlet MKMapView *mapView; 解决了这个问题。这里还有checklist

    【讨论】:

      【解决方案6】:

      对我来说,解决方案包括两个步骤:

      1. 在添加覆盖之前先设置委托。一旦添加了叠加层,就会调用委托方法。
      2. 检查委托。愚蠢的是,我的代码中有一个名为 delegate 的本地属性。在 iOS 6 中,这无关紧要,在 iOS 7 中确实如此。一旦我删除了那个流浪的委托,rendererForOverlay: 就被调用了。

      【讨论】:

        【解决方案7】:

        对于像我这样在 IOS 13 上苦苦挣扎的人来说只是一个提示。看起来,因为 IOS 13 忽略了以下内容:

        // self is a MKTileOverlayRenderer
        // the code is called, if a method has produced a tile for a given MKMapRect
        
        // tell the system that we are ready
        DispatchQueue.main.async(execute: {
        
            // invalidate the mapRect
            self.setNeedsDisplay(mapRect)
        
        })
        

        此代码在 IOS 10 - IOS 12 中运行良好,但现在您必须将 zoomScale 添加到 setNeedsDisplay(),否则 IOS 13 似乎会忽略它...花了我 4 个小时来整理它;- )

        self.setNeedsDisplay(mapRect, zoomScale: zoomScale)
        

        【讨论】:

          猜你喜欢
          • 2016-05-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多