private async Task <PolylineOptions> RequestRoutePolyline(Geoposition from, Geoposition to) { // Origin of route string originQueryParam = $"origin={from.Latitude.ToString(CultureInfo.InvariantCulture)},{from.Longitude.ToString(CultureInfo.InvariantCulture)}"; // Destination of route string destinationQueryParam = $"destination={to.Latitude.ToString(CultureInfo.InvariantCulture)},{to.Longitude.ToString(CultureInfo.InvariantCulture)}"; // Sensor enabled string sensor = "sensor=false"; // Auth string key = "key=AIzaSyDDKvhUqz1fnEImpiC8zrflraQdgo8PaMo"; RootRouteModel routeData = default(RootRouteModel); try { // Building the parameters to the web service string parameters = string.Join("&", new[] { originQueryParam, destinationQueryParam, sensor, key }); UriBuilder uri = new UriBuilder("https://maps.googleapis.com/maps/api/directions/json"); uri.Query = parameters; var requestProvider = new RequestProvider(); routeData = await requestProvider.GetAsync <RootRouteModel>(uri.ToString()); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Error calling routes api from google: {ex}"); } PolylineOptions polylineOptions = new PolylineOptions(); if (routeData?.Routes?.Any() == true) { var route = routeData.Routes.FirstOrDefault(); var polylinePoints = route.Polyline.Points; foreach (var step in route.Steps) { var points = DecodePolyline(polylinePoints); foreach (var point in points) { polylineOptions.Add(point); } } } return(polylineOptions); }
private async Task <PolylineOptions> RequestRoutePolyline(CarPool.Clients.Core.Maps.Model.RouteModel route) { // Origin of route string originQueryParam = $"origin={route.From.Latitude.ToString(CultureInfo.InvariantCulture)},{route.From.Longitude.ToString(CultureInfo.InvariantCulture)}"; // Destination of route string destinationQueryParam = $"destination={route.To.Latitude.ToString(CultureInfo.InvariantCulture)},{route.To.Longitude.ToString(CultureInfo.InvariantCulture)}"; // route waypoints if (route.WayPoints != null && route.WayPoints.Any()) { destinationQueryParam += "&waypoints=optimize:true"; foreach (var point in route.WayPoints) { destinationQueryParam += $"|{point.Latitude.ToString(CultureInfo.InvariantCulture)},{point.Longitude.ToString(CultureInfo.InvariantCulture)}"; } } // Sensor enabled string sensor = "sensor=false"; // Auth // TODO sacar string key = $"key={AppSettings.GoogleMapsAPIKey}"; RootRouteModel routeData = default(RootRouteModel); try { // Building the parameters to the web service string parameters = string.Join("&", new[] { originQueryParam, destinationQueryParam, sensor, key }); UriBuilder uri = new UriBuilder("https://maps.googleapis.com/maps/api/directions/json"); uri.Query = parameters; var requestProvider = new RequestProvider(); routeData = await requestProvider.GetAsync <RootRouteModel>(uri.ToString()); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Error calling routes api from google: {ex}"); } PolylineOptions polylineOptions = new PolylineOptions(); if (routeData?.Routes?.Any() == true) { var serviceRoute = routeData.Routes.FirstOrDefault(); var polylinePoints = serviceRoute.Polyline.Points; double totalDistance = 0, totalTime = 0; var points = DecodePolyline(polylinePoints); foreach (var point in points) { polylineOptions.Add(point); } foreach (var step in serviceRoute.Steps) { totalDistance += step?.Distance?.Value ?? 0; totalTime += step?.Duration?.Value ?? 0; } route.Distance = totalDistance; route.Duration = totalTime / 60; } return(polylineOptions); }