internal void UpdateLine(GoogleDirections gd) { Execute.OnUIThread(() => { var m = new WebMercator(); var ps = DecodeLatLong(gd.Directions.Polyline.points); var pl = new ESRI.ArcGIS.Client.Geometry.Polyline {Paths = new ObservableCollection<PointCollection>()}; var pc = new PointCollection(); foreach (var p in ps) { pc.Add((MapPoint) m.FromGeographic(new MapPoint(p.Longitude, p.Latitude))); } pl.Paths.Add(pc); _line.Geometry = pl; _line.SetZIndex(0); }); }
public static GoogleDirections GetDirections(string addressA, string addressB, string mode, string wayPoints = "") { var sbUrl = new StringBuilder(); //sbUrl.Append("http://maps.google.com/maps/nav?output=js&oe=utf8&q="); sbUrl.Append("http://maps.googleapis.com/maps/api/directions/json?"); sbUrl.AppendFormat("origin={0}", HttpUtility.UrlEncode(addressA)); sbUrl.AppendFormat("&destination={0}", HttpUtility.UrlEncode(addressB)); if (!string.IsNullOrEmpty(wayPoints)) sbUrl.AppendFormat("&waypoints={0}", HttpUtility.UrlEncode(wayPoints)); sbUrl.Append("&sensor=false&mode=" + mode); var googleUrl = new Uri(sbUrl.ToString()); var g = AppStateSettings.Instance.AddDownload(googleUrl.ToString(), "routing"); try { var request = (HttpWebRequest)WebRequest.Create(googleUrl); var response = (HttpWebResponse)request.GetResponse(); using (var reader = new StreamReader(response.GetResponseStream())) { var gResponse = reader.ReadToEnd(); dynamic directions = Newtonsoft.Json.JsonConvert.DeserializeObject(gResponse); var route = directions.routes[0]; string points = route.overview_polyline.points; var totMeters = 0; var totSeconds = 0; foreach (var leg in route.legs) { int result; string d = leg.distance.value; if (int.TryParse(d, NumberStyles.Number, CultureInfo.InvariantCulture, out result)) totMeters += result; string s = leg.duration.value; if (int.TryParse(s, NumberStyles.Number, CultureInfo.InvariantCulture, out result)) totSeconds += result; } var gd = new GoogleDirections { Directions = new Directions { Distance = new Distance { //html = leg.distance.text, meters = totMeters.ToString(CultureInfo.InvariantCulture) }, Duration = new Duration { //html = leg.duration.text, seconds = totSeconds.ToString(CultureInfo.InvariantCulture) }, SummaryHtml = route.summary.Value, Polyline = new Polyline {points = points} } }; return gd; } } catch (Exception e) { Logger.Log("Routing Tool", "Error downloading routing info", e.Message + ": " + googleUrl, Logger.Level.Error); } finally { AppStateSettings.Instance.FinishDownload(g); } return null; }
void GeoTimer_Elapsed(object sender, ElapsedEventArgs e) { var web = new WebMercator(); var mps = web.ToGeographic(_start.Geometry) as MapPoint; var mpf = web.ToGeographic(_finish.Geometry) as MapPoint; if ( (Moved && lastUpdated.AddSeconds(1) < DateTime.Now) && (startTime.AddSeconds(3) < DateTime.Now) && (start == null || end == null || start.X != mps.X || start.Y != mps.Y || end.X != mpf.X || end.Y != mpf.Y)) { start = mps; end = mpf; lastUpdated = DateTime.Now; var gd = DirectionsUtils.GetDirections(mps.Y.ToString(CultureInfo.InvariantCulture) + "," + mps.X.ToString(CultureInfo.InvariantCulture), mpf.Y.ToString(CultureInfo.InvariantCulture) + "," + mpf.X.ToString(CultureInfo.InvariantCulture), mode = Mode); if (gd != null) { if (gd.Directions != null) { UpdateLine(gd); } Directions = gd; } Moved = false; } if (Moved && lastDirection != null) { //UpdateLine(lastDirection); } }