首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift MapKit叠加不会在地图上显示

问Swift MapKit叠加不会在地图上显示
EN

Stack Overflow用户
提问于 2019-12-31 04:42:41
回答 1查看 288关注 0票数 0

我正在尝试显示地图上的任何叠加层。我遵循了很多很多教程,但都不起作用。地图将始终加载,没有错误,但覆盖将永远不会显示在地图上。

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

代码语言:javascript
复制
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()
}
}
代码语言:javascript
复制
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
    }
}

视图控制器

代码语言:javascript
复制
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)
        }
    }




}

但我会满足于任何覆盖工作,作为在正确的方向上的一步。有没有人知道在地图上显示叠加的最新代码?谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-01-07 22:36:41

错误似乎是您的委派造成的

代码语言:javascript
复制
mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer

您将初始化一个新的MKCircle,而不是在此函数中使用覆盖。

试试这个吧

代码语言:javascript
复制
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if let circle = overlay as? MKCircle {
        return MKInvertedCircleOverlayRenderer(circle: circle)
    } else {
        return MKOverlayRenderer(overlay: overlay)
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59536640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档