0

I am trying to get a redirect from Google Maps using a project that is currently only at .Net Framework 4.0.

Take Microsoft's mailing address as an example:

1 Microsoft Way, Redmond, WA

Format a string that replaces Spaces , Commas , with Plus + signs:

https://www.google.com/maps?daddr=1+microsoft+way+redmond+wa

By the time Google finishes processing the page, the final URL has the Latitude and Longitude:

https://www.google.com/maps/dir//1+Microsoft+Way,+Redmond,+WA+98052/@47.6393095,-122.1327333,16z/data=!3m1!4b1!4m8!4m7!1m0!1m5!1m1!1s0x54906d73f7de150b:0x922499d19305e9a0!2m2!1d-122.1283559!2d47.6393096

I need the Latitude and Longitude from this URL.

I found this article already on SO:

Get a collection of redirected URLs from HttpWebResponse

Using the solution there, I created this very similar looking method:

private String latitude, longitude;

private void GetLatLongFromAddress(String p_street_name, String p_city, String p_state, String p_postal_code)
{
    var street_name = String.Format("{0}", p_street_name.Replace(' ', '+')).Trim();
    var city = String.Format("{0}", p_city).Trim();
    var state = String.Format("{0}", p_state).Trim();
    var postal_code = String.Format("{0}", p_postal_code).Trim();
    var mapUrl = String.Format("http://maps.google.com/?daddr={0}+{1}+{2}+{3}",
                               street_name, city, state, postal_code);
    var location = String.Copy(mapUrl);
    while (!String.IsNullOrEmpty(location))
    {
        var req1 = (HttpWebRequest)HttpWebRequest.Create(location);
        req1.AllowAutoRedirect = false;
        // give page time to redirect?
        // System.Threading.Thread.Sleep(5000);
        using (var resp = (HttpWebResponse)req1.GetResponse())
        {
            location = resp.GetResponseHeader("Location");
            if (!String.IsNullOrEmpty(location))
            {
                var postalIndex = location.IndexOf(postal_code);
                var slashAtIndex = location.IndexOf("/@") + 1;
                if ((postalIndex < slashAtIndex) && (slashAtIndex < location.Length))
                {
                    var split = location.Substring(slashAtIndex).Split(',');
                    latitude = split[0];
                    longitude = split[1];
                }
            }
        }
    }
}

You can see where I format the original URL with this line:

var mapUrl = String.Format("http://maps.google.com/?daddr={0}+{1}+{2}+{3}",
                          street_name, city, state, postal_code);

The problem is with this line:

location = resp.GetResponseHeader("Location");

Every time I get the response, the returned location value is the same as the mapUrl value that I started with because Google does not redirect it immediately.

I tried adding a Sleep(5000) call, but that did not change anything.

How would I go about getting the final redirect?

3
  • 1
    I think your problem lies in the fact that Google does the redirect via Javascript rather than directly from the HTTP request. Commented May 4, 2016 at 18:15
  • @drz, that's possible. Is there a way to get the redirect using Javascript instead of the HTTP request? I'll go search on that now, but if you know something please share. Commented May 4, 2016 at 18:37
  • 1
    I don't know of any direct way to deal with it. You would need something that could parse and process JS, which isn't easy Commented May 4, 2016 at 18:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.