gpt4 book ai didi

ios - map View :rendererForOverlay delegate method never called when addOverlay is called on MKMapView

转载 作者:搜寻专家 更新时间:2023-11-01 07:26:49 25 4
gpt4 key购买 nike

我创建了一个 MapViewController 类,其中的场景在 Storyboard中有一个 MKMapView。 MKMapView 有一个 mapViewMapViewController 的导出,委托(delegate)在 Storyboard中设置为 MapViewControllerMapViewController 是一个 MKMapViewDelegate,我实现了可选的委托(delegate)方法 mapView:rendererForOverlay 和其他一些方法。

viewDidAppear中,我调用了addMapOverlay,定义为:

  func addMapOverlay() {
let overlayRect = MKMapRectMake(
44.97077, -93.2864435,
0.1, 0.1)

let coordinate = CLLocationCoordinate2DMake(44.97087, -93.2865435)

let overlay = MapOverlay(coordinate: coordinate, rect: overlayRect)

print("adding overlay with level...")
mapView.delegate = self;
mapView.addOverlay(overlay, level: .AboveRoads)
}

尽管在 Storyboard 中设置了委托(delegate),但我还是尝试看看如果我也将其设置在这里是否会有帮助。 (没有区别。)

我这样实现委托(delegate)方法:

extension MapViewController: MKMapViewDelegate {

func mapViewWillStartRenderingMap(mapView: MKMapView) {
print("mapViewWillStartRenderingMap...")
}

func mapView(mapView: MKMapView, didAddOverlayViews overlayViews: [AnyObject]) {
print("mapView:didAddOverlayViews...")
}

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay)
-> MKOverlayRenderer {
print("mapView:rendererForOverlay")

if overlay is MapOverlay {
let image = UIImage(named: "main_map_overlay")
let overlayView = MapOverlayView(overlay: overlay, overlayImage: image!)

return overlayView
}

return MKPolylineRenderer()
}

}

我应该在 mapView 中看到带有坐标的 UIImage,但我没有。问题是 mapView:rendererForOverlay 永远不会被调用。缺少什么?我唯一的输出是:

adding overlay with level...
mapViewWillStartRenderingMap...

最佳答案

好吧,我找到了问题的答案。事实证明,在这种情况下,CLLocationCoordinate2DMake(44.97087, -93.2865435) 不是制作坐标的正确方法。无论哪种方式都是错误的,因此,在没有任何有用信息的情况下,添加叠加层会悄无声息地失败。我做了以下操作,现在没问题了:

let parkTopLeft = MKMapPointForCoordinate(CLLocationCoordinate2D(
latitude: 44.972189,
longitude: -93.287307))
let parkBottomRight = MKMapPointForCoordinate(CLLocationCoordinate2D(
latitude: 44.967810,
longitude: -93.28186))

let overlayRect = MKMapRectMake(
parkTopLeft.x,
parkTopLeft.y,
parkBottomRight.x - parkTopLeft.x,
parkBottomRight.y - parkTopLeft.y)

let coordinate = CLLocationCoordinate2DMake(
overlayRect.origin.x + overlayRect.size.width / 2,
overlayRect.origin.y + overlayRect.size.height / 2)

let overlay = MapOverlay(coordinate: coordinate, rect: overlayRect)

mapView.addOverlay(overlay, level: .AboveRoads)

关于ios - map View :rendererForOverlay delegate method never called when addOverlay is called on MKMapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35542284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com