ViewController.swift
import UIKit import MapKit import GEOSwift
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
mapView.delegate = self
super.viewDidLoad()
addBoundry()
}
func addBoundry()
{
if let geoJSONURL = NSBundle.mainBundle().URLForResource("multipolygon", withExtension: "geojson"),
let geometries = try! Geometry.fromGeoJSON(geoJSONURL),
let geo = geometries[0] as? MultiPolygon
{
geo
}
//mapView.addOverlay(geo)
}
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolygon {
let polygonView = MKPolygonRenderer(overlay: overlay)
polygonView.strokeColor = UIColor.magentaColor()
return polygonView
}
return MKOverlayRenderer()
}}
- Multipolygon.geojson je naplnený Geometriami z Talianska https://github.com/andreacremaschi/GEOSwift/blob/master/GEOSPlayground.playground/Resources/multipolygon.geojson
Snažím sa použiť libary, pretože chcem vytvoriť polygon s otvormi. Ale nemôžem nájsť spôsob, ako vyriešiť môj problém.
keď sa pokúsim pridať Multipolygon s addoverlay
hádže chybu
Cannot invoke "addOverlay" with an argument list of type "(MultiPolygon<Polygon>)"
ktokoľvek s metódou na vyriešenie môjho problému?
odpovede:
0 pre odpoveď č. 1Tu je úryvok kódu. (swift 3.0)
func addBoundry() {
if let geoJSONURL = Bundle.main.url(forResource: "multipolygon", withExtension: "geojson") {
do {
let geometries = try Geometry.fromGeoJSON(geoJSONURL)
if let geo = geometries?[0] as? MultiPolygon {
if let shapesCollection = geo.mapShape() as? MKShapesCollection {
let shapes = shapesCollection.shapes
for shape in shapes {
if let polygon = shape as? MKPolygon {
mapView.add(polygon)
}
}
}
}
} catch {
print("Unable to load geojson data")
}
}
}