示例#1
0
        public void DrawRouteFromUserLocation(string i_Location)
        {
            PointLatLng location = GetLocationByName(i_Location);

            if (location.IsEmpty)
            {
                throw new ArgumentException("Could not find the location you entered");
            }
            else
            {
                if (UserLocation != PointLatLng.Empty)
                {
                    GDirections          directions;
                    DirectionsStatusCode directionStatus = GoogleMapProvider.Instance.GetDirections(out directions, UserLocation, location, false, false, false, false, true);
                    if (directions != null)
                    {
                        GMapRoute mapRoute = new GMapRoute(directions.Route, "route");
                        r_routesOverlay.Routes.Clear();
                        r_routesOverlay.Routes.Add(mapRoute);
                        MapControl.Overlays.Remove(r_routesOverlay);
                        MapControl.Overlays.Add(r_routesOverlay);
                    }
                    else
                    {
                        throw new Exception("There is no route between these points");
                    }
                }

                MapControl.Position = location;
                MapControl.Zoom     = 15;
            }
        }
示例#2
0
        public void GerarRota()
        {
            DatabaseContext DbGMap = new DatabaseContext();

            GMapOverlay.Markers.Clear();
            GMapOverlay.Routes.Clear();

            List <IPassageiro> ListaPassageiro = DbGMap.Passageiros.ToList();

            if (DbGMap.Fretamentos.Count() > 0)
            {
                foreach (IFretamento IFretamento in DbGMap.Fretamentos)
                {
                    List <PointLatLng> WAYPOINTS = new List <PointLatLng>();

                    Random RANDOM      = new Random();
                    Color  RAMDOMCOLOR = Color.FromArgb(RANDOM.Next(256), RANDOM.Next(256), RANDOM.Next(256));

                    GMarkerGoogle ORIGEM = new GMarkerGoogle(IFretamento.I5_ORG_COORDENADAS, GMarkerGoogleType.MARKER_BEGIN, RAMDOMCOLOR, string.Empty);
                    GMapOverlay.Markers.Add(ORIGEM);
                    WAYPOINTS.Add(ORIGEM.Position);

                    if (!string.IsNullOrEmpty(IFretamento.I5_PASSAGEIROS))
                    {
                        foreach (IPassageiro IPassageiro in ListaPassageiro.Where(x => IFretamento.I5_PASSAGEIROS.Contains(x.I0_ID.ToString().PadLeft(6, '0'))))
                        {
                            GMarkerGoogle WAYPOINT = new GMarkerGoogle(IPassageiro.I0_COORDENADAS, GMarkerGoogleType.MARKER_WAYPOINT, RAMDOMCOLOR, string.Empty);
                            GMapOverlay.Markers.Add(WAYPOINT);
                            WAYPOINTS.Add(WAYPOINT.Position);
                        }
                    }

                    GMarkerGoogle DESTINO = new GMarkerGoogle(IFretamento.I5_DEST_COORDENADAS, GMarkerGoogleType.MARKER_END, RAMDOMCOLOR, string.Empty);
                    GMapOverlay.Markers.Add(DESTINO);
                    WAYPOINTS.Add(DESTINO.Position);

                    GDirections          GDirections          = new GDirections();
                    DirectionsStatusCode DirectionsStatusCode = DirectionsStatusCode.UNKNOWN_ERROR;
                    DirectionsStatusCode = GoogleMapProvider.Instance.GetDirections(out GDirections, WAYPOINTS.First(), WAYPOINTS.Where(x => x != WAYPOINTS.First() && x != WAYPOINTS.Last()).ToList(), WAYPOINTS.Last(), false, true, false, false, true);
                    if (DirectionsStatusCode != DirectionsStatusCode.OK)
                    {
                    }

                    if (DirectionsStatusCode == DirectionsStatusCode.OK)
                    {
                        GMapRoute GMapRoute = new GMapRoute(GDirections.Route, "Rota Planejada");
                        GMapRoute.Stroke       = (Pen)GMapRoute.Stroke.Clone();
                        GMapRoute.Stroke.Color = RAMDOMCOLOR;
                        GMapRoute.Stroke.Width = 5;
                        GMapOverlay.Routes.Add(GMapRoute);
                        GMapControl.Refresh();
                    }
                }
            }

            GMapControl.Refresh();
        }
示例#3
0
        static public GDirections GetDiraction(PointLatLng start, PointLatLng finish)
        {
            DirectionsStatusCode statucCode = DirectionsStatusCode.NOT_FOUND;

            while (statucCode != DirectionsStatusCode.OK)
            {
                statucCode = GMapProviders.GoogleMap.GetDirections(out m_gDiractiaon, start, finish, true, true, false, false, true);
            }

            return(m_gDiractiaon);
        }
示例#4
0
        static public double GetRouteDistance(PointLatLng start, PointLatLng finish)
        {
            DirectionsStatusCode statucCode = DirectionsStatusCode.NOT_FOUND;

            while (statucCode != DirectionsStatusCode.OK)
            {
                statucCode = GMapProviders.GoogleMap.GetDirections(out m_gDiractiaon, start, finish, true, true, false, false, true);
            }

            string distance = Helper.DeleteLetterFromString(m_gDiractiaon.Distance);

            return(double.Parse(distance, CultureInfo.InvariantCulture));
        }
        /// <summary>
        /// Thêm đường vào bản đồ và lấy độ dài của đoạn đường
        /// </summary>
        /// <param name="start">Điểm bắt đầu</param>
        /// <param name="end">Điểm kết thúc</param>
        /// <param name="color">mầu</param>
        /// <returns>KM</returns>
        private KeyValuePair <double, string> AddRoute(PointLatLng start, PointLatLng end, Color color)
        {
            GDirections          direction;
            DirectionsStatusCode directionStatus = GMapProviders.GoogleMap.GetDirections(out direction, start, end, false, false, false, false, false);
            GMapRoute            route           = new GMapRoute(direction.Route, "MyRoute");

            if (route.Points != null && route.Points.Count > 0)
            {
                MainMap.AddRoute(route, CommonUtils.SetTransparency(100, color));
            }
            else
            {
                throw new Exception("Không tìm thấy đường");
            }
            return(new KeyValuePair <double, string>(route.Distance, route.Name));
        }
示例#6
0
 /// <summary>
 // Thêm đường vào bản đồ và lấy độ dài của đoạn đườn        /// </summary>
 /// <param name="start">Điểm bắt đầu</param>
 /// <param name="end">Điểm kết thúc</param>
 /// <param name="color">mầu</param>
 /// <returns>KM</returns>
 private KeyValuePair <double, string> AddRoute(PointLatLng start, PointLatLng end, Color color)
 {
     try
     {
         GDirections          direction;
         DirectionsStatusCode directionStatus = GMapProviders.GoogleMap.GetDirections(out direction, start, end, avoidHighways, false, false, false, false);
         GMapRoute            route           = new GMapRoute(direction.Route, "MyRoute");
         if (route.Points != null && route.Points.Count > 0)
         {
             MainMap.AddRoute(route, CommonUtils.SetTransparency(100, color));
         }
         else
         {
             lblMsg.Text = "Không tìm thấy đường";
         }
         return(new KeyValuePair <double, string>(route.Distance, route.Name));
     }
     catch (Exception ex)
     {
         lblMsg.Text = "Không tìm thấy đường";
         LogError.WriteLogError("AddRoute:", ex);
         return(new KeyValuePair <double, string>(0, ""));
     }
 }
        /// <summary>
        /// NotImplemented
        /// </summary>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="avoidHighways"></param>
        /// <param name="avoidTolls"></param>
        /// <param name="walkingMode"></param>
        /// <param name="sensor"></param>
        /// <param name="metric"></param>
        /// <returns></returns>
        public IEnumerable<GDirections> GetDirections(out DirectionsStatusCode status, PointLatLng start, PointLatLng end, bool avoidHighways, bool avoidTolls, bool walkingMode, bool sensor, bool metric)
        {
            // TODO: add alternative directions

            throw new NotImplementedException();
        }
示例#8
0
        DirectionsStatusCode GetDirectionsUrl(string url, out GDirections direction)
        {
            DirectionsStatusCode ret = DirectionsStatusCode.UNKNOWN_ERROR;

            direction = null;

            try
            {
                string route = GMaps.Instance.UseRouteCache
                    ? Cache.Instance.GetContent(url, CacheType.DirectionsCache, TimeSpan.FromHours(TTLCache))
                    : string.Empty;
                if (string.IsNullOrEmpty(route))
                {
                    route = GetContentUsingHttp(url);
                    if (!string.IsNullOrEmpty(route))
                    {
                        if (GMaps.Instance.UseRouteCache)
                        {
                            Cache.Instance.SaveContent(url, CacheType.DirectionsCache, route);
                        }
                    }
                }

                #region -- gpx response --

                //<?xml version="1.0" encoding="UTF-8"?>
                //<gpx creator="" version="1.1" xmlns="http://www.topografix.com/GPX/1/1"
                //    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                //    xsi:schemaLocation="http://www.topografix.com/GPX/1/1 gpx.xsd ">
                //    <extensions>
                //        <distance>293</distance>
                //        <time>34</time>
                //        <start>Perckhoevelaan</start>
                //        <end>Goudenregenlaan</end>
                //    </extensions>
                //    <wpt lat="51.17702" lon="4.39630" />
                //    <wpt lat="51.17656" lon="4.39655" />
                //    <wpt lat="51.17639" lon="4.39670" />
                //    <wpt lat="51.17612" lon="4.39696" />
                //    <wpt lat="51.17640" lon="4.39767" />
                //    <wpt lat="51.17668" lon="4.39828" />
                //    <wpt lat="51.17628" lon="4.39874" />
                //    <wpt lat="51.17618" lon="4.39888" />
                //    <rte>
                //        <rtept lat="51.17702" lon="4.39630">
                //            <desc>Head south on Perckhoevelaan, 0.1 km</desc>
                //            <extensions>
                //                <distance>111</distance>
                //                <time>13</time>
                //                <offset>0</offset>
                //                <distance-text>0.1 km</distance-text>
                //                <direction>S</direction>
                //                <azimuth>160.6</azimuth>
                //            </extensions>
                //        </rtept>
                //        <rtept lat="51.17612" lon="4.39696">
                //            <desc>Turn left at Laarstraat, 0.1 km</desc>
                //            <extensions>
                //                <distance>112</distance>
                //                <time>13</time>
                //                <offset>3</offset>
                //                <distance-text>0.1 km</distance-text>
                //                <direction>NE</direction>
                //                <azimuth>58.1</azimuth>
                //                <turn>TL</turn>
                //                <turn-angle>269.0</turn-angle>
                //            </extensions>
                //        </rtept>
                //        <rtept lat="51.17668" lon="4.39828">
                //            <desc>Turn right at Goudenregenlaan, 70 m</desc>
                //            <extensions>
                //                <distance>70</distance>
                //                <time>8</time>
                //                <offset>5</offset>
                //                <distance-text>70 m</distance-text>
                //                <direction>SE</direction>
                //                <azimuth>143.4</azimuth>
                //                <turn>TR</turn>
                //                <turn-angle>89.8</turn-angle>
                //            </extensions>
                //        </rtept>
                //    </rte>
                //</gpx>

                #endregion

                if (!string.IsNullOrEmpty(route))
                {
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.LoadXml(route);
                    XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmldoc.NameTable);
                    xmlnsManager.AddNamespace("sm", "http://www.topografix.com/GPX/1/1");

                    XmlNodeList wpts = xmldoc.SelectNodes("/sm:gpx/sm:wpt", xmlnsManager);
                    if (wpts != null && wpts.Count > 0)
                    {
                        ret = DirectionsStatusCode.OK;

                        direction       = new GDirections();
                        direction.Route = new List <PointLatLng>();

                        foreach (XmlNode w in wpts)
                        {
                            double lat = double.Parse(w.Attributes["lat"].InnerText, CultureInfo.InvariantCulture);
                            double lng = double.Parse(w.Attributes["lon"].InnerText, CultureInfo.InvariantCulture);
                            direction.Route.Add(new PointLatLng(lat, lng));
                        }

                        if (direction.Route.Count > 0)
                        {
                            direction.StartLocation = direction.Route[0];
                            direction.EndLocation   = direction.Route[direction.Route.Count - 1];
                        }

                        XmlNode n = xmldoc.SelectSingleNode("/sm:gpx/sm:metadata/sm:copyright/sm:license",
                                                            xmlnsManager);
                        if (n != null)
                        {
                            direction.Copyrights = n.InnerText;
                        }

                        n = xmldoc.SelectSingleNode("/sm:gpx/sm:extensions/sm:distance", xmlnsManager);
                        if (n != null)
                        {
                            direction.Distance = n.InnerText + "m";
                        }

                        n = xmldoc.SelectSingleNode("/sm:gpx/sm:extensions/sm:time", xmlnsManager);
                        if (n != null)
                        {
                            direction.Duration = n.InnerText + "s";
                        }

                        n = xmldoc.SelectSingleNode("/sm:gpx/sm:extensions/sm:start", xmlnsManager);
                        if (n != null)
                        {
                            direction.StartAddress = n.InnerText;
                        }

                        n = xmldoc.SelectSingleNode("/sm:gpx/sm:extensions/sm:end", xmlnsManager);
                        if (n != null)
                        {
                            direction.EndAddress = n.InnerText;
                        }

                        wpts = xmldoc.SelectNodes("/sm:gpx/sm:rte/sm:rtept", xmlnsManager);
                        if (wpts != null && wpts.Count > 0)
                        {
                            direction.Steps = new List <GDirectionStep>();

                            foreach (XmlNode w in wpts)
                            {
                                GDirectionStep step = new GDirectionStep();

                                double lat = double.Parse(w.Attributes["lat"].InnerText, CultureInfo.InvariantCulture);
                                double lng = double.Parse(w.Attributes["lon"].InnerText, CultureInfo.InvariantCulture);

                                step.StartLocation = new PointLatLng(lat, lng);

                                XmlNode nn = w.SelectSingleNode("sm:desc", xmlnsManager);
                                if (nn != null)
                                {
                                    step.HtmlInstructions = nn.InnerText;
                                }

                                nn = w.SelectSingleNode("sm:extensions/sm:distance-text", xmlnsManager);
                                if (nn != null)
                                {
                                    step.Distance = nn.InnerText;
                                }

                                nn = w.SelectSingleNode("sm:extensions/sm:time", xmlnsManager);
                                if (nn != null)
                                {
                                    step.Duration = nn.InnerText + "s";
                                }

                                direction.Steps.Add(step);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ret       = DirectionsStatusCode.EXCEPTION_IN_CODE;
                direction = null;
                Debug.WriteLine("GetDirectionsUrl: " + ex);
            }

            return(ret);
        }
示例#9
0
 /// <summary>
 ///     NotImplemented
 /// </summary>
 /// <param name="status"></param>
 /// <param name="start"></param>
 /// <param name="end"></param>
 /// <param name="avoidHighways"></param>
 /// <param name="avoidTolls"></param>
 /// <param name="walkingMode"></param>
 /// <param name="sensor"></param>
 /// <param name="metric"></param>
 /// <returns></returns>
 public IEnumerable <GDirections> GetDirections(out DirectionsStatusCode status, PointLatLng start,
                                                PointLatLng end, bool avoidHighways, bool avoidTolls, bool walkingMode, bool sensor, bool metric)
 {
     throw new NotImplementedException();
 }
示例#10
0
 /// <summary>
 /// NotImplemented
 /// </summary>
 /// <param name="status"></param>
 /// <param name="start"></param>
 /// <param name="end"></param>
 /// <param name="avoidHighways"></param>
 /// <param name="avoidTolls"></param>
 /// <param name="walkingMode"></param>
 /// <param name="sensor"></param>
 /// <param name="metric"></param>
 /// <returns></returns>
 public IEnumerable<GDirections> GetDirections(out DirectionsStatusCode status, string start, string end, bool avoidHighways, bool avoidTolls, bool walkingMode, bool sensor, bool metric)
 {
    throw new NotImplementedException();
 }
示例#11
0
        private void generate_btn_Click(object sender, EventArgs e)
        {
            MainMap.Visible = true;
            int order = 1;

            routes.Routes.Clear();
            objects.Markers.Clear();
            String[] startingAddress = new Franchisee().getAddress(userid);

            PointLatLng?       pos;
            GeoCoderStatusCode status = GeoCoderStatusCode.Unknow;

            {
                pos = GMapProviders.GoogleMap.GetPoint(startingAddress[2] + ", " + startingAddress[0], out status);
                if (pos != null && status == GeoCoderStatusCode.G_GEO_SUCCESS)
                {
                    currentMarker.Position = pos.Value;
                }
            }
            AddLocation(order++, startingAddress[0]);
            List <PointLatLng> myWaypoints = new List <PointLatLng>();

            if (workOrderTable.SelectedRows.Count > 0)
            {
                String[] address  = new String[workOrderTable.SelectedRows.Count];
                String[] location = new String[workOrderTable.SelectedRows.Count];
                String[] country  = new String[workOrderTable.SelectedRows.Count];
                for (int i = 0; i < workOrderTable.SelectedRows.Count; i++)
                {
                    country[i] = workOrderTable.SelectedRows[i].Cells[6].Value.ToString();
                    address[i] = workOrderTable.SelectedRows[i].Cells[3].Value.ToString();
//                    location[i] = workOrderTable.SelectedRows[i].Cells[6].Value.ToString() + ", " + workOrderTable.SelectedRows[i].Cells[4].Value.ToString();
                    location[i] = workOrderTable.SelectedRows[i].Cells[3].Value.ToString() + ", "
                                  + workOrderTable.SelectedRows[i].Cells[4].Value.ToString() + ", "
                                  + workOrderTable.SelectedRows[i].Cells[5].Value.ToString() + ", "
                                  + workOrderTable.SelectedRows[i].Cells[6].Value.ToString();
                }

                for (int i = 0; i < workOrderTable.SelectedRows.Count; i++)
                {
                    //MessageBox.Show(location[i].ToString());
                    PointLatLng?pos1 = GMapProviders.GoogleMap.GetPoint(location[i].ToString(), out status);
                    if (pos1 != null && status == GeoCoderStatusCode.G_GEO_SUCCESS)
                    {
                        myWaypoints.Add(pos1.Value);
                        AddLocation(order++, location[i]);
                        currentMarker.Position = pos1.Value;
                    }
                    else
                    {
                        myWaypoints.Add(new PointLatLng(42.98252, -81.25397));
                        AddLocation(order++, location[i]);
                        if (currentMarker != null)
                        {
                            currentMarker.Position = new PointLatLng(42.98252, -81.25397);
                        }
                    }
                }
            }
            if (servicesTable.SelectedRows.Count > 0)
            {
                String[] address  = new String[servicesTable.SelectedRows.Count];
                String[] location = new String[servicesTable.SelectedRows.Count];
                String[] country  = new String[servicesTable.SelectedRows.Count];
                for (int i = 0; i < servicesTable.SelectedRows.Count; i++)
                {
                    country[i]  = servicesTable.SelectedRows[i].Cells[8].Value.ToString();
                    address[i]  = servicesTable.SelectedRows[i].Cells[5].Value.ToString();
                    location[i] = servicesTable.SelectedRows[i].Cells[8].Value.ToString() + ", " + servicesTable.SelectedRows[i].Cells[6].Value.ToString();
                }

                for (int i = 0; i < servicesTable.SelectedRows.Count; i++)
                {
                    PointLatLng?pos1 = GMapProviders.GoogleMap.GetPoint(country[i] + ", " + address[i], out status);
                    myWaypoints.Add(pos1.Value);
                    AddLocation(order++, address[i]);
                    currentMarker.Position = pos1.Value;
                }
            }
            GMapRoute rte = new GMapRoute("name");

            GDirections _dir;

            if (pos == null)
            {
                pos = new PointLatLng();
            }
            DirectionsStatusCode _code = GMapProviders.GoogleMap.GetDirections(out _dir, pos.Value, myWaypoints, false, false, false, true, true);

            if (_code == DirectionsStatusCode.OK)
            {
                foreach (GDirectionStep _step in _dir.Steps)
                {
                    rte.Points.AddRange(_step.Points);
                }
            }

            routes.Routes.Add(rte);
        }