//public static void Initialize(string googleMapsKey) //{ // _googleMapsKey = googleMapsKey; //} public async Task <GoogleDirection> GetDirections(string originLatitude, string originLongitude, string destinationLatitude, string destinationLongitude) { GoogleDirection googleDirection = new GoogleDirection(); using (var httpClient = CreateClient()) { var endpoint = $"api/directions/json?mode=driving&transit_routing_preference=less_driving&origin={originLatitude},{originLongitude}&destination={destinationLatitude},{destinationLongitude}&key={_googleMapsKey}"; // endpoint = "api/directions/json?mode=driving&transit_routing_preference=less_driving&origin=28.644800,77.216721&destination=19.076090,72.877426&key=AIzaSyBzdtok4lk__zTykbElAp4fNgvgoYMyILg"; var response = await httpClient.GetAsync(endpoint).ConfigureAwait(false); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (!string.IsNullOrWhiteSpace(json)) { googleDirection = await Task.Run(() => JsonConvert.DeserializeObject <GoogleDirection>(json) ).ConfigureAwait(false); } } } return(googleDirection); }
public async Task <GoogleDirection> GetDirections(string originLatitude, string originLongitude, string destinationLatitude, string destinationLongitude) { GoogleDirection googleDirection = new GoogleDirection(); using (var httpClient = CreateClient()) { string url = ApiBaseAddress + "api/directions/json?mode=driving&transit_routing_preference=less_driving&origin=" + originLatitude + "," + originLongitude + "&destination=" + destinationLatitude + "," + destinationLongitude + "&key=###"; var response = await httpClient.GetAsync(url).ConfigureAwait(false); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (!string.IsNullOrWhiteSpace(json)) { googleDirection = await Task.Run(() => JsonConvert.DeserializeObject <GoogleDirection>(json) ).ConfigureAwait(false); } } } return(googleDirection); }
public async Task <GoogleDirection> GetDirections(string originLatitude, string originLongitude, string destinationLatitude, string destinationLongitude) { GoogleDirection googleDirection = new GoogleDirection(); var httpClient = new HttpClient() { BaseAddress = new Uri(APIBaseAddress) }; httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType)); var response = await httpClient.GetAsync($"api/directions/json?mode=driving&transit_routing_preference=less_driving&origin={originLatitude},{originLongitude}&destination={destinationLatitude},{destinationLongitude}&key={googleMapsAPIKey}").ConfigureAwait(false); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (!string.IsNullOrWhiteSpace(json)) { googleDirection = JsonConvert.DeserializeObject <GoogleDirection>(json); } } return(googleDirection); }
public async Task <GoogleDirection> GetDirections(string originLatitude, string originLongitude, string destinationLatitude, string destinationLongitude) { GoogleDirection googleDirection = new GoogleDirection(); using (var httpClient = CreateClient()) { var response = await httpClient.GetAsync($"api/directions/json?mode=driving&transit_routing_preference=less_driving&origin={originLatitude}," + $"{originLongitude}&destination={destinationLatitude},{destinationLongitude}&key=AIzaSyBfx7Wgm0NDTk0ctaKAPWlfb61zM1YGGgs").ConfigureAwait(false); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (!string.IsNullOrWhiteSpace(json)) { googleDirection = await Task.Run(() => JsonConvert.DeserializeObject <GoogleDirection>(json) ).ConfigureAwait(false); } } } return(googleDirection); }
public async Task <GoogleDirection> GetDirectionAsync(string originLatitude, string originLongitude, string destinationLatitude, string destinationLongitude) { GoogleDirection googleDirection = new GoogleDirection(); var googleMapsApi = RestService.For <IGoogleMapsAPI>(ApiBaseAddress); var response = await googleMapsApi.GetDirections(originLatitude, originLongitude, destinationLatitude, destinationLongitude, Constants.GoogleMapsApiKey); if (response.IsSuccessStatusCode) { googleDirection = response.Content; } return(googleDirection); }
//InitPins() should be called before this method, _waypoints is added in InitPins() //A method that let's google handle the directions, shortest path etc. private async void MapDirections(string message = "") { //for diagnostic numberOfAPICalls++; Console.WriteLine($"{message} Number Of API Calls: #{numberOfAPICalls}"); map.Polylines.Clear(); if (_currentLocation == null) { _currentLocation = await Geolocation.GetLastKnownLocationAsync(); } Position lastKnownPosition = new Position(_currentLocation.Latitude, _currentLocation.Longitude); if (_waypoints.Count > 0 && App.CheckIfInternet()) { _invoicesCollection.Clear(); _direction = await GoogleMapsAPI.MapDirectionsWithWaypoints(lastKnownPosition, _waypoints.ToArray()); //Only create a line if it returns something from google if (_direction.Status != "ZERO_RESULTS" && _direction.Routes.Count > 0) { List <Position> directionPolylines = PolylineHelper.Decode(_direction.Routes[0].OverviewPolyline.Points).ToList(); CreatePolylinesOnMap(directionPolylines); foreach (int order in GoogleMapsAPI._waypointsOrder) { _invoicesCollection.Add(_invoices[order]); } DeliveryItemView.ItemsSource = _invoicesCollection; } else { await DisplayAlert("Oops", "Unable to map the directions, please try to use internet connections or restart the app", "OK"); } } else if (_waypoints.Count() == 0 && App.CheckIfInternet()) { _direction = await GoogleMapsAPI.MapDirectionsNoWaypoints(lastKnownPosition); if (_direction.Status != "ZERO_RESULTS" && _direction.Routes.Count > 0) { List <Position> directionPolylines = PolylineHelper.Decode(_direction.Routes[0].OverviewPolyline.Points).ToList(); CreatePolylinesOnMap(directionPolylines); } } }
public async Task <GoogleDirection> GetDirections(string originLatitude, string originLongitude, string destinationLatitude, string destinationLongitude) { GoogleDirection googleDirection = new GoogleDirection(); var response = await client.GetAsync($"api/directions/json?mode=driving&transit_routing_preference=less_driving&origin={originLatitude},{originLongitude}&destination={destinationLatitude},{destinationLongitude}&key={Constants.GoogleMapsApiKey}").ConfigureAwait(false); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (!string.IsNullOrWhiteSpace(json)) { googleDirection = await Task.Run(() => JsonConvert.DeserializeObject <GoogleDirection>(json) ).ConfigureAwait(false); } } return(googleDirection); }
async void DrawRoute(Position start, Position end) { GoogleDirection googleDirection = await googleMapsApi.GetDirections(start.Latitude + "", start.Longitude + "", end.Latitude + "", end.Longitude + ""); if (googleDirection.Routes != null && googleDirection.Routes.Count > 0) { var positions = (Enumerable.ToList(PolylineHelper.Decode(googleDirection.Routes.First().OverviewPolyline.Points))); map.Polylines.Clear(); var polyline = new Xamarin.Forms.GoogleMaps.Polyline(); polyline.StrokeColor = Color.Black; polyline.StrokeWidth = 3; foreach (var p in positions) { polyline.Positions.Add(p); } map.Polylines.Add(polyline); map.MoveToRegion(MapSpan.FromBounds(new Xamarin.Forms.GoogleMaps.Bounds(new Position(polyline.Positions[0].Latitude, polyline.Positions[0].Longitude - 0.008), new Position(polyline.Positions[polyline.Positions.Count - 1].Latitude, polyline.Positions[polyline.Positions.Count - 1].Longitude + 0.008))), true); } }
public static PolylineOptions ResolveRoute(GoogleDirection googleDirection) { PolylineOptions po = new PolylineOptions(); if (googleDirection.routes.Count > 0) { string encodedPoints = googleDirection.routes[0].overview_polyline.points; List <LatLng> decodedPoints = DecodePolyLinePoints(encodedPoints); LatLng[] latLngPoints = new LatLng[decodedPoints.Count]; int index = 0; foreach (LatLng loc in decodedPoints) { latLngPoints[index++] = loc; } po.InvokeColor(Android.Graphics.Color.Red); po.Geodesic(true); po.Add(latLngPoints); } return(po); }
public async Task <GoogleDirection> GetDirections(string originLatitude, string originLongitude, string destinationLatitude, string destinationLongitude) { try { GoogleDirection googleDirection = new GoogleDirection(); using (var httpClient = CreateClient()) { originLatitude = originLatitude.Replace(',', '.'); originLongitude = originLongitude.Replace(',', '.'); destinationLatitude = destinationLatitude.Replace(',', '.'); destinationLongitude = destinationLongitude.Replace(',', '.'); Constants.jsoncallstring = $"api/directions/json?origin={originLatitude},{originLongitude}&destination={destinationLatitude},{destinationLongitude}®ion='pt-PT'&key={_googleMapsKey}"; var response = await httpClient.GetAsync(Constants.jsoncallstring).ConfigureAwait(false); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (!string.IsNullOrWhiteSpace(json)) { Constants.jsonstring = json.ToString(); googleDirection = await Task.Run(() => JsonConvert.DeserializeObject <GoogleDirection>(json) ).ConfigureAwait(false); } } } return(googleDirection); } catch (Exception ex) { throw ex; } }
private async void btn32_Clicked(object sender, EventArgs e) { Mapa1.IsShowingUser = true; MyStackLayout.IsVisible = false; MyStackLayout1.IsVisible = false; MyStackLayout2.IsVisible = false; MyStackLayout3.IsVisible = false; MyStackLayout4.IsVisible = false; MyStackLayout5.IsVisible = false; btn32.TextColor = Color.DarkRed; btn32.BackgroundColor = Color.White; btn32.FontAttributes = FontAttributes.Bold; // Stack2.IsVisible = false; btn2.BackgroundColor = Color.DarkRed; btn2.TextColor = Color.White; btn3.BackgroundColor = Color.DarkRed; btn3.TextColor = Color.White; var listApartmana = await _apartmani.Get <IList <Model.Apartmani> >(null); var listaAtrakcija = await _atrakcije.Get <IList <Model.Atrakcije> >(null); var listaKafica = await _kafici.Get <IList <Model.Kafici> >(null); var listaHotela = await _hoteli.Get <IList <Model.Hoteli> >(null); var listaRestorana = await _restorani.Get <IList <Model.Restorani> >(null); var listaNk = await _nocniklubovi.Get <IList <Model.Nightclubs> >(null); StackMapa.IsVisible = true; var request = new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(10)); cts = new CancellationTokenSource(); tcs = new TaskCompletionSource <PermissionStatus>(); var location = await Geolocation.GetLocationAsync(request, cts.Token); var client = new System.Net.Http.HttpClient(); Position position = new Position(location.Latitude, location.Longitude); MapSpan mapSpan = MapSpan.FromCenterAndRadius(position, Distance.FromKilometers(0.444)); Mapa1.MoveToRegion(mapSpan); if (APIService.Atraction) { foreach (var item in listaAtrakcija) { Pin pin = new Pin { Label = item.Naziv, Address = item.Lokacija, Type = PinType.Place, Position = new Position((double)item.Latitude, (double)item.Longitude) }; Mapa1.Pins.Add(pin); var lat1 = item.Latitude; var lon1 = item.Longitude; var lat2 = location.Latitude; var lon2 = location.Longitude; string trazeniUrl = @"https://maps.googleapis.com/maps/api/directions/json?origin=" + lat2 + "," + lon2 + "&destination=" + lat1 + "," + lon1 + "&key=AIzaSyDP-0g1tNQWjpbUKC0uLv3tJ7GGm6a3t8Q"; var response = await client.GetAsync(trazeniUrl); string contactsJson = await response.Content.ReadAsStringAsync(); //Getting response GoogleDirection ObjContactList = new GoogleDirection(); if (response != null) { ObjContactList = JsonConvert.DeserializeObject <GoogleDirection>(contactsJson); } Xamarin.Forms.Maps.Polyline polyline = new Xamarin.Forms.Maps.Polyline { StrokeColor = Color.Blue, StrokeWidth = 12, }; var brojRouta = ObjContactList.Routes[0].Legs[0].Steps.Count(); for (int i = 0; i < brojRouta; i++) { polyline.Geopath.Add(new Position(ObjContactList.Routes[0].Legs[0].Steps[i].StartLocation.Lat, ObjContactList.Routes[0].Legs[0].Steps[i].StartLocation.Lng)); polyline.Geopath.Add(new Position(ObjContactList.Routes[0].Legs[0].Steps[i].EndLocation.Lat, ObjContactList.Routes[0].Legs[0].Steps[i].EndLocation.Lng)); } Mapa1.MapElements.Add(polyline); } } if (APIService.Apartments) { foreach (var item in listApartmana) { Pin pin = new Pin { Label = item.Naziv, Address = item.Lokacija, Type = PinType.Place, Position = new Position((double)item.Latitude, (double)item.Longitude) }; Mapa1.Pins.Add(pin); var lat1 = item.Latitude; var lon1 = item.Longitude; var lat2 = location.Latitude; var lon2 = location.Longitude; string trazeniUrl = @"https://maps.googleapis.com/maps/api/directions/json?origin=" + lat2 + "," + lon2 + "&destination=" + lat1 + "," + lon1 + "&key=AIzaSyDP-0g1tNQWjpbUKC0uLv3tJ7GGm6a3t8Q"; var response = await client.GetAsync(trazeniUrl); string contactsJson = await response.Content.ReadAsStringAsync(); //Getting response GoogleDirection ObjContactList = new GoogleDirection(); if (response != null) { ObjContactList = JsonConvert.DeserializeObject <GoogleDirection>(contactsJson); } Xamarin.Forms.Maps.Polyline polyline = new Xamarin.Forms.Maps.Polyline { StrokeColor = Color.Blue, StrokeWidth = 12, }; var brojRouta = ObjContactList.Routes[0].Legs[0].Steps.Count(); for (int i = 0; i < brojRouta; i++) { polyline.Geopath.Add(new Position(ObjContactList.Routes[0].Legs[0].Steps[i].StartLocation.Lat, ObjContactList.Routes[0].Legs[0].Steps[i].StartLocation.Lng)); polyline.Geopath.Add(new Position(ObjContactList.Routes[0].Legs[0].Steps[i].EndLocation.Lat, ObjContactList.Routes[0].Legs[0].Steps[i].EndLocation.Lng)); } Mapa1.MapElements.Add(polyline); } } if (APIService.Food) { foreach (var item in listaRestorana) { Pin pin = new Pin { Label = item.Naziv, Address = item.Lokacija, Type = PinType.Place, Position = new Position((double)item.Latitude, (double)item.Longitude) }; Mapa1.Pins.Add(pin); var lat1 = item.Latitude; var lon1 = item.Longitude; var lat2 = location.Latitude; var lon2 = location.Longitude; string trazeniUrl = @"https://maps.googleapis.com/maps/api/directions/json?origin=" + lat2 + "," + lon2 + "&destination=" + lat1 + "," + lon1 + "&key=AIzaSyDP-0g1tNQWjpbUKC0uLv3tJ7GGm6a3t8Q"; var response = await client.GetAsync(trazeniUrl); string contactsJson = await response.Content.ReadAsStringAsync(); //Getting response GoogleDirection ObjContactList = new GoogleDirection(); if (response != null) { ObjContactList = JsonConvert.DeserializeObject <GoogleDirection>(contactsJson); } Xamarin.Forms.Maps.Polyline polyline = new Xamarin.Forms.Maps.Polyline { StrokeColor = Color.Blue, StrokeWidth = 12, }; var brojRouta = ObjContactList.Routes[0].Legs[0].Steps.Count(); for (int i = 0; i < brojRouta; i++) { polyline.Geopath.Add(new Position(ObjContactList.Routes[0].Legs[0].Steps[i].StartLocation.Lat, ObjContactList.Routes[0].Legs[0].Steps[i].StartLocation.Lng)); polyline.Geopath.Add(new Position(ObjContactList.Routes[0].Legs[0].Steps[i].EndLocation.Lat, ObjContactList.Routes[0].Legs[0].Steps[i].EndLocation.Lng)); } Mapa1.MapElements.Add(polyline); } } if (APIService.Nightclubs) { foreach (var item in listaNk) { Pin pin = new Pin { Label = item.Naziv, Address = item.Lokacija, Type = PinType.Place, Position = new Position((double)item.Latitude, (double)item.Longitude) }; Mapa1.Pins.Add(pin); var lat1 = item.Latitude; var lon1 = item.Longitude; var lat2 = location.Latitude; var lon2 = location.Longitude; string trazeniUrl = @"https://maps.googleapis.com/maps/api/directions/json?origin=" + lat2 + "," + lon2 + "&destination=" + lat1 + "," + lon1 + "&key=AIzaSyDP-0g1tNQWjpbUKC0uLv3tJ7GGm6a3t8Q"; var response = await client.GetAsync(trazeniUrl); string contactsJson = await response.Content.ReadAsStringAsync(); //Getting response GoogleDirection ObjContactList = new GoogleDirection(); if (response != null) { ObjContactList = JsonConvert.DeserializeObject <GoogleDirection>(contactsJson); } Xamarin.Forms.Maps.Polyline polyline = new Xamarin.Forms.Maps.Polyline { StrokeColor = Color.Blue, StrokeWidth = 12, }; var brojRouta = ObjContactList.Routes[0].Legs[0].Steps.Count(); for (int i = 0; i < brojRouta; i++) { polyline.Geopath.Add(new Position(ObjContactList.Routes[0].Legs[0].Steps[i].StartLocation.Lat, ObjContactList.Routes[0].Legs[0].Steps[i].StartLocation.Lng)); polyline.Geopath.Add(new Position(ObjContactList.Routes[0].Legs[0].Steps[i].EndLocation.Lat, ObjContactList.Routes[0].Legs[0].Steps[i].EndLocation.Lng)); } Mapa1.MapElements.Add(polyline); } } if (APIService.Coffeeshops) { foreach (var item in listaKafica) { Pin pin = new Pin { Label = item.Naziv, Address = item.Lokacija, Type = PinType.Place, Position = new Position((double)item.Latitude, (double)item.Longitude) }; Mapa1.Pins.Add(pin); var lat1 = item.Latitude; var lon1 = item.Longitude; var lat2 = location.Latitude; var lon2 = location.Longitude; string trazeniUrl = @"https://maps.googleapis.com/maps/api/directions/json?origin=" + lat2 + "," + lon2 + "&destination=" + lat1 + "," + lon1 + "&key=AIzaSyDP-0g1tNQWjpbUKC0uLv3tJ7GGm6a3t8Q"; var response = await client.GetAsync(trazeniUrl); string contactsJson = await response.Content.ReadAsStringAsync(); //Getting response GoogleDirection ObjContactList = new GoogleDirection(); if (response != null) { ObjContactList = JsonConvert.DeserializeObject <GoogleDirection>(contactsJson); } Xamarin.Forms.Maps.Polyline polyline = new Xamarin.Forms.Maps.Polyline { StrokeColor = Color.Blue, StrokeWidth = 12, }; var brojRouta = ObjContactList.Routes[0].Legs[0].Steps.Count(); for (int i = 0; i < brojRouta; i++) { polyline.Geopath.Add(new Position(ObjContactList.Routes[0].Legs[0].Steps[i].StartLocation.Lat, ObjContactList.Routes[0].Legs[0].Steps[i].StartLocation.Lng)); polyline.Geopath.Add(new Position(ObjContactList.Routes[0].Legs[0].Steps[i].EndLocation.Lat, ObjContactList.Routes[0].Legs[0].Steps[i].EndLocation.Lng)); } Mapa1.MapElements.Add(polyline); } } }
public async void AddPins() { var listApartmana = await _apartmani.Get <IList <Model.Apartmani> >(null); var listaAtrakcija = await _atrakcije.Get <IList <Model.Atrakcije> >(null); var listaKafica = await _kafici.Get <IList <Model.Kafici> >(null); var listaHotela = await _hoteli.Get <IList <Model.Hoteli> >(null); var listaRestorana = await _restorani.Get <IList <Model.Restorani> >(null); var listaNk = await _nocniklubovi.Get <IList <Model.Nightclubs> >(null); object trazeniModel = null; foreach (var item in listApartmana) { string lowerSearch = APIService.SearchCon.ToLower(); string lowerLetters = item.Naziv.ToLower(); if (lowerLetters.Contains(lowerSearch)) { trazeniModel = item; temp.Add(new Model.ReportClass { Naziv = item.Naziv, Ocjena = item.Ocjena, Vrsta = item.Vrsta, Latitude = item.Latitude, Longitude = item.Longitude, Adresa = item.Lokacija }); } } foreach (var item in listaAtrakcija) { string lowerSearch = APIService.SearchCon.ToLower(); string lowerLetters = item.Naziv.ToLower(); if (lowerLetters.Contains(lowerSearch)) { trazeniModel = item; temp.Add(new Model.ReportClass { Naziv = item.Naziv, Ocjena = item.Ocjena, Vrsta = item.Vrsta, Latitude = item.Latitude, Longitude = item.Longitude, Adresa = item.Lokacija }); } } foreach (var item in listaHotela) { string lowerSearch = APIService.SearchCon.ToLower(); string lowerLetters = item.Naziv.ToLower(); if (lowerLetters.Contains(lowerSearch)) { trazeniModel = item; temp.Add(new Model.ReportClass { Naziv = item.Naziv, Ocjena = item.Ocjena, Vrsta = "Hotel", Latitude = item.Latitude, Longitude = item.Longitude, Adresa = item.Lokacija }); } } foreach (var item in listaKafica) { string lowerSearch = APIService.SearchCon.ToLower(); string lowerLetters = item.Naziv.ToLower(); if (lowerLetters.Contains(lowerSearch)) { trazeniModel = item; temp.Add(new Model.ReportClass { Naziv = item.Naziv, Ocjena = item.Ocjena, Vrsta = "Kafic", Latitude = item.Latitude, Longitude = item.Longitude, Adresa = item.Lokacija }); } } foreach (var item in listaRestorana) { string lowerSearch = APIService.SearchCon.ToLower(); string lowerLetters = item.Naziv.ToLower(); if (lowerLetters.Contains(lowerSearch)) { trazeniModel = item; temp.Add(new Model.ReportClass { Naziv = item.Naziv, Ocjena = item.Ocjena, Vrsta = "Restoran", Latitude = item.Latitude, Longitude = item.Longitude, Adresa = item.Lokacija }); } } foreach (var item in listaNk) { string lowerSearch = APIService.SearchCon.ToLower(); string lowerLetters = item.Naziv.ToLower(); if (lowerLetters.Contains(lowerSearch)) { trazeniModel = item; temp.Add(new Model.ReportClass { Naziv = item.Naziv, Ocjena = item.Ocjena, Vrsta = "Nightclub", Latitude = item.Latitude, Longitude = item.Longitude, Adresa = item.Lokacija }); } } var request = new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(10)); cts = new CancellationTokenSource(); tcs = new TaskCompletionSource <PermissionStatus>(); var location = await Geolocation.GetLocationAsync(request, cts.Token); var client = new System.Net.Http.HttpClient(); foreach (var item in temp) { Pin pin = new Pin { Label = item.Naziv, Address = item.Adresa, Type = PinType.Place, Position = new Position((double)item.Latitude, (double)item.Longitude) }; Mapa.Pins.Add(pin); var lat1 = item.Latitude; var lon1 = item.Longitude; var lat2 = location.Latitude; var lon2 = location.Longitude; string trazeniUrl = @"https://maps.googleapis.com/maps/api/directions/json?origin=" + lat2 + "," + lon2 + "&destination=" + lat1 + "," + lon1 + "&key=AIzaSyDP-0g1tNQWjpbUKC0uLv3tJ7GGm6a3t8Q"; var response = await client.GetAsync(trazeniUrl); string contactsJson = await response.Content.ReadAsStringAsync(); //Getting response GoogleDirection ObjContactList = new GoogleDirection(); if (response != null) { ObjContactList = JsonConvert.DeserializeObject <GoogleDirection>(contactsJson); } Xamarin.Forms.Maps.Polyline polyline = new Xamarin.Forms.Maps.Polyline { StrokeColor = Color.Blue, StrokeWidth = 12, }; var brojRouta = ObjContactList.Routes[0].Legs[0].Steps.Count(); for (int i = 0; i < brojRouta; i++) { polyline.Geopath.Add(new Position(ObjContactList.Routes[0].Legs[0].Steps[i].StartLocation.Lat, ObjContactList.Routes[0].Legs[0].Steps[i].StartLocation.Lng)); polyline.Geopath.Add(new Position(ObjContactList.Routes[0].Legs[0].Steps[i].EndLocation.Lat, ObjContactList.Routes[0].Legs[0].Steps[i].EndLocation.Lng)); } Mapa.MapElements.Add(polyline); } }
//A method that let's google handle the directions, shortest path etc. private async void MapDirections(string message = "") { //for diagnostic numberOfAPICalls++; Console.WriteLine($"{message} Number Of API Calls: #{numberOfAPICalls}"); map.Polylines.Clear(); if (_currentLocation == null) { _currentLocation = await Geolocation.GetLastKnownLocationAsync(); } Position lastKnownPosition = new Position(_currentLocation.Latitude, _currentLocation.Longitude); // Position lastKnownPosition = new Position(-46.3, 168); if (_waypoints.Count > 0 && App.CheckIfInternet()) { _invoicesCollection.Clear(); if (_waypoints.Count > MAX_WAYPOINTS) { GoogleMapsAPI.SortWaypoints(lastKnownPosition, _waypoints.ToArray()); List <Invoice> tempInvoice = new List <Invoice>(); for (int i = 0; i < GoogleMapsAPI._waypointsOrder.Count; i++) { tempInvoice.Add(_invoices[GoogleMapsAPI._waypointsOrder[i]]); } _invoices = tempInvoice; } for (int i = 0; i < _waypoints.Count; i += MAX_WAYPOINTS) { string destinationWaypoint; int waypointCountLeft = _waypoints.Count - i; string[] orderedWaypoints; if (waypointCountLeft > MAX_WAYPOINTS) { orderedWaypoints = new string[MAX_WAYPOINTS]; _waypoints.CopyTo(i, orderedWaypoints, 0, MAX_WAYPOINTS); destinationWaypoint = orderedWaypoints[orderedWaypoints.Count() - 1]; } else { orderedWaypoints = new string[waypointCountLeft]; _waypoints.CopyTo(i, orderedWaypoints, 0, waypointCountLeft); if ((destinationWaypoint = Preferences.Get("EndPointGeoWaypoint", string.Empty)) == "") { destinationWaypoint = orderedWaypoints[orderedWaypoints.Count() - 1]; } } _direction = await GoogleMapsAPI.MapDirectionsWithWaypoints(lastKnownPosition, destinationWaypoint, orderedWaypoints); string[] newCurrentPosition = orderedWaypoints[orderedWaypoints.Count() - 1].Split(new string[] { "%2C" }, StringSplitOptions.None); lastKnownPosition = new Position(Convert.ToDouble(newCurrentPosition[0]), Convert.ToDouble(newCurrentPosition[1])); //Only create a line if it returns something from google if (_direction.Status != "ZERO_RESULTS" && _direction.Routes.Count > 0) { AddPolyLine(); foreach (int order in GoogleMapsAPI._waypointsOrder) { _invoicesCollection.Add(_invoices[order + i]); } } else { await DisplayAlert("Oops", "Unable to map the directions, Please make sure the address entered is legit", "OK"); } } DeliveryItemView.ItemsSource = _invoicesCollection; } else if (_waypoints.Count() == 0 && App.CheckIfInternet()) { string destinationPoint; if ((destinationPoint = Preferences.Get("EndPointGeoWaypoint", string.Empty)) == "") { return; } _direction = await GoogleMapsAPI.MapDirectionsNoWaypoints(lastKnownPosition, destinationPoint); if (_direction.Status != "ZERO_RESULTS" && _direction.Routes.Count > 0) { AddPolyLine(); } } }