如何使用mapkit和swift在指定位置叠加圆圈

问题描述 投票:3回答:1

我无法弄清楚如何在用户位置唯一的所需位置显示透明圆形或矩形。我是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)
}

}
ios swift location mapkit overlay
1个回答
6
投票

这已更新为支持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#>
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.