我正在尝试使用 Swift Xcode 中的 MapKit 向我的地图添加叠加圆圈。但是,叠加层没有出现
I am trying to add an overlay circle to my map using MapKit in Swift Xcode. But, the overlay is not showing up
我发现有很多人在做和我一样的事情,他们的工作也很成功。当它运行时,它会打印“addregion pressed”但它实际上并没有画圆圈
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet var map: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
map.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
@IBAction func addRegion(_ sender: Any) {
print("addregion pressed")
guard let longPress = sender as? UILongPressGestureRecognizer else {return}
let touchLocation = longPress.location(in: map)
let coordinates = map.convert(touchLocation, toCoordinateFrom: map)
let region = CLCircularRegion(center: coordinates, radius: 5000, identifier: "geofence")
map.removeOverlays(map.overlays)
locationManager.startMonitoring(for: region)
let circle = MKCircle(center: coordinates, radius: region.radius)
map.addOverlay(circle)
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationManager.stopUpdatingLocation()
map.showsUserLocation = true
}
}
extension ViewController: MKMapViewDelegate {
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
}
}
想通了。刚需map.delegate = self
我发现有很多人在做和我一样的事情,他们的工作也很成功。当它运行时,它会打印“addregion pressed”但它实际上并没有画圆圈
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet var map: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
map.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
@IBAction func addRegion(_ sender: Any) {
print("addregion pressed")
guard let longPress = sender as? UILongPressGestureRecognizer else {return}
let touchLocation = longPress.location(in: map)
let coordinates = map.convert(touchLocation, toCoordinateFrom: map)
let region = CLCircularRegion(center: coordinates, radius: 5000, identifier: "geofence")
map.removeOverlays(map.overlays)
locationManager.startMonitoring(for: region)
let circle = MKCircle(center: coordinates, radius: region.radius)
map.addOverlay(circle)
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationManager.stopUpdatingLocation()
map.showsUserLocation = true
}
}
extension ViewController: MKMapViewDelegate {
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
}
}
想通了。刚需map.delegate = self