public bool ComprobarDistanciaYCarretera(InfoCloseVehicule infoVehicle, Coordinate x1, Coordinate x2, Coordinate y1, Coordinate y2) { try { if (infoVehicle.distance < alertDistance) { List <string> listacalles = ListaCalles(); List <string> listacontrolarsentidoautopistas = ListaAutopistas(); bool esunacarretera = !listacalles.Any(x => infoVehicle.adressSlowVehicule.Split(',')[0].ToLower().Contains(x.ToLower())); bool esdeunicosentido = listacontrolarsentidoautopistas.Any(x => infoVehicle.adressSlowVehicule.Split(',')[0].ToLower().Contains(x.ToLower())); if (infoVehicle.adressSlowVehicule.Split(',')[0] == infoVehicle.adressMyVehicule.Split(',')[0] && esunacarretera) { if (esdeunicosentido) { if (EsMismoSentido(x1, x2, y1, y2)) { return(true); } } else { return(true); } } } return(false); } catch (Exception ex) { Debug.WriteLine("ComprobarDistanciaYCarretera: " + ex.Message); return(false); } }
/// <summary> /// Funcion que devuelve la informacion avanzada entre dos puntos /// </summary> /// <param name="fromLong">Longitud origen</param> /// <param name="fromLat">Latitud origen</param> /// <param name="toLong">Longitud destino</param> /// <param name="toLat">Latitud destino</param> /// <returns>Devuelve informacion de si estan en la misma carretera, distancia por carretera, tiempo</returns> private async Task <InfoCloseVehicule> GetInformationCloseVehicle(double fromLong, double fromLat, double toLong, double toLat) { try { InfoCloseVehicule informacionVehiculo = new InfoCloseVehicule(); var url = HttpWebRequest.Create("https://maps.googleapis.com/maps/api/directions/json?origin=" + fromLong.ToString().Replace(',', '.') + "," + fromLat.ToString().Replace(',', '.') + "&destination=" + toLong.ToString().Replace(',', '.') + "," + toLat.ToString().Replace(',', '.') + "&sensor=false");/*&key=AIzaSyAC25WcCdJIF5uvWLXMgGuYK4Y9sBZpJ34");*/ url.ContentType = "application/json"; url.Method = "GET"; using (HttpWebResponse response = url.GetResponseAsync().Result as HttpWebResponse) { if (response.StatusCode == HttpStatusCode.OK) { StreamReader reader = new StreamReader(response.GetResponseStream()); var content = reader.ReadToEnd(); var JSONObject = Newtonsoft.Json.Linq.JObject.Parse(content); string distanciaText = JSONObject["routes"][0]["legs"][0]["distance"]["text"].ToString(); string tiempoText = JSONObject["routes"][0]["legs"][0]["duration"]["text"].ToString(); string distancia = JSONObject["routes"][0]["legs"][0]["distance"]["value"].ToString(); string tiempo = JSONObject["routes"][0]["legs"][0]["duration"]["value"].ToString(); string direccionVehiculo = JSONObject["routes"][0]["legs"][0]["end_address"].ToString(); string direccionPropia = JSONObject["routes"][0]["legs"][0]["start_address"].ToString(); informacionVehiculo.distance = Int32.Parse(distancia); informacionVehiculo.time = Int32.Parse(tiempo); informacionVehiculo.distanceText = distanciaText; informacionVehiculo.timeText = tiempoText; informacionVehiculo.adressSlowVehicule = direccionVehiculo; informacionVehiculo.adressMyVehicule = direccionPropia; } } return(informacionVehiculo); } catch (Exception ex) { Debug.WriteLine("GetInformationCloseVehicle: " + ex.Message); return(new InfoCloseVehicule()); } }
/// <summary> /// Funcion que actualiza los puntos del mapa, comprueba la distancia por coordendas, si esta muy cerca llama a la API de google y lanza aviso si es necesario /// </summary> /// <returns></returns> public async Task <bool> UpdateMarkers() { try { VehiclesService _vehServ = new VehiclesService(); var vehicles = await _vehServ.GetVehicles(); this._map.Pins.Clear(); List <Alerta> alertasbuffer = new List <Alerta>(); foreach (var vehicle in vehicles) { InfoCloseVehicule infoVehicle = new InfoCloseVehicule(); if (CalculateDistanceLine(myPosition.Latitude, myPosition.Longitude, vehicle.Object.CurrentPosition.Coordinate.Latitude, vehicle.Object.CurrentPosition.Coordinate.Longitude) < distancePosibleAlert) { infoVehicle = await this.GetInformationCloseVehicle(myPosition.Latitude, myPosition.Longitude, vehicle.Object.CurrentPosition.Coordinate.Latitude, vehicle.Object.CurrentPosition.Coordinate.Longitude); } var pin = new Pin { Type = PinType.Place, Position = new Position(vehicle.Object.CurrentPosition.Coordinate.Latitude, vehicle.Object.CurrentPosition.Coordinate.Longitude), Label = vehicle.Object.CurrentPosition.Vehicle.ToString(), }; this._map.Pins.Add(pin); bool lanzaraviso = ComprobarDistanciaYCarretera(infoVehicle, new Coordinate(myPosition.Latitude, myPosition.Longitude), new Coordinate(myLastPosition.Latitude, myLastPosition.Longitude), vehicle.Object.CurrentPosition.Coordinate, vehicle.Object.LastPosition.Coordinate); if (lanzaraviso) { alertasbuffer.Add(new Alerta(vehicle.Key, vehicle.Object.CurrentPosition.Vehicle, infoVehicle.distanceText, 0, TextoANumero(infoVehicle.distanceText))); } } //alertasbuffer.Add(new Alerta("1", new Vehicle(), "300 m", 0, 300)); //alertasbuffer.Add(new Alerta("2", new Vehicle(), "350 m", 0, 350)); //alertasbuffer.Add(new Alerta("3", new Vehicle(), "360 m", 0, 360)); //alertasbuffer.Add(new Alerta("4", new Vehicle(), "300 m", 0, 300)); //alertasbuffer.Add(new Alerta("5", new Vehicle(), "300 m", 0, 300)); //alertasbuffer.Add(new Alerta("6", new Vehicle(), "300 m", 0, 300)); alertas = CompararAlertasGuardadasVsNuevas(alertasbuffer); if (alertas.Count > 0) { Alertar(alertas); } else { PararAlertar(); } await Task.Delay(5000); CenterMap(); return(true); } catch (Exception ex) { CenterMap(); Debug.WriteLine("UpdateMarkers: " + ex.Message); return(true); } }