如何使用 mapkit 和 swift 在设定的位置覆盖一个圆圈

Posted

技术标签:

【中文标题】如何使用 mapkit 和 swift 在设定的位置覆盖一个圆圈【英文标题】:How do I overlay a circle at a set location using mapkit and swift 【发布时间】:2016-10-30 05:27:31 【问题描述】:

我无法弄清楚如何在与用户位置不同的所需位置显示透明圆形或矩形。我是 mapkit 的初学者,提前致谢。

class FirstViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate


@IBOutlet weak var mapView: MKMapView!

let locationManager = CLLocationManager()

override func viewDidLoad()

    super.viewDidLoad()

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.startUpdatingLocation()
    self.mapView.showsUserLocation = true



func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

    let location = locations.last
    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
    self.mapView.setRegion(region, animated: true)
    self.locationManager.stopUpdatingLocation()//


func locationManager(manager: CLLocationManager, didFailWithError error: NSError)

    print("Errors: " + error.localizedDescription)



【问题讨论】:

【参考方案1】:

已更新以支持 Swift 4.2。提供了评论来解释我所做的几个选择。

import UIKit
import MapKit

class Map: UIViewController 
    var mapView = MKMapView()

    func setup() 
        // Assign delegate here. Can call the circle at startup,
        // or at a later point using the method below.
        // Includes <# #> syntax to simplify code completion.
        mapView.delegate = self
        showCircle(coordinate: <#CLLocationCoordinate2D#>,
                   radius: <#CLLocationDistance#>)
    

    // Radius is measured in meters
    func showCircle(coordinate: CLLocationCoordinate2D,
                    radius: CLLocationDistance) 
        let circle = MKCircle(center: coordinate,
                              radius: radius)
        mapView.addOverlay(circle)
    


extension Map: MKMapViewDelegate 
    func mapView(_ mapView: MKMapView,
                 rendererFor overlay: MKOverlay) -> MKOverlayRenderer 
        // If you want to include other shapes, then this check is needed.
        // If you only want circles, then remove it.
        if let circleOverlay = overlay as? MKCircle 
            let circleRenderer = MKCircleRenderer(overlay: circleOverlay)
            circleRenderer.fillColor = .black
            circleRenderer.alpha = 0.1

            return circleRenderer
        

        // If other shapes are required, handle them here
        return <#Another overlay type#>
    

【讨论】:

另一种选择是使用该圆圈的图像进行自定义注释。我只是在你想要那种功能的条件下把它扔出去 如果我将 MKOverlayRenderer 的 nil 设置为不可接受,我应该在 rendererForOverlay 方法中返回什么【参考方案2】:

我已经使用以下实现了以下

import UIKit
import MapKit


class MapVC: UIViewController,CLLocationManagerDelegate 
var locationManager = CLLocationManager()
@IBOutlet weak var mapView : MKMapView!
override func viewDidLoad() 
    super.viewDidLoad()


    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    // Check for Location Services
    if (CLLocationManager.locationServicesEnabled()) 
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
    

    //Zoom to user location
    if let userLocation = locationManager.location?.coordinate 
        let viewRegion = MKCoordinateRegion(center: userLocation, latitudinalMeters: 200, longitudinalMeters: 200)
        let region = CLCircularRegion(center: userLocation, radius: 5000, identifier: "geofence")
        mapView.addOverlay(MKCircle(center: userLocation, radius: 200))
        mapView.setRegion(viewRegion, animated: false)
    

    DispatchQueue.main.async 
        self.locationManager.startUpdatingLocation()
    


extension MapVC : MKMapViewDelegate
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer 

    var circleRenderer = MKCircleRenderer()
    if let overlay = overlay as? MKCircle 
        circleRenderer = MKCircleRenderer(circle: overlay)
        circleRenderer.fillColor = UIColor.green
        circleRenderer.strokeColor = .black
        circleRenderer.alpha = 0.5

    
    return circleRenderer


我的输出:

【讨论】:

以上是关于如何使用 mapkit 和 swift 在设定的位置覆盖一个圆圈的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Mapkit (Swift) 去用户位置

如何以编程方式在 Mapkit 中选择特定注释 - Swift

Swift:SwiftUI中MapKit的使用体验

如何更新显示的 ETA 和距离值? Swift 4 MKDirections MapKit

在给定坐标附近,在 Swift、MapKit 和核心定位中打印 hello

Swift 中的 MapKit