コード例 #1
0
ファイル: ucVEarth.cs プロジェクト: CooLMoRTaL/VEUserControl
        /// <summary>
        /// Initiate route calculations
        /// </summary>
        /// <param name="locations">The waypoints of the route (max 25)</param>
        /// <param name="displayRoute">Display the route on the map yes or no</param>
        /// <param name="setBestMapView">Center the map to have best view of the route yes or no</param>
        /// <param name="routeMode">Walking or driving directions</param>
        /// <param name="distanceUnit">Miles or kilometers</param>
        /// <param name="routeOptimizeFactor">Optimize for minimal time or minimal distance</param> 
        /// <returns>RouteDirection object </returns> 
        /// <remarks>If the routeOptimizeFactor is set to Time and the RouteMode is set to walking, then an empty object is returned.</remarks> 
        public RouteDirections VE_GetDirections(List<SearchLocation> locations, bool displayRoute, bool setBestMapView, RouteModeEnum routeMode, DistanceUnitEnum distanceUnit, RouteOptimizeEnum routeOptimizeFactor)
        {
            List<SearchLocation> locationsWithLatLongCoordinates = new List<SearchLocation>();
            List<SearchLocation> locationsWithoutLatLongCoordinates = new List<SearchLocation>();

            if (locations.Count > 25)
            {
                throw new Exception("Maximum number of waypoints in a route is 25.");
            }

            // split locations in those with and without latitude and longtidude information
            foreach (SearchLocation location in locations)
            {
                if (location.Latitude != null && location.Longitude != null)
                {
                    locationsWithLatLongCoordinates.Add(location);
                }
                else
                {
                    locationsWithoutLatLongCoordinates.Add(location);
                }
            }

            // if there are locations without latitude and Longitude information, find the lat/long coordinates using where or what
            if (locationsWithoutLatLongCoordinates.Count > 0)
            {
                VE_FindLocations(locationsWithoutLatLongCoordinates, false, false, null);

                while (!finishedFindingLocations)
                {
                    Application.DoEvents();
                }

                locationsWithLatLongCoordinates.AddRange(this.locations);
            }

            vEarthBrowser.Document.InvokeScript("clearRouteWaypoints");

            // add the waypoints by latitude and Longitude
            foreach (SearchLocation searchLocation in locationsWithLatLongCoordinates)
            {
                if (searchLocation.Latitude != null && searchLocation.Longitude != null)
                {
                    vEarthBrowser.Document.InvokeScript("addRouteWaypoint",
                        new object[]
                        {
                            searchLocation.Longitude,
                            searchLocation.Latitude
                        });
                }
            }

            routeDirections = null;

            object waypoints = vEarthBrowser.Document.InvokeScript("getDirections", new object[] { displayRoute, setBestMapView, (int)routeMode, distanceUnit, (int)routeOptimizeFactor });

            // wait for asynchronous call to getDirections callback function to finish
            while (routeDirections == null)
            {
                Application.DoEvents();
            }

            return routeDirections;
        }
コード例 #2
0
ファイル: ucVEarth.cs プロジェクト: CooLMoRTaL/VEUserControl
        /// <summary>
        /// Callback function for VE_GetDirections
        /// </summary>
        /// <param name="time">Total time in seconds</param>
        /// <param name="distance">Total distance in miles or kilometers</param>
        /// <param name="description">Route description</param>
        public void OnDirectionsFinished(object duration, object distance, object description)
        {
            System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
            nfi.NumberDecimalSeparator = ".";

            routeDirections = new RouteDirections(double.Parse(duration.ToString()), decimal.Parse(distance.ToString(), nfi));

            foreach (string routeLeg in ((string)description).Trim('|').Split('|'))
            {
                string[] routeLegDetails = routeLeg.Split('~');
                routeDirections.AddRouteLeg(new RouteLeg(routeLegDetails[0], double.Parse(routeLegDetails[2]), decimal.Parse(routeLegDetails[1], nfi)));
            }
        }