/// <summary> /// Возвращаем GPS координаты точки /// </summary> /// <param name="Point">Точка с координатами Яндекса</param> /// <returns>GPS координаты</returns> public static PointD ConvertToGPSPoint( PointD Point ) { PointD result = new PointD(); double integerX = Math.Truncate(Point.X); result.X = integerX + ( Point.X - integerX ) * 0.6; double integerY = Math.Truncate( Point.Y ); result.Y = integerY + ( Point.Y - integerY ) * 0.6; return result; }
private string GetJSCode(PointD[] RoutePoints) { string strRoutePoints = ""; foreach (PointD routePoint in RoutePoints) { string DotX = routePoint.X.ToString().Replace(",", "."); string DotY = routePoint.Y.ToString().Replace(",", "."); strRoutePoints += "[" + DotX + ", " + DotY + "],"; } strRoutePoints = strRoutePoints.Trim(new[] { ' ', ',' }); string jsCode = "ymaps.ready(init); " + "function init() { ymaps.route([" + strRoutePoints + "]).then(function (route) { " + "window.external.GetData(" + "route.getLength(), route.getJamsTime(), route.getTime(), " + "route.getHumanLength(), route.getHumanJamsTime(), route.getHumanTime()" + "); }, function (error) { window.external.GetDataError(); });" + " }"; return jsCode; }
public Route Load(PointD DotA, PointD DotB) { bool IsLoad = false; PointD[] RoutePoints = new PointD[] { DotA, DotB }; return Load(RoutePoints); }
public Route Load(PointD[] RoutePoints) { using (WebBrowser webBrowser = new WebBrowser()) { ScriptManager scriptManager = new ScriptManager(); webBrowser.ScriptErrorsSuppressed = true; webBrowser.ObjectForScripting = scriptManager; webBrowser.WebBrowserShortcutsEnabled = false; scriptManager.GetState = ScriptManager.State.Running; string jsCode = GetJSCode(RoutePoints); webBrowser.DocumentText = String.Format(htmlTemplate, jsCode); bool IsCheck = true; DateTime starTime = DateTime.Now; while (IsCheck) { switch (scriptManager.GetState) { case ScriptManager.State.Ok: Route route = new Route(); route.RouteId = Guid.NewGuid().ToString(); route.RouteLength = scriptManager.routeLength; route.RouteJamsTime = scriptManager.routeJamsTime; route.RouteTime = scriptManager.routeTime; route.RouteHumanLength = scriptManager.routeHumanLength; route.RouteHumanJamsTime = scriptManager.routeHumanJamsTime; route.RouteHumanTime = scriptManager.routeHumanTime; IsCheck = false; return route; case ScriptManager.State.Error: IsCheck = false; break; } // Выходим по TimeOut TimeSpan ts = DateTime.Now - starTime; if (ts.Seconds > requestTimeout) { IsCheck = false; } Thread.Sleep(100); Application.DoEvents(); } webBrowser.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } return null; }