栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Swift中创建MKCircle?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Swift中创建MKCircle?

将展示有关如何使用xpre 8.3.3的swift 3在地图视图上创建圆形叠加层的分步方法

在您的主故事板文件中,将地图工具包视图拖到故事板的场景(视图)上,并为其创建出口,在这里我创建了mapView。另外,您还希望在地图上长按时动态创建叠加层,因此将“长按手势识别器”从对象库拖动到mapView上,然后为其创建动作方法,这里我为该对象创建了addRegion()。

  1. 为CLLocationManager类创建一个全局常量,以便可以在每个函数中对其进行访问。在您的viewDidLoad方法中,添加一些代码以获取用户授权。

        import UIKit  import MapKitclass ViewController: UIViewController {    @IBOutlet var mapView: MKMapView!    let locationManager = CLLocationManager()override func viewDidLoad() {          super.viewDidLoad()        locationManager.delegate = self        locationManager.requestAlwaysAuthorization()        locationManager.requestWhenInUseAuthorization()        locationManager.desiredAccuracy = kCLLocationAccuracyBest        locationManager.startUpdatingLocation()    }
  2. 只要在长按手势识别器操作方法addRegion()中执行长按手势识别器,就添加用于生成圆形区域的代码。

        @IBAction func addRegion(_ sender: Any) {          print("addregion pressed")          guard let longPress = sender as? UILongPressGestureRecognizer else {return}        let touchLocation = longPress.location(in: mapView)        let coordinates = mapView.convert(touchLocation, toCoordinateFrom: mapView)        let region = CLCircularRegion(center: coordinates, radius: 5000, identifier: "geofence")        mapView.removeOverlays(mapView.overlays)        locationManager.startMonitoring(for: region)        let circle = MKCircle(center: coordinates, radius: region.radius)        mapView.add(circle)    }

除非您在地图上渲染圆圈,否则您仍然不会在地图上实际看到圆圈。为此,您需要实现mapviewdelegate的委托。

        func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {}
  1. 为了使代码看起来更简洁,您可以在类结束的最后大括号后创建扩展。一个扩展包含CLLocationManagerDelegate的代码,另一个包含MKMapViewDelegate的代码。
        extension ViewController: CLLocationManagerDelegate {    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {        locationManager.stopUpdatingLocation()        mapView.showsUserLocation = true    }}

您应该在委托方法中调用locationManager.stopUpdatingLocation(),以免电池电量耗尽。

        extension ViewController: MKMapViewDelegate { func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {     guard let circelOverLay = overlay as? MKCircle else {return MKOverlayRenderer()}     let circleRenderer = MKCircleRenderer(circle: circelOverLay)     circleRenderer.strokeColor = .blue     circleRenderer.fillColor = .blue     circleRenderer.alpha = 0.2     return circleRenderer }        }

在这里,我们在屏幕上绘制实际的圆。

最终代码应如下所示。

import UIKitimport MapKitclass ViewController: UIViewController {    @IBOutlet var mapView: MKMapView!    let locationManager = CLLocationManager()    override func viewDidLoad() {        super.viewDidLoad()        locationManager.delegate = self        locationManager.requestAlwaysAuthorization()        locationManager.requestWhenInUseAuthorization()        locationManager.desiredAccuracy = kCLLocationAccuracyBest        locationManager.startUpdatingLocation()    }    // MARK: Long Press Gesture Recognizer Action Method    @IBAction func addRegion(_ sender: Any) {        print("addregion pressed")        guard let longPress = sender as? UILongPressGestureRecognizer else {return}        let touchLocation = longPress.location(in: mapView)        let coordinates = mapView.convert(touchLocation, toCoordinateFrom: mapView)        let region = CLCircularRegion(center: coordinates, radius: 5000, identifier: "geofence")        mapView.removeOverlays(mapView.overlays)        locationManager.startMonitoring(for: region)        let circle = MKCircle(center: coordinates, radius: region.radius)        mapView.add(circle)    }}extension ViewController: CLLocationManagerDelegate {    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {        locationManager.stopUpdatingLocation()        mapView.showsUserLocation = true    }}extension ViewController: MKMapViewDelegate {    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {        guard let circelOverLay = overlay as? MKCircle else {return MKOverlayRenderer()}        let circleRenderer = MKCircleRenderer(circle: circelOverLay)        circleRenderer.strokeColor = .blue        circleRenderer.fillColor = .blue        circleRenderer.alpha = 0.2        return circleRenderer    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/382067.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号