Swift MapKit - 创建多边形
Swift MapKit - create a polygon
我想在我的地图上创建一个多边形来显示边界。但是当我 运行 我的应用程序没有出现。我可以看到地图,但看不到多边形 我的错误是什么?谢谢。
import UIKit
import MapKit
import CoreLocation
class MapScreen : UIViewController {
@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
mapView.delegate = self as! MKMapViewDelegate
// center the location on the screen
var location = CLLocationCoordinate2DMake(41.308792, -72.928641)
var span = MKCoordinateSpanMake(0.01, 0.01)
var region = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: true)
//calling the method
addBoundry()
}
func addBoundry(){ //creation of a polygon
var points = [CLLocationCoordinate2DMake(41.311674, -72.925506),
CLLocationCoordinate2DMake(41.307308, -72.928694),
CLLocationCoordinate2DMake(41.307108, -72.928324),
CLLocationCoordinate2DMake(41.307892, -72.930285),
CLLocationCoordinate2DMake(41.307892, -72.931223),
CLLocationCoordinate2DMake(41.307227, -72.932494),
CLLocationCoordinate2DMake(41.308452, -72.931663),
CLLocationCoordinate2DMake(41.308730, -72.932773),
CLLocationCoordinate2DMake(41.308496, -72.931614),
CLLocationCoordinate2DMake(41.308496, -72.931614),
CLLocationCoordinate2DMake(41.311288, -72.931630),
CLLocationCoordinate2DMake(41.311659, -72.930945),
CLLocationCoordinate2DMake(41.312893, -72.932153),
CLLocationCoordinate2DMake(41.313433, -72.930542),
CLLocationCoordinate2DMake(41.313324, -72.929963),
CLLocationCoordinate2DMake(41.312758, -72.929027),
CLLocationCoordinate2DMake(41.312373, -72.927167),
CLLocationCoordinate2DMake(41.311674, -72.925506)]
let polygon = MKPolygon(coordinates: &points, count: points.count)
mapView.add(polygon)
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolygon {
let polygonView = MKPolygonRenderer(overlay: overlay)
polygonView.fillColor = UIColor(red: 0, green: 0.847, blue: 1, alpha: 0.25)
return polygonView
}
return nil
}
}
您 mapView(_:rendererFor:)
的签名不正确。在 Swift 3 中,它将是:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
...
}
如果您正式声明遵守 MKMapViewDelegate
等协议,它通常会警告您这些事情。例如,最佳实践是在 class 扩展中而不是在 class 定义本身中这样做:
extension MapScreen: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
...
}
}
顺便说一句,如果这样做,您不仅会收到有关协议一致性的警告,而且在设置 delegate
:
时也不需要转换它
mapView.delegate = self
我想在我的地图上创建一个多边形来显示边界。但是当我 运行 我的应用程序没有出现。我可以看到地图,但看不到多边形 我的错误是什么?谢谢。
import UIKit
import MapKit
import CoreLocation
class MapScreen : UIViewController {
@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
mapView.delegate = self as! MKMapViewDelegate
// center the location on the screen
var location = CLLocationCoordinate2DMake(41.308792, -72.928641)
var span = MKCoordinateSpanMake(0.01, 0.01)
var region = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: true)
//calling the method
addBoundry()
}
func addBoundry(){ //creation of a polygon
var points = [CLLocationCoordinate2DMake(41.311674, -72.925506),
CLLocationCoordinate2DMake(41.307308, -72.928694),
CLLocationCoordinate2DMake(41.307108, -72.928324),
CLLocationCoordinate2DMake(41.307892, -72.930285),
CLLocationCoordinate2DMake(41.307892, -72.931223),
CLLocationCoordinate2DMake(41.307227, -72.932494),
CLLocationCoordinate2DMake(41.308452, -72.931663),
CLLocationCoordinate2DMake(41.308730, -72.932773),
CLLocationCoordinate2DMake(41.308496, -72.931614),
CLLocationCoordinate2DMake(41.308496, -72.931614),
CLLocationCoordinate2DMake(41.311288, -72.931630),
CLLocationCoordinate2DMake(41.311659, -72.930945),
CLLocationCoordinate2DMake(41.312893, -72.932153),
CLLocationCoordinate2DMake(41.313433, -72.930542),
CLLocationCoordinate2DMake(41.313324, -72.929963),
CLLocationCoordinate2DMake(41.312758, -72.929027),
CLLocationCoordinate2DMake(41.312373, -72.927167),
CLLocationCoordinate2DMake(41.311674, -72.925506)]
let polygon = MKPolygon(coordinates: &points, count: points.count)
mapView.add(polygon)
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolygon {
let polygonView = MKPolygonRenderer(overlay: overlay)
polygonView.fillColor = UIColor(red: 0, green: 0.847, blue: 1, alpha: 0.25)
return polygonView
}
return nil
}
}
您 mapView(_:rendererFor:)
的签名不正确。在 Swift 3 中,它将是:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
...
}
如果您正式声明遵守 MKMapViewDelegate
等协议,它通常会警告您这些事情。例如,最佳实践是在 class 扩展中而不是在 class 定义本身中这样做:
extension MapScreen: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
...
}
}
顺便说一句,如果这样做,您不仅会收到有关协议一致性的警告,而且在设置 delegate
:
mapView.delegate = self