Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Created November 11, 2019 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anupamchugh/5147dbfb4387b1ce34cfb6fa366b6cdd to your computer and use it in GitHub Desktop.
Save anupamchugh/5147dbfb4387b1ce34cfb6fa366b6cdd to your computer and use it in GitHub Desktop.
struct ContentView: View {
var body: some View {
PolylineMapView()
}
}
struct PolylineMapView: UIViewRepresentable {
func makeCoordinator() -> MapViewCoordinator{
return MapViewCoordinator(self)
}
func updateUIView(_ view: MKMapView, context: Context){
view.delegate = context.coordinator
let b2MLocation = [CLLocationCoordinate2D(
latitude: 12.9352, longitude: 77.6244), CLLocationCoordinate2D(
latitude: 19.0760, longitude: 72.8777)]
let m2DLocation = [CLLocationCoordinate2D(
latitude: 19.0760, longitude: 72.8777),
CLLocationCoordinate2D(latitude: 28.7041, longitude: 77.1025)]
let d2BLocation = [CLLocationCoordinate2D(latitude: 28.7041, longitude: 77.1025),
CLLocationCoordinate2D(
latitude: 12.9352, longitude: 77.6244)]
let polyline1 = MKPolyline(coordinates: b2MLocation, count: b2MLocation.count)
let polyline2 = MKPolyline(coordinates: m2DLocation, count: m2DLocation.count)
let polyline3 = MKPolyline(coordinates: d2BLocation, count: d2BLocation.count)
view.addOverlays([polyline1, polyline2, polyline3])
}
func makeUIView(context: Context) -> MKMapView{
MKMapView(frame: .zero)
}
}
class MapViewCoordinator: NSObject, MKMapViewDelegate {
var mapViewController: PolylineMapView
init(_ control: PolylineMapView) {
self.mapViewController = control
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if let multiPolyline = overlay as? MKMultiPolyline{
let polylineRenderer = MKMultiPolylineRenderer(multiPolyline: multiPolyline)
polylineRenderer.strokeColor = UIColor.blue.withAlphaComponent(0.5)
polylineRenderer.lineWidth = 5
}
return MKOverlayRenderer(overlay: overlay)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment