如何使用 MapKit 和 swift 绘制多边形叠加层
How to draw a polygon overlay using MapKit and swift
我正在尝试使用以下代码在地图上绘制多边形,但由于某种原因它不起作用。我这里哪里出错了?
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let initialLocation = CLLocation(latitude: 49.140838, longitude: -123.127886)
centerMapOnLocation(initialLocation)
addBoundry()
}
func addBoundry()
{
var points=[CLLocationCoordinate2DMake(49.142677, -123.135139),CLLocationCoordinate2DMake(49.142730, -123.125794),CLLocationCoordinate2DMake(49.140874, -123.125805),CLLocationCoordinate2DMake(49.140885, -123.135214)]
let polygon = MKPolygon(coordinates: &points, count: points.count)
mapView.addOverlay(polygon)
}
let regionRadius: CLLocationDistance = 1000
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolygon {
let polygonView = MKPolygonRenderer(overlay: overlay)
polygonView.strokeColor = UIColor.magentaColor()
return polygonView
}
return nil
}
您似乎已将 mapView(_:renderFor:)
(以前称为 rendererForOverlay
)实现为全局函数。该方法必须在 ViewController
class 定义中,或者更好的是,为了使我们的代码井井有条,在 MKMapViewDelegate
扩展名中:
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
...
}
}
此外,请确保您已将视图控制器定义为地图视图的 delegate
。在 IB 中可能最容易做到这一点,但您也可以在 viewDidLoad
:
中做到这一点
mapView.delegate = self
我正在尝试使用以下代码在地图上绘制多边形,但由于某种原因它不起作用。我这里哪里出错了?
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let initialLocation = CLLocation(latitude: 49.140838, longitude: -123.127886)
centerMapOnLocation(initialLocation)
addBoundry()
}
func addBoundry()
{
var points=[CLLocationCoordinate2DMake(49.142677, -123.135139),CLLocationCoordinate2DMake(49.142730, -123.125794),CLLocationCoordinate2DMake(49.140874, -123.125805),CLLocationCoordinate2DMake(49.140885, -123.135214)]
let polygon = MKPolygon(coordinates: &points, count: points.count)
mapView.addOverlay(polygon)
}
let regionRadius: CLLocationDistance = 1000
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolygon {
let polygonView = MKPolygonRenderer(overlay: overlay)
polygonView.strokeColor = UIColor.magentaColor()
return polygonView
}
return nil
}
您似乎已将 mapView(_:renderFor:)
(以前称为 rendererForOverlay
)实现为全局函数。该方法必须在 ViewController
class 定义中,或者更好的是,为了使我们的代码井井有条,在 MKMapViewDelegate
扩展名中:
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
...
}
}
此外,请确保您已将视图控制器定义为地图视图的 delegate
。在 IB 中可能最容易做到这一点,但您也可以在 viewDidLoad
:
mapView.delegate = self