2017-12-19 2 views
0

나는 사용자의 경로 (실행중인 앱)를 추적하고있는 MKMapView를 가지고 있지만 요구 사항은 두 가지 색상으로 된 선을 만듭니다. 하나는 뇌졸중의 중심이고, 다른 하나는 뇌졸중의 경계입니다. 이를 위해 렌더러를 반환하기 위해 func mapView (_ mapView : MKMapView, rendererFor 오버레이 : MKOverlay) -> MKOverlayRenderer UIViewController 클래스의 메서드를 구현하고 있습니다.MKMapView에서 다른 획/색상으로 2 개의 MKPolyline을 그릴 수있는 방법은 무엇입니까?

스위프트를 사용하고 있습니다. 4

아이디어가 있으십니까?

미리 감사드립니다.

답변

0

좋아, 질문을 작성하는 동안 솔루션에 도달하므로 해결책을 말씀 드리겠습니다.

먼저, MKPolyline 클래스를

fileprivate class ForegroundOverlay: MKPolyline{ 

} 
fileprivate class BackgroundOverlay: MKPolyline{ 

} 

두 번째 연장 두 개의 클래스를 생성해야합니다, 당신은 당신이 위치 업데이트에

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) 

    } 

셋째 트리거되는 이벤트를 수정해야 폴리선이 하나 또는 다른 클래스인지 묻습니다.

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

모양을
관련 문제