Пример #1
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);
        }
Пример #2
0
        DirectionsStatusCode GetDirectionsUrl(string url, out GDirections direction)
        {
            DirectionsStatusCode ret = DirectionsStatusCode.UNKNOWN_ERROR;
            direction = null;

            try
            {
                string kml = GMaps.Instance.UseDirectionsCache ? Cache.Instance.GetContent(url, CacheType.DirectionsCache) : string.Empty;

                bool cache = false;

                if (string.IsNullOrEmpty(kml))
                {
                    kml = GetContentUsingHttp(url);
                    if (!string.IsNullOrEmpty(kml))
                    {
                        cache = true;
                    }
                }

                if (!string.IsNullOrEmpty(kml))
                {
                    #region -- kml response --
                    //<?xml version="1.0" encoding="UTF-8"?>
                    //<DirectionsResponse>
                    // <status>OK</status>
                    // <route>
                    //  <summary>A1/E85</summary>
                    //  <leg>
                    //   <step>
                    //    <travel_mode>DRIVING</travel_mode>
                    //    <start_location>
                    //     <lat>54.6893800</lat>
                    //     <lng>25.2800500</lng>
                    //    </start_location>
                    //    <end_location>
                    //     <lat>54.6907800</lat>
                    //     <lng>25.2798000</lng>
                    //    </end_location>
                    //    <polyline>
                    //     <points>soxlIiohyCYLkCJ}@Vs@?</points>
                    //    </polyline>
                    //    <duration>
                    //     <value>32</value>
                    //     <text>1 min</text>
                    //    </duration>
                    //    <html_instructions>Head &lt;b&gt;north&lt;/b&gt; on &lt;b&gt;Vilniaus gatvė&lt;/b&gt; toward &lt;b&gt;Tilto gatvė&lt;/b&gt;</html_instructions>
                    //    <distance>
                    //     <value>157</value>
                    //     <text>0.2 km</text>
                    //    </distance>
                    //   </step>
                    //   <step>
                    //    <travel_mode>DRIVING</travel_mode>
                    //    <start_location>
                    //     <lat>54.6907800</lat>
                    //     <lng>25.2798000</lng>
                    //    </start_location>
                    //    <end_location>
                    //     <lat>54.6942500</lat>
                    //     <lng>25.2621300</lng>
                    //    </end_location>
                    //    <polyline>
                    //     <points>kxxlIwmhyCmApUF`@GvAYpD{@dGcCjIoIvOuAhDwAtEa@vBUnDAhB?~AThDRxAh@hBtAdC</points>
                    //    </polyline>
                    //    <duration>
                    //     <value>133</value>
                    //     <text>2 mins</text>
                    //    </duration>
                    //    <html_instructions>Turn &lt;b&gt;left&lt;/b&gt; onto &lt;b&gt;A. Goštauto gatvė&lt;/b&gt;</html_instructions>
                    //    <distance>
                    //     <value>1326</value>
                    //     <text>1.3 km</text>
                    //    </distance>
                    //   </step>
                    //   <step>
                    //    <travel_mode>DRIVING</travel_mode>
                    //    <start_location>
                    //     <lat>54.6942500</lat>
                    //     <lng>25.2621300</lng>
                    //    </start_location>
                    //    <end_location>
                    //     <lat>54.6681200</lat>
                    //     <lng>25.2377500</lng>
                    //    </end_location>
                    //    <polyline>
                    //     <points>anylIi_eyC`AwD~@oBLKr@K`U|FdF|@`J^~E[j@Lh@\hB~Bn@tBZhBLrC?zIJ~DzA~OVrELlG^lDdAtDh@hAfApA`EzCvAp@jUpIpAl@bBpAdBpBxA|BdLpV`BxClAbBhBlBbChBpBhAdAXjBHlE_@t@?|@Lt@X</points>
                    //    </polyline>
                    //    <duration>
                    //     <value>277</value>
                    //     <text>5 mins</text>
                    //    </duration>
                    //    <html_instructions>Turn &lt;b&gt;left&lt;/b&gt; to merge onto &lt;b&gt;Geležinio Vilko gatvė&lt;/b&gt;</html_instructions>
                    //    <distance>
                    //     <value>3806</value>
                    //     <text>3.8 km</text>
                    //    </distance>
                    //   </step>
                    //   <step>
                    //    <travel_mode>DRIVING</travel_mode>
                    //    <start_location>
                    //     <lat>54.6681200</lat>
                    //     <lng>25.2377500</lng>
                    //    </start_location>
                    //    <end_location>
                    //     <lat>54.6584100</lat>
                    //     <lng>25.1411300</lng>
                    //    </end_location>
                    //    <polyline>
                    //     <points>wjtlI}f`yC~FhBlFr@jD|A~EbC~VjNxBbBdA`BnvA|zCba@l`Bt@tDTbBJpBBfBMvDaAzF}bBjiF{HnXiHxZ</points>
                    //    </polyline>
                    //    <duration>
                    //     <value>539</value>
                    //     <text>9 mins</text>
                    //    </duration>
                    //    <html_instructions>Continue onto &lt;b&gt;Savanorių prospektas&lt;/b&gt;</html_instructions>
                    //    <distance>
                    //     <value>8465</value>
                    //     <text>8.5 km</text>
                    //    </distance>
                    //   </step>
                    //   <step>
                    //    <travel_mode>DRIVING</travel_mode>
                    //    <start_location>
                    //     <lat>54.6584100</lat>
                    //     <lng>25.1411300</lng>
                    //    </start_location>
                    //    <end_location>
                    //     <lat>54.9358200</lat>
                    //     <lng>23.9260000</lng>
                    //    </end_location>
                    //    <polyline>
                    //     <points>anrlIakmxCiq@|qCuBbLcK~n@wUrkAcPnw@gCnPoQt}AoB`MuAdHmAdFoCtJqClImBxE{DrIkQ|ZcEvIkDzIcDhKyBxJ{EdXuCtS_G`g@mF|\eF`WyDhOiE~NiErMaGpOoj@ppAoE|K_EzKeDtKkEnOsLnd@mDzLgI~U{FrNsEvJoEtI_FpI{J`O_EjFooBf_C{GdJ_FjIsH`OoFhMwH`UcDtL{CzMeDlQmAzHuU~bBiArIwApNaBfWaLfiCoBpYsDf\qChR_FlVqEpQ_ZbfA}CfN{A~HwCtRiAfKmBlVwBx[gBfRcBxMaLdp@sXrzAaE~UqCzRyC`[_q@z|LgC|e@m@vNqp@b}WuLraFo@jPaS~bDmJryAeo@v|G}CnWsm@~`EoKvo@kv@lkEkqBrlKwBvLkNj|@cu@`~EgCnNuiBpcJakAx|GyB`KqdC~fKoIfYicAxtCiDrLu@hDyBjQm@xKoGdxBmQhoGuUn|Dc@nJ[`OW|VaEn|Ee@`X</points>
                    //    </polyline>
                    //    <duration>
                    //     <value>3506</value>
                    //     <text>58 mins</text>
                    //    </duration>
                    //    <html_instructions>Continue onto &lt;b&gt;A1/E85&lt;/b&gt;</html_instructions>
                    //    <distance>
                    //     <value>85824</value>
                    //     <text>85.8 km</text>
                    //    </distance>
                    //   </step>
                    //   <step>
                    //    <travel_mode>DRIVING</travel_mode>
                    //    <start_location>
                    //     <lat>54.9358200</lat>
                    //     <lng>23.9260000</lng>
                    //    </start_location>
                    //    <end_location>
                    //     <lat>54.9376500</lat>
                    //     <lng>23.9195600</lng>
                    //    </end_location>
                    //    <polyline>
                    //     <points>{shnIo``qCQ^MnD[lBgA`DqBdEu@xB}@zJCjB</points>
                    //    </polyline>
                    //    <duration>
                    //     <value>39</value>
                    //     <text>1 min</text>
                    //    </duration>
                    //    <html_instructions>Take the exit toward &lt;b&gt;Senamiestis/Aleksotas&lt;/b&gt;</html_instructions>
                    //    <distance>
                    //     <value>476</value>
                    //     <text>0.5 km</text>
                    //    </distance>
                    //   </step>
                    //   <step>
                    //    <travel_mode>DRIVING</travel_mode>
                    //    <start_location>
                    //     <lat>54.9376500</lat>
                    //     <lng>23.9195600</lng>
                    //    </start_location>
                    //    <end_location>
                    //     <lat>54.9361300</lat>
                    //     <lng>23.9189700</lng>
                    //    </end_location>
                    //    <polyline>
                    //     <points>i_inIgx~pCnHtB</points>
                    //    </polyline>
                    //    <duration>
                    //     <value>28</value>
                    //     <text>1 min</text>
                    //    </duration>
                    //    <html_instructions>Turn &lt;b&gt;left&lt;/b&gt; onto &lt;b&gt;Kleboniškio gatvė&lt;/b&gt;</html_instructions>
                    //    <distance>
                    //     <value>173</value>
                    //     <text>0.2 km</text>
                    //    </distance>
                    //   </step>
                    //   <step>
                    //    <travel_mode>DRIVING</travel_mode>
                    //    <start_location>
                    //     <lat>54.9361300</lat>
                    //     <lng>23.9189700</lng>
                    //    </start_location>
                    //    <end_location>
                    //     <lat>54.9018900</lat>
                    //     <lng>23.8937000</lng>
                    //    </end_location>
                    //    <polyline>
                    //     <points>yuhnIqt~pCvAb@JLrOvExSdHvDdAv`@pIpHnAdl@hLdB`@nDvAtEjDdCvCjLzOvAzBhC`GpHfRbQd^`JpMPt@ClA</points>
                    //    </polyline>
                    //    <duration>
                    //     <value>412</value>
                    //     <text>7 mins</text>
                    //    </duration>
                    //    <html_instructions>Continue onto &lt;b&gt;Jonavos gatvė&lt;/b&gt;</html_instructions>
                    //    <distance>
                    //     <value>4302</value>
                    //     <text>4.3 km</text>
                    //    </distance>
                    //   </step>
                    //   <step>
                    //    <travel_mode>DRIVING</travel_mode>
                    //    <start_location>
                    //     <lat>54.9018900</lat>
                    //     <lng>23.8937000</lng>
                    //    </start_location>
                    //    <end_location>
                    //     <lat>54.8985600</lat>
                    //     <lng>23.8933400</lng>
                    //    </end_location>
                    //    <polyline>
                    //     <points>y_bnIsvypCMf@FnARlAf@zAl@^v@EZ_@pAe@x@k@xBPpA@pAQNSf@oB</points>
                    //    </polyline>
                    //    <duration>
                    //     <value>69</value>
                    //     <text>1 min</text>
                    //    </duration>
                    //    <html_instructions>At the roundabout, take the &lt;b&gt;3rd&lt;/b&gt; exit and stay on &lt;b&gt;Jonavos gatvė&lt;/b&gt;</html_instructions>
                    //    <distance>
                    //     <value>478</value>
                    //     <text>0.5 km</text>
                    //    </distance>
                    //   </step>
                    //   <step>   
                    //    <travel_mode>DRIVING</travel_mode>
                    //    <start_location>
                    //     <lat>54.8985600</lat>
                    //     <lng>23.8933400</lng>
                    //    </start_location>
                    //    <end_location>
                    //     <lat>54.8968500</lat>
                    //     <lng>23.8930000</lng>
                    //    </end_location>
                    //    <polyline>
                    //     <points>_kanIktypCbEx@pCH</points>
                    //    </polyline>
                    //    <duration>
                    //     <value>38</value>
                    //     <text>1 min</text>
                    //    </duration>
                    //    <html_instructions>Turn &lt;b&gt;right&lt;/b&gt; onto &lt;b&gt;A. Mapu gatvė&lt;/b&gt;&lt;div style=&quot;font-size:0.9em&quot;&gt;Destination will be on the right&lt;/div&gt;</html_instructions>
                    //    <distance>
                    //     <value>192</value>
                    //     <text>0.2 km</text>
                    //    </distance>
                    //   </step>
                    //   <duration>
                    //    <value>5073</value>
                    //    <text>1 hour 25 mins</text>
                    //   </duration>
                    //   <distance>
                    //    <value>105199</value>
                    //    <text>105 km</text>
                    //   </distance>
                    //   <start_location>
                    //    <lat>54.6893800</lat>
                    //    <lng>25.2800500</lng>
                    //   </start_location>
                    //   <end_location>
                    //    <lat>54.8968500</lat>
                    //    <lng>23.8930000</lng>
                    //   </end_location>
                    //   <start_address>Vilnius, Lithuania</start_address>
                    //   <end_address>Kaunas, Lithuania</end_address>
                    //  </leg>
                    //  <copyrights>Map data ©2011 Tele Atlas</copyrights>
                    //  <overview_polyline>
                    //   <points>soxlIiohyCYL}Fb@mApUF`@GvAYpD{@dGcCjIoIvOwBpFy@xC]jBSxCC~E^~Er@lCtAdC`AwD~@oB`AW`U|FdF|@`J^~E[tAj@hB~BjA~ELrCJzOzA~Od@`N^lDdAtDt@xAjAnApDlCbXbKpAl@bBpAdBpBxA|BdLpV`BxCvDpEbChBpBhAdAXjBHbG_@|@LtHbClFr@jK`F~VjNxBbB`@h@rwAt|Cba@l`BjAxGNxEMvDaAzF}bBjiFcFbQ_y@|gD{CxMeBnJcK~n@wh@dkCkAlIoQt}AeEfV}EzQqClImBxE{DrIkQ|ZcEvIkDzIcDhKyBxJ{EdXuCtS_G`g@mF|\eF`WyDhOiE~NiErMaGpOoj@ppAoE|K_EzKeDtKmXzbAgI~U{FrNsEvJoLfT{J`O_EjFooBf_C{GdJkLtSwI`SyClI}CrJcDtL{CzMeDlQcXzlBiArIwApNaBfWaLfiCoBpYsDf\qChR_FlVqEpQ_ZbfAyFfXwCtRiAfKeFfs@gBfRcBxMaLdp@sXrzAaE~UqCzRyC`[_q@z|LuDtu@qp@b}WuLraFo@jPo^r}Faq@pfHaBtMsm@~`EoKvo@kv@lkEcuBjzKkNj|@cu@`~EgCnNuiBpcJakAx|GyB`KqdC~fKoIfYidAbwCoD|MeAbHcA|Im@xK}YnhKyV~gEs@~f@aEn|Ee@`XQ^MnD[lBoF`N}@zJCjBfKxCJLdj@bQv`@pIpHnAdl@hLdB`@nDvAtEjDdCvCbOvSzLhZbQd^`JpMPt@QtBFnAz@hDl@^j@?f@e@pAe@x@k@xBPfCEf@Uj@wBbEx@pCH</points>
                    //  </overview_polyline>
                    //  <bounds>
                    //   <southwest>
                    //    <lat>54.6389500</lat>
                    //    <lng>23.8920900</lng>
                    //   </southwest>
                    //   <northeast>
                    //    <lat>54.9376500</lat>
                    //    <lng>25.2800500</lng>
                    //   </northeast>
                    //  </bounds>
                    // </route>
                    //</DirectionsResponse> 
                    #endregion

                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(kml);

                    XmlNode nn = doc.SelectSingleNode("/DirectionsResponse/status");
                    if (nn != null)
                    {
                        ret = (DirectionsStatusCode)Enum.Parse(typeof(DirectionsStatusCode), nn.InnerText, false);
                        if (ret == DirectionsStatusCode.OK)
                        {
                            if (cache && GMaps.Instance.UseDirectionsCache)
                            {
                                Cache.Instance.SaveContent(url, CacheType.DirectionsCache, kml);
                            }

                            direction = new GDirections();

                            nn = doc.SelectSingleNode("/DirectionsResponse/route/summary");
                            if (nn != null)
                            {
                                direction.Summary = nn.InnerText;
                                Debug.WriteLine("summary: " + direction.Summary);
                            }

                            nn = doc.SelectSingleNode("/DirectionsResponse/route/leg/duration");
                            if (nn != null)
                            {
                                var t = nn.SelectSingleNode("text");
                                if (t != null)
                                {
                                    direction.Duration = t.InnerText;
                                    Debug.WriteLine("duration: " + direction.Duration);
                                }

                                t = nn.SelectSingleNode("value");
                                if (t != null)
                                {
                                    if (!string.IsNullOrEmpty(t.InnerText))
                                    {
                                        direction.DurationValue = uint.Parse(t.InnerText);
                                        Debug.WriteLine("value: " + direction.DurationValue);
                                    }
                                }
                            }

                            nn = doc.SelectSingleNode("/DirectionsResponse/route/leg/distance");
                            if (nn != null)
                            {
                                var t = nn.SelectSingleNode("text");
                                if (t != null)
                                {
                                    direction.Distance = t.InnerText;
                                    Debug.WriteLine("distance: " + direction.Distance);
                                }

                                t = nn.SelectSingleNode("value");
                                if (t != null)
                                {
                                    if (!string.IsNullOrEmpty(t.InnerText))
                                    {
                                        direction.DistanceValue = uint.Parse(t.InnerText);
                                        Debug.WriteLine("value: " + direction.DistanceValue);
                                    }
                                }
                            }

                            nn = doc.SelectSingleNode("/DirectionsResponse/route/leg/start_location");
                            if (nn != null)
                            {
                                var pt = nn.SelectSingleNode("lat");
                                if (pt != null)
                                {
                                    direction.StartLocation.Lat = double.Parse(pt.InnerText, CultureInfo.InvariantCulture);
                                }

                                pt = nn.SelectSingleNode("lng");
                                if (pt != null)
                                {
                                    direction.StartLocation.Lng = double.Parse(pt.InnerText, CultureInfo.InvariantCulture);
                                }
                            }

                            nn = doc.SelectSingleNode("/DirectionsResponse/route/leg/end_location");
                            if (nn != null)
                            {
                                var pt = nn.SelectSingleNode("lat");
                                if (pt != null)
                                {
                                    direction.EndLocation.Lat = double.Parse(pt.InnerText, CultureInfo.InvariantCulture);
                                }

                                pt = nn.SelectSingleNode("lng");
                                if (pt != null)
                                {
                                    direction.EndLocation.Lng = double.Parse(pt.InnerText, CultureInfo.InvariantCulture);
                                }
                            }

                            nn = doc.SelectSingleNode("/DirectionsResponse/route/leg/start_address");
                            if (nn != null)
                            {
                                direction.StartAddress = nn.InnerText;
                                Debug.WriteLine("start_address: " + direction.StartAddress);
                            }

                            nn = doc.SelectSingleNode("/DirectionsResponse/route/leg/end_address");
                            if (nn != null)
                            {
                                direction.EndAddress = nn.InnerText;
                                Debug.WriteLine("end_address: " + direction.EndAddress);
                            }

                            nn = doc.SelectSingleNode("/DirectionsResponse/route/copyrights");
                            if (nn != null)
                            {
                                direction.Copyrights = nn.InnerText;
                                Debug.WriteLine("copyrights: " + direction.Copyrights);
                            }

                            nn = doc.SelectSingleNode("/DirectionsResponse/route/overview_polyline/points");
                            if (nn != null)
                            {
                                direction.Route = new List<PointLatLng>();
                                DecodePointsInto(direction.Route, nn.InnerText);
                            }

                            XmlNodeList steps = doc.SelectNodes("/DirectionsResponse/route/leg/step");
                            if (steps != null)
                            {
                                if (steps.Count > 0)
                                {
                                    direction.Steps = new List<GDirectionStep>();
                                }

                                foreach (XmlNode s in steps)
                                {
                                    GDirectionStep step = new GDirectionStep();

                                    Debug.WriteLine("----------------------");
                                    nn = s.SelectSingleNode("travel_mode");
                                    if (nn != null)
                                    {
                                        step.TravelMode = nn.InnerText;
                                        Debug.WriteLine("travel_mode: " + step.TravelMode);
                                    }

                                    nn = s.SelectSingleNode("start_location");
                                    if (nn != null)
                                    {
                                        var pt = nn.SelectSingleNode("lat");
                                        if (pt != null)
                                        {
                                            step.StartLocation.Lat = double.Parse(pt.InnerText, CultureInfo.InvariantCulture);
                                        }

                                        pt = nn.SelectSingleNode("lng");
                                        if (pt != null)
                                        {
                                            step.StartLocation.Lng = double.Parse(pt.InnerText, CultureInfo.InvariantCulture);
                                        }
                                    }

                                    nn = s.SelectSingleNode("end_location");
                                    if (nn != null)
                                    {
                                        var pt = nn.SelectSingleNode("lat");
                                        if (pt != null)
                                        {
                                            step.EndLocation.Lat = double.Parse(pt.InnerText, CultureInfo.InvariantCulture);
                                        }

                                        pt = nn.SelectSingleNode("lng");
                                        if (pt != null)
                                        {
                                            step.EndLocation.Lng = double.Parse(pt.InnerText, CultureInfo.InvariantCulture);
                                        }
                                    }

                                    nn = s.SelectSingleNode("duration");
                                    if (nn != null)
                                    {
                                        nn = nn.SelectSingleNode("text");
                                        if (nn != null)
                                        {
                                            step.Duration = nn.InnerText;
                                            Debug.WriteLine("duration: " + step.Duration);
                                        }
                                    }

                                    nn = s.SelectSingleNode("distance");
                                    if (nn != null)
                                    {
                                        nn = nn.SelectSingleNode("text");
                                        if (nn != null)
                                        {
                                            step.Distance = nn.InnerText;
                                            Debug.WriteLine("distance: " + step.Distance);
                                        }
                                    }

                                    nn = s.SelectSingleNode("html_instructions");
                                    if (nn != null)
                                    {
                                        step.HtmlInstructions = nn.InnerText;
                                        Debug.WriteLine("html_instructions: " + step.HtmlInstructions);
                                    }

                                    nn = s.SelectSingleNode("polyline");
                                    if (nn != null)
                                    {
                                        nn = nn.SelectSingleNode("points");
                                        if (nn != null)
                                        {
                                            step.Points = new List<PointLatLng>();
                                            DecodePointsInto(step.Points, nn.InnerText);
                                        }
                                    }

                                    direction.Steps.Add(step);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                direction = null;
                ret = DirectionsStatusCode.ExceptionInCode;
                Debug.WriteLine("GetDirectionsUrl: " + ex);
            }
            return ret;
        }
Пример #3
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) : 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);
               System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.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.ExceptionInCode;
            direction = null;
            Debug.WriteLine("GetDirectionsUrl: " + ex);
         }

         return ret;
      }