用户位置的简单MKCircle实现

时间:2018-12-01 18:43:52

标签: ios swift mapkit mkcircle

IOS和SWIFT的真正新手。尝试在当前位置上覆盖一个圆圈。以下代码生成地图视图,在用户位置放置一个蓝点,但不绘制覆盖圆。.请帮助。

//
//  ViewController.swift
//  userLocation
//

//

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    //Map outlet
    @IBOutlet weak var map: MKMapView!
    @IBOutlet weak var zoomFactorStepper: UIStepper!

    let manager = CLLocationManager()
    var zoomFactor = 0.01

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations[0]
        let span:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: zoomFactor, longitudeDelta: zoomFactor)
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region:MKCoordinateRegion = MKCoordinateRegion(center: myLocation, span: span)
        map.setRegion(region, animated: true)
        self.map.showsUserLocation = true
        map.removeOverlays(map.overlays)
        let circle = MKCircle(center: location.coordinate, radius: 100)
        map.addOverlay(circle)

    }

此功能完成主要工作。

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()

    }

    @IBAction func zoomFactorStepperAction(_ sender: Any) {
        zoomFactor = zoomFactorStepper.value * 0.01
        //map.setNeedsDisplay()
    }

    func map(_ map: 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
    }

}

1 个答案:

答案 0 :(得分:0)

这个消失了,一旦我添加了这个圆圈,圆环就按预期下降了。

map.delegate =自我