Last active
February 12, 2020 05:56
-
-
Save mzeeshanid/8b7f9ce44db9b844a5d0621b6edecd62 to your computer and use it in GitHub Desktop.
Directed polyline function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func drawPolyline() { | |
//Step 1: | |
let coordinates = self.geoJson.map({CLLocationCoordinate2D(latitude: $0.last!, longitude: $0.first!)}) | |
//Step 2: | |
let chunkSize = 3 | |
let chunkedCoordinates = coordinates.chunked(into: chunkSize) | |
//Step 3: | |
let path = GMSMutablePath() | |
//Step 4: | |
for chunk in chunkedCoordinates { | |
for coordinate in chunk { | |
path.add(coordinate) | |
} | |
if chunk.count == 3 { | |
let location1 = CLLocation(latitude: chunk[1].latitude, longitude: chunk[1].longitude) | |
let location2 = CLLocation(latitude: chunk[2].latitude, longitude: chunk[2].longitude) | |
if !GMSGeometryIsLocationOnPath(chunk[1], path, false) || | |
!GMSGeometryIsLocationOnPath(chunk[2], path, false) || | |
location2.distance(from: location1) < 1.5 { | |
continue | |
} | |
let angle = GMSGeometryHeading(chunk[1], chunk[2])// chunk[1].heading(to: chunk[2]) | |
var arrowImage = UIImage(named: "icon_navigation")! | |
arrowImage = arrowImage.withColor(UIColor.orange.withAlphaComponent(0.8)) | |
let marker = GMSMarker(position: chunk[1]) | |
marker.icon = arrowImage | |
marker.groundAnchor = CGPoint(x: 0.5, y: 0.5) | |
marker.isFlat = true | |
marker.rotation = angle | |
marker.map = self.mapView | |
} | |
} | |
//Step 5: | |
let polyline = GMSPolyline(path: path) | |
polyline.strokeColor = UIColor.orange | |
polyline.strokeWidth = 5.0 | |
polyline.map = self.mapView | |
//Step 6: | |
let bounds = GMSCoordinateBounds(path: path) | |
self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment