locked
Using Google API to Determine Distance RRS feed

  • Question

  • User2080593117 posted

    Hello, I have a site that has been running for years without any issues. Just recently, one of the pages started giving me an error. This page determines the distance between 2 zip codes using Google Maps API. There page is here:

    http://actrucking.com/getquote.aspx

    You can see the error by simply typing 2 valid zip codes. Here is the code:

    Dim xmlReq As HttpWebRequest = WebRequest.Create(reqUrl)
    xmlReq.ContentType = "text/xml"
    xmlReq.Method = "POST"
    
    Dim poststr As String = "blah"                    
    Dim Distance As String
    Dim xd As XDocument = XDocument.Load(reqUrl)
    Distance = xd.Descendants("distance").FirstOrDefault().Element("text").Value

    The error occurs on the last line and all I get is, Object reference not set to an instance of an object.

    Monday, February 25, 2019 5:46 PM

All replies

  • User753101303 posted

    Hi,

    What if you just look at the XML document you get ??? More likely this is not any more the XML document you expect (edit: maybe the API key expired ?)

    Tuesday, February 26, 2019 4:06 PM
  • User2080593117 posted

    OK, sure, I can take a look. How can I display the XML doc?

    Tuesday, February 26, 2019 8:37 PM
  • User2080593117 posted

    I tried:

    Dim theXML As String
    theXML = Server.HtmlEncode(File.ReadAllText(xd.ToString))
    lit1.Text = theXML

    ...and I am getting "Illegal characters in path"

    Tuesday, February 26, 2019 8:51 PM
  • User475983607 posted

    Your code does not make sense.  Here your are trying to read a file from the HDD.

    Dim theXML As String
    theXML = Server.HtmlEncode(File.ReadAllText(xd.ToString))
    lit1.Text = theXML

    The error is telling that the value of xd.ToString is not a valid file path.

    This code...

    Dim xmlReq As HttpWebRequest = WebRequest.Create(reqUrl)
    xmlReq.ContentType = "text/xml"
    xmlReq.Method = "POST"
    
    Dim poststr As String = "blah"                    
    Dim Distance As String
    Dim xd As XDocument = XDocument.Load(reqUrl)
    Distance = xd.Descendants("distance").FirstOrDefault().Element("text").Value

    ..creates a WebRequest object that's never used.  The same reqUrl variable used in the WebRequest object is also used to load an XML response from a remote URL a local XML file?  Set a break point to figure out what reqUrl is.  At this point, there's no indication that you are actually invoking a Google API.

    Tuesday, February 26, 2019 9:10 PM
  • User2080593117 posted

    Sorry, missed one line of code at the top, here is everything:

    Dim reqUrl As String = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" & txtStartZip.Text & "&destinations=" & txtEndZip.Text & "&mode=driving&sensor=false&units=imperial"
    Dim xmlReq As HttpWebRequest = WebRequest.Create(reqUrl)
    xmlReq.ContentType = "text/xml"
    xmlReq.Method = "POST"
    Dim Distance As String
    Dim xd As XDocument = XDocument.Load(reqUrl)
    
    Distance = xd.Descendants("distance").FirstOrDefault().Element("text").Value

    After this I just do some calculations to display the quote.

    Tuesday, February 26, 2019 9:32 PM
  • User475983607 posted

    ftbadolato

    Sorry, missed one line of code at the top, here is everything:

    Dim reqUrl As String = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" & txtStartZip.Text & "&destinations=" & txtEndZip.Text & "&mode=driving&sensor=false&units=imperial"
    Dim xmlReq As HttpWebRequest = WebRequest.Create(reqUrl)
    xmlReq.ContentType = "text/xml"
    xmlReq.Method = "POST"
    Dim Distance As String
    Dim xd As XDocument = XDocument.Load(reqUrl)
    
    Distance = xd.Descendants("distance").FirstOrDefault().Element("text").Value

    After this I just do some calculations to display the quote.

    Again, you are not using the WebRequest object at all so that code is not needed.  You're using the same reqUrl to load the XDocument object though.  Keep in mind there's no credentials, nothing.  So I gave it a try by replacing the zip codes and copying the URL to my browser's address bar.  I received the following error message.

    <DistanceMatrixResponse>
    <status>REQUEST_DENIED</status>
    <error_message>
    You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account
    </error_message>
    </DistanceMatrixResponse>

    You'll need to read the Google docs and maybe create an account first.

    Tuesday, February 26, 2019 9:41 PM
  • User2080593117 posted

    This code has been working fine for literally years, no issues at all. I'm hesitant to start from scratch when this should be working. I'm thinking Google changed something in the format of their XML doc that's now causing an issue.

    You get a "request denied" error because you don't have a key. I have a key on the server that Google references. I checked and they do not expire.

    Tuesday, February 26, 2019 9:49 PM
  • User475983607 posted

    ftbadolato

    This code has been working fine for literally years, no issues at all. I'm hesitant to start from scratch when this should be working. I'm thinking Google changed something in the format of their XML doc that's now causing an issue.

    You get a "request denied" error because you don't have a key. I have a key on the server that Google references. I checked and they do not expire.

    There is absolutely no indication in the code provided that shows you are sending a key.  Also, there is no reason why you can't test this yourself.  Simply set a break point and see what comes back.  Or do as I did and copy the URL to your browser's address bar.

    I recommend contacting Google for assistance. 

    And if you follow the link in the error message.

    https://cloud.google.com/maps-platform/user-guide/account-changes/

    Tuesday, February 26, 2019 9:58 PM
  • User753101303 posted

    Try xd.Document.ToString() to show the XML document and understand why you don't find any more the expected content. 

    Wednesday, February 27, 2019 8:48 AM