Created
December 31, 2017 01:24
-
-
Save ucotta/8f3f5bc5e0a79b4c0d406929dbb48834 to your computer and use it in GitHub Desktop.
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
/* | |
Get animated polyline route: | |
- https://stackoverflow.com/questions/42620510/how-to-get-animated-polyline-route-in-gmsmapview-so-that-it-move-along-with-map | |
Get polyline from origin and destination coordinates: | |
- https://github.com/lukagabric/LRouteController/blob/master/LRouteControllerSample/Classes/LRouteController/LRouteController.m | |
Draw stroke with some style: | |
var styles:[GMSStrokeStyle] = [GMSStrokeStyle.solidColor(UIColor.cyan), GMSStrokeStyle.solidColor(UIColor.clear)] | |
let polilyne = GMSPolyline(path: path) | |
polilyne.spans = GMSStyleSpans(path, styles, [20, 20], .rhumb) | |
*/ | |
// Google Maps example: | |
import UIKit | |
import GoogleMaps | |
import Alamofire | |
class ViewController: UIViewController { | |
var mapView: GMSMapView! | |
public typealias ApiData = Dictionary<String, Any> | |
public typealias ArrayApiData = Array<ApiData> | |
func presentaRutas(origen: CLLocationCoordinate2D, destino: CLLocationCoordinate2D, mode: String, color: UIColor) { | |
let url = "https://maps.googleapis.com/maps/api/directions/json" | |
var requestParameters: [String : Any] = [:] | |
requestParameters["origin"] = "\(origen.latitude),\(origen.longitude)" | |
requestParameters["destination"] = "\(destino.latitude),\(destino.longitude)" | |
requestParameters["sensor"] = "false" | |
requestParameters["mode"] = mode | |
requestParameters["key"] = "XXXXXXXXXXXXXXXXXXXXXXXXXXX" | |
Alamofire.request(url, method: .get, parameters: requestParameters).responseJSON { response in | |
guard response.result.isSuccess, | |
let data = response.result.value as? ApiData, | |
let routes = data["routes"] as? ArrayApiData, | |
routes.count > 0, | |
let first = routes.first as? ApiData, | |
let overview = first["overview_polyline"] as? ApiData, | |
let points = overview["points"] as? String, | |
let path = GMSPath(fromEncodedPath: points) else { | |
return | |
} | |
let gsm = GMSPolyline(path: path) | |
gsm.strokeColor = color | |
gsm.strokeWidth = 4 | |
if mode == "walking" { | |
var styles:[GMSStrokeStyle] = [GMSStrokeStyle.solidColor(UIColor.cyan), GMSStrokeStyle.solidColor(UIColor.clear)] | |
gsm.spans = GMSStyleSpans(path, styles, [20, 20], .rhumb) | |
} | |
gsm.map = self.mapView | |
} | |
} | |
override func loadView() { | |
navigationItem.title = "Hello Map" | |
let camera = GMSCameraPosition.camera(withLatitude: 40.4192079, | |
longitude: -3.6961897999999564, | |
zoom: 16) | |
mapView = GMSMapView.map(withFrame: .zero, camera: camera) | |
//let marker = GMSMarker() | |
//marker.position = camera.target | |
//marker.snippet = "Hello World" | |
//marker.appearAnimation = .pop | |
//marker.map = mapView | |
view = mapView | |
presentaRutas(origen: CLLocationCoordinate2DMake(40.4172679, -3.7010867999999846), destino: CLLocationCoordinate2DMake(40.4124759, -3.700625500000001), mode: "walking", color: UIColor.blue.withAlphaComponent(0.50)) | |
presentaRutas(origen: CLLocationCoordinate2DMake(40.4124759, -3.700625500000001), destino: CLLocationCoordinate2DMake(40.4320996, -3.663307400000008), mode: "bicycling", color: UIColor.red.withAlphaComponent(0.40)) | |
presentaRutas(origen: CLLocationCoordinate2DMake(40.4320996, -3.663307400000008), destino: CLLocationCoordinate2DMake(40.393910805867925, -3.9304579600873364), mode: "optimistic", color: UIColor.green.withAlphaComponent(0.40)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment