2017-07-11 47 views
1

マップ上に境界線を表示するためにポリゴンを作成しようとしています。しかし、私は私のアプリを実行すると何も表示されません。私は地図を見ることはできますが、ポリゴンは見ることができません私のエラーは何ですか?ありがとう。Swift MapKit - ポリゴンを作成する

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  
    } 
} 
+0

あなたにコードを書く人がいるようです。 Stack Overflowは、コード作成サービスではなく、Q&Aサイトです。効果的な質問を書く方法については、こちらをご覧ください(http://stackoverflow.com/help/how-to-ask)。 – herrbischoff

答えて

2

mapView(_:rendererFor:)の署名が間違っています。スウィフト3では、それは次のようになります。

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 
    ... 
} 

あなたが正式にMKMapViewDelegateのようなプロトコルにあなたの適合を宣言すると、それは多くの場合、これらの事についての警告が表示されます。また、あなたは、だけでなく、あなたは、プロトコルの適合性についての警告を取得しないことをやる場合、ところで

extension MapScreen: MKMapViewDelegate { 

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 
     ... 
    } 

} 

が、たとえば、ベストプラクティスは、クラスの拡張ではなく、クラス定義自体で行うことになりますdelegateを設定するときにキャストする必要はありません。

mapView.delegate = self 
関連する問題