2017-12-19 1 views
0

私はMKMapViewを使用しています(実行中のアプリケーションです)が、ユーザーのパスを追跡していますが、2色のラインを作成する必要があります。 1つはストロークの中心、もう1つはストロークの境界です。このため、レンダラーを返すために、func mapView(_ mapView:MKMapView、rendererFor:MKOverlay) - > MKOverlayRendererというUIViewControllerクラスのメソッドを実装しています。MKMapViewに異なるストローク/カラーで2つのMKPolylineを描画するにはどうすればよいですか?

私は

任意のアイデアスウィフト4を使用していますか?

ありがとうございます。

答えて

0

質問を書いている間に解決策に到達するので、解決方法を教えてください。

まず、あなたが

fileprivate class ForegroundOverlay: MKPolyline{ 

} 
fileprivate class BackgroundOverlay: MKPolyline{ 

} 

セカンドMKPolylineクラスを拡張する2つのクラスを作成する必要があり、あなたは、位置更新

var positions = [CLLocationCoordinate2D]() 
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     let userLocation:CLLocation = locations[0] as CLLocation 

     positions.append(userLocation.coordinate) 

     print("Nuber of locations \(positions.count)") 
     print("user latitude = \(userLocation.coordinate.latitude)") 
     print("user longitude = \(userLocation.coordinate.longitude)") 

     speedIndicator.text = "Speed: \(userLocation.speed * 3.6). Altitude: \(userLocation.altitude)" 


     let fPolyLine = BackgroundOverlay(coordinates: positions, count: positions.count) 

     mapView.addOverlays([fPolyLine], level: MKOverlayLevel.aboveRoads) 

     let bPolyLine = ForegroundOverlay(coordinates: positions, count: positions.count) 

     mapView.addOverlays([bPolyLine], level: MKOverlayLevel.aboveRoads) 

    } 

第三にトリガーされるイベントを変更する必要があり、あなたは持っていますポリラインが1つまたは別のクラスかどうかを尋ねます。

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 
    let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline) 
    if overlay is ForegroundOverlay { 
     renderer.strokeColor = UIColor(red: 230/255, green: 230/255, blue: 1, alpha: 0.5) 
     renderer.lineWidth = 10 
    } else { 
     renderer.strokeColor = UIColor(red: 0, green: 0, blue: 1, alpha: 0.5) 
     renderer.lineWidth = 30 
    } 

    return renderer 
} 

結果は、この

enter image description here

のようになります。
関連する問題