0
votes

We are facing issues of getting lat and long from Google map API. Actually, we are using google map api to get lat and long from address.

But in some cases google map api is not giving accurate lat and long but getting accurate lat and long if we search the address into google map directly as below mentioned google map URL.

Google Map URL : https://www.google.com/maps/place/mc carter hwy & verona ave newark nj

Google Map API URL : https://maps.googleapis.com/maps/api/geocode/xml?address=mc carter hwy & verona ave newark nj

Please suggest the alternate solution for the same. If we can solve with google map api then it's good.

2
What coordinates do you get? I get the same coordinates (<location> <lat>40.7771670</lat> <lng>-74.1522531</lng> </location>) google map showing result of the geocoder, google maps link (& is not allowed in a URL) - geocodezip
We are getting (<location><lat>40.7549580</lat><lng> -74.1666020 </lng></location>) coordinates using mentioned google api url. Could you please share the URL from which you got <lat>40.7771670</lat> <lng>-74.1522531</lng> result because we also want the same result. - Anil Prajapati
I replaced & with and, as I said in my first comment, & is not allowed in a URL (at least the way you are using it). - geocodezip
It works. Thanks a lot. - Anil Prajapati

2 Answers

2
votes

Check out encodeURI().

It is likely the spaces and symbols (& in particular) are interfering with the URL you're giving for the API.

Edit: To be a little more specific, note how the entire link isn't a hyperlink and that it stops after the space:

https://maps.googleapis.com/maps/api/geocode/xml?address=mc carter hwy & verona ave newark nj

Now here is the same string with your query encoded:

https://maps.googleapis.com/maps/api/geocode/xml?address=mc%20carter%20hwy%20%26%20verona%20ave%20newark%20nj

0
votes

& is not allowed in URLs. Use "and" instead:

https://maps.googleapis.com/maps/api/geocode/xml?address=mc carter hwy and verona ave newark nj

link

returns:

<GeocodeResponse>
 <status>OK</status>
 <result>
  <type>route</type>
  <formatted_address>NJ-21, New Jersey, USA</formatted_address>
  <geometry>
   <location>
    <lat>40.7917804</lat>
    <lng>-74.1448213</lng>
   </location>