2014-07-17 2 views
9

Swift 앱에서 폴리 라인을 그립니다.Swift에서 MKPolylineView를 사용하는 방법

스위프트 코드

class MapViewController: UIViewController, MKMapViewDelegate { 

    @IBOutlet var theMapView: MKMapView 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     setMapView() 
    } 

    func setMapView() { 
     //theMapView.zoomEnabled = false 
     //theMapView.scrollEnabled = false 
     theMapView.rotateEnabled = false 

     // 
     var lat: CLLocationDegrees = 37.586601 
     var lng: CLLocationDegrees = 127.009381 

     // 
     var latDelta: CLLocationDegrees = 0.008 
     var lngDelta: CLLocationDegrees = 0.008 

     var theSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lngDelta) 

     var initLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, lng) 
     var theRegion: MKCoordinateRegion = MKCoordinateRegionMake(initLocation, theSpan) 
     self.theMapView.setRegion(theRegion, animated: true) 

     var locations = [CLLocation(latitude: 37.582691, longitude: 127.011186), CLLocation(latitude: 37.586112,longitude: 127.011047), CLLocation(latitude: 37.588212, longitude: 127.010438)] 
     var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate}) 
     var polyline = MKPolyline(coordinates: &coordinates, count: locations.count) 

     var myPolylineView : MKPolylineView 
     /* error */ 
     myPolylineView.polyline = polyline // #1 
     myPolylineView.strokeColor = UIColor.blueColor() // #2 
     myPolylineView.lineWidth = 5; // #3 
     self.theMapView.addOverlay(myPolylineView) // #4 
     /* ----- */  
    } 
} 

오류 :이 해결책을 찾을 수 없습니다

// #1 <br> 
Cannot assign to 'polyline' in 'myPolylineView' <br> 
// #2 <br> 
'strokeColor' is unvailable: APIs deprecated as iOS 7 and earlier are unavailable in Swift <br> 
// #3 <br> 
'lineWidth' is unvailable: APIs deprecated as iOS 7 and earlier are unavailable in Swift <br> 
// #4 <br> 
Missing argument for parameter 'level' in call <br> 

.

답변

19

MKPolylineView 대신에 처음으로이되어야합니다. MKPolylineRenderer을 만들어야합니다.

MKPolylineView은 iOS 7 이후 더 이상 사용되지 않으며 필요한 경우 Objective-C에서도 사용할 수 있지만 Swift에서는 지원되지 않습니다.

둘째가 작성하고 (addOverlay에 전달되지 않음) rendererForOverlay 위임 방법에 MKPolylineRenderer을 반환해야합니다.

addOverlay 메서드에서는 MKPolylineView 또는 MKPolylineRenderer이 아닌 MKPolyline 개체를 전달합니다.

만들어 줄를 제거 setMapView 방법에 따라서


(. 당신이 rendererForOverlay에 반환 어떤 객체 addOverlay 대에 전달할 객체 것 사이의 차이에 대한 설명은 Adding MKOverlayPathRenderer as overlay to MKMapView gets exception 참조) 및 myPolylineView 설정 구현 rendererForOverlay 방법을 다음

self.theMapView.addOverlay(polyline) 

: 및에 addOverlay 줄을 변경

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { 
    if overlay is MKPolyline { 
     var polylineRenderer = MKPolylineRenderer(overlay: overlay) 
     polylineRenderer.strokeColor = UIColor.blueColor() 
     polylineRenderer.lineWidth = 5 
     return polylineRenderer 
    } 

    return nil 
} 

그렇지 않으면 위임 메서드가 호출되지 않고 오버레이가 표시되지 않습니다. theMapView가 함께 IBOutlet 경우, delegate 콘센트를 연결하거나 코드에서 설정 (예 : viewDidLoad에서 슈퍼를 호출 한 후.) :

self.theMapView.delegate = self 
+0

내가 정확히 한이 있지만, 프로토콜 rendererForOverlay가 .... –

+0

@VanDuTran 호출되지 않습니다, 1)지도보기 콘센트가 연결되어 있는지, 2)지도보기 대리자가 연결/설정되었는지, 3) rendererForOverlay 메서드의 철자가 올바른지 확인하십시오. 여전히 전화하지 않는 경우, 귀하의 세부 사항과 함께 새로운 질문을하십시오. – Anna

+0

내 geojson에 이상한 좌표가 있기 때문에 : "좌표": [[294636.0501,5039009.536], [294572.3566,5039049.88]] –

관련 문제