none
Google Maps Problem: Can not find the distance and time? RRS feed

  • Question

  • Hi!

    I have to determine the distance and the time between two points via Google Maps ...
    Until recently everything went smoothly ...
    Somehow Google has tinkered with the APIs :-(

    can someone tell me the "now" correct call?

    Here is my old call:

            public void GetDistance()
            {
    
                EntfernungText = "";
                EntfernungMeter = "";
                ZeitraumText = "";
                ZeitraumMinuten = "";
    
    //      0 = Auto(Driving), 1 = Bahn(Transit), 2 = Fahhrad(Bicycling), 3 = Zu Fuß(Walking), 4 = Flugzeug
    //        public String Verkehrsmittel;
                string bewegenMit;
                switch (Convert.ToInt32(Verkehrsmittel))
                {
                    case 0:
                        bewegenMit = "&mode=Driving";
                        break;
                    case 1:
                        bewegenMit = "&mode=Transit";
                        break;
                    case 2:
                        bewegenMit = "&mode=Bicycling";
                        break;
                    case 3:
                        bewegenMit = "&mode=Walking";
                        break;
                    default:
                        bewegenMit = "&mode=Driving";
                        break;
                }
    
                WebRequest request = WebRequest.Create("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + Adresse.Trim() + bewegenMit.Trim() + "&sensor=false");
                IWebProxy proxy = request.Proxy;
                if (proxy != null)
                {
                    string proxyuri = proxy.GetProxy(request.RequestUri).ToString();
                    request.UseDefaultCredentials = true;
                    request.Proxy = new WebProxy(proxyuri, false);
                    request.Proxy.Credentials = CredentialCache.DefaultCredentials;
                }
                using (WebResponse response = request.GetResponse())
                {
                    Stream dataStream = response.GetResponseStream();
                    using (StreamReader sreader = new StreamReader(dataStream))
                    {
                        string xmlResponse = sreader.ReadToEnd();
                        using (XmlReader xreader = XmlReader.Create(new StringReader(xmlResponse)))
                        {
                            xreader.ReadToFollowing("status");
                            string status1 = xreader.ReadElementContentAsString();
    
                            // If the first 'Status' field != OK, probably the daily request limit has been reached (when using the free API)
                            if (status1 == "OK")
                            {
                                xreader.ReadToFollowing("status");
                                string status2 = xreader.ReadElementContentAsString();
    
                                if (status2 == "OK")
                                {
    
                                    xreader.ReadToFollowing("duration");
                                    xreader.ReadToFollowing("value");
                                    ZeitraumMinuten = xreader.ReadElementContentAsString();
    
                                    int Min = Convert.ToInt32(ZeitraumMinuten);
                                    ZeitraumText = "" + TimeSpan.FromSeconds(Min);
                                    string[] buffer = ZeitraumText.Split(':');
                                    if (Convert.ToInt32(buffer[2]) > 50)
                                    {
                                        int neu = Convert.ToInt32(buffer[1]);
                                        neu++;
                                        buffer[1] = neu.ToString();
                                    }
                                    if (buffer[0] == "00")
                                    {
                                        if (buffer[1] == "00")
                                        {
                                            buffer[1] = "01";
                                        }
                                        ZeitraumText = buffer[1].TrimStart('0') + " Minuten";
                                    }
                                    else
                                    {
                                        ZeitraumText = buffer[0].TrimStart('0') + " Stunde, " + buffer[1] + " Minuten";
                                    }
                                    xreader.ReadToFollowing("distance");
                                    xreader.ReadToFollowing("value");
                                    EntfernungMeter = xreader.ReadElementContentAsString();
    
                                    decimal meter = Convert.ToDecimal(EntfernungMeter);
    
                                    if (meter < 1000)
                                    {
                                        EntfernungText = meter.ToString().TrimStart('0') + " Meter";
                                    }
                                    else
                                    {
                                        decimal Kilometer = meter / 1000;
                                        decimal Kilometer2 = Math.Round(Kilometer);
                                        EntfernungText = Kilometer2.ToString() + " KM";
                                    }
    
                                }
                                else
                                {
                                    // Address is probably unknown, send to error buffer
                                }
                            }
    
                        }
                    
    
                    }
                   
                }
            }

    Best Regards

    Bernd

    • Moved by CoolDadTx Tuesday, March 6, 2018 6:49 PM Third party product
    Tuesday, March 6, 2018 3:46 PM

Answers

All replies

  • These forums are for questions related to MS products only. For questions related to Google Maps you'll need to post in their forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, March 6, 2018 6:48 PM
  • I got it :-)
    • Marked as answer by Bernd Riemke Tuesday, June 19, 2018 7:35 PM
    Tuesday, June 19, 2018 7:35 PM