【问题标题】:Swift MapKit Overlays will not Show on MapSwift MapKit 叠加层不会显示在地图上
【发布时间】:2019-12-30 20:42:41
【问题描述】:

我正在尝试在地图上显示任何叠加层。我遵循了许多教程,但没有任何效果。地图将始终没有错误地加载,但叠加层将永远不会显示在地图上。

我正在尝试让 this (一个倒置的圆圈)显示在地图上。

import Foundation
import UIKit
import MapKit

class MKInvertedCircleOverlayRenderer: MKOverlayRenderer {

var fillColor: UIColor = UIColor.red
var strokeColor: UIColor = UIColor.blue
var lineWidth: CGFloat = 3
var circle: MKCircle

init(circle: MKCircle) {
    self.circle = circle
    super.init(overlay: circle)
}

override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
    let path = UIBezierPath(rect: rect(for: MKMapRect.world))

    let excludePath: UIBezierPath = UIBezierPath(roundedRect: CGRect(x: circle.coordinate.latitude, y: circle.coordinate.longitude,
                                                                     width: circle.boundingMapRect.size.width,
                                                                     height: circle.boundingMapRect.size.height),
                                                 cornerRadius: CGFloat(circle.boundingMapRect.size.width))

    context.setFillColor(fillColor.cgColor)

    path.append(excludePath)
    context.addPath(path.cgPath)
    context.fillPath(using: .evenOdd)

    context.addPath(excludePath.cgPath)
    context.setLineWidth(9 / zoomScale)
    context.setStrokeColor(strokeColor.cgColor)
    context.strokePath()

    //line showing circle radius
    let lineBeginPoint = CGPoint(x: excludePath.bounds.midX, y: excludePath.bounds.midY)
    let lineEndPoint = CGPoint(x: excludePath.bounds.maxX, y: excludePath.bounds.midY)
    let linePath: UIBezierPath = UIBezierPath()
    linePath.move(to: lineBeginPoint)
    linePath.addLine(to: lineEndPoint)

    context.addPath(linePath.cgPath)
    context.setLineWidth(6/zoomScale)
    context.setStrokeColor(UIColor.black.cgColor)
    context.setLineDash(phase: 1, lengths: [20 / zoomScale, 10 / zoomScale])
    context.strokePath()

    // circle at the end of the line above
    let circleSize: CGFloat = 30/zoomScale
    let circleRect = CGRect(origin: CGPoint(x: lineEndPoint.x - (circleSize/2), y: lineEndPoint.y - (circleSize/2)),
                            size: CGSize(width: circleSize, height: circleSize))

    let circlePath: UIBezierPath =
        UIBezierPath(roundedRect: circleRect, cornerRadius: circleSize)

    context.addPath(circlePath.cgPath)
    context.setFillColor(UIColor.black.cgColor)
    context.fillPath()
}
}
import Foundation
import MapKit
import UIKit

class MKInvertedCircle : NSObject, MKOverlay {

    var coordinate: CLLocationCoordinate2D

    var boundingMapRect: MKMapRect {
        return MKMapRect.world
    }

    init(center coord: CLLocationCoordinate2D) {
        self.coordinate = coord
    }
}

视图控制器

import MapKit
import UIKit
import Foundation





class ViewController: UIViewController, MKMapViewDelegate {

    private let locationManager = CLLocationManager()
    private var currentCoordinate: CLLocationCoordinate2D?



    @IBOutlet weak var mapView: MKMapView!


  func addCircleOverlay() {
    let overlay = MKInvertedCircle(center: CLLocationCoordinate2D())
    mapView.addOverlay(overlay)
  }

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        if overlay is MKInvertedCircle {
        let circleRenderer = MKInvertedCircleOverlayRenderer(circle: MKCircle())

        return circleRenderer
    }
    else {
       return MKOverlayRenderer(overlay: overlay)
    }
    }


        // if screen loads ask for location permissions
    override func viewDidLoad() {
        super.viewDidLoad()
        configureLocationServices()
        mapView.delegate = self;

        // Do any additional setup after loading the view.
    }


    //how to ask for location permissions
    private func configureLocationServices() {

        locationManager.delegate = self
        //check location permissions
        let status = CLLocationManager.authorizationStatus()
        //if location permissions not set, ask
        if status == .notDetermined {
            locationManager.requestAlwaysAuthorization()
        } else if status == .authorizedAlways || status == .authorizedWhenInUse {
            //begin tracking location
            beginLocationUpdates(locationManager: locationManager)

        }
    }
    //begin tracking location
    private func beginLocationUpdates(locationManager: CLLocationManager) {
        //show blue dot
        mapView.showsUserLocation = true
        //track location to best of phones ability
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        //start updating location
        locationManager.startUpdatingLocation()

    }
    //zoom to location
    private func zoomToLatestLocation(with coordinate: CLLocationCoordinate2D) {
        //set zoom level
        let zoomRegion = MKCoordinateRegion.init(center: coordinate, latitudinalMeters: 10000, longitudinalMeters: 10000)
        //tells map to zoom animated
        mapView.setRegion(zoomRegion, animated: true)

    }

}

extension ViewController: CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("Did get Latest Location")

        guard let latestLocation = locations.first else {return }

        if currentCoordinate == nil{
            zoomToLatestLocation(with: latestLocation.coordinate)
        }

        currentCoordinate = latestLocation.coordinate

    }
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
            print("The Status Changed")
        if status == .authorizedAlways || status == .authorizedWhenInUse {
            beginLocationUpdates(locationManager:  manager)
        }
    }




}

但我会接受任何覆盖工作,作为朝着正确方向迈出的一步。 有谁知道将在地图上显示叠加层的最新代码? 谢谢。

【问题讨论】:

  • 欢迎来到 SO!请分享一些您尝试过的代码并解释到底出了什么问题。 “没有任何效果”不是很有指示性。如果没有更多信息,很难提供帮助。另请参阅How to Ask
  • 嘿@koen,抱歉没有具体说明。我已经用我拥有的所有代码更新了帖子。我已经按照其他堆栈溢出问题尝试解决此问题,例如this onethis blog,但是即使在复制并粘贴这些代码后,地图上也没有显示任何内容。我是 swift 新手,所以我不太了解我正在使用的代码。

标签: swift xcode mapkit


【解决方案1】:

错误似乎是你的委托人的

mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer

您正在初始化一个新的 MKCircle,而不是在此函数中使用叠加层。

试试这个

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if let circle = overlay as? MKCircle {
        return MKInvertedCircleOverlayRenderer(circle: circle)
    } else {
        return MKOverlayRenderer(overlay: overlay)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多