2 Replies - 4003 Views - Last Post: 16 June 2012 - 06:12 PM Rate Topic: -----

#1 fisicx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 24-February 12

Google maps address conversion

Posted 16 June 2012 - 07:41 AM

I want to display a google map on a webspage with a marker against a specific address.

The address is collected from a database, the spaces replaced with plus signs and given the variable name $map. All well and good and the map display:

<img src="http://maps.googleapis.com/maps/api/staticmap?center=' . $map . '&zoom=13&size=300x300&markers=color:blue&maptype=roadmap&sensor=false">


To display the marker I need to convert the address into lat/long and despite much reading of the google API I still can't work out how to use the Geocoding json.
I'm assuming I need to send the $map variable to Google and json will return the lat/long which I can then add to the map parameters.

But how?

Is This A Good Question/Topic? 0
  • +

Replies To: Google maps address conversion

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,994
  • Joined: 08-June 10

Re: Google maps address conversion

Posted 16 June 2012 - 05:51 PM

According to the Gocoding API docs, all you have to do is send a HTTP request with the address to Google, and it will return all the data on that address to you in either JSON or XML format.

The address you need to use to get JSON data, when looking for addresses from a database, looks like this:
http://maps.googleapis.com/maps/api/geocode/json?address={$address}&sensor=false


So assuming you have your address in a $address variable in PHP, you could construct that URL like this:
$reqUrl = sprintf(
	"http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false",
	urlencode($address));


The urlencode call ensures that the address can be safely injected into the query string.

The simplest way to send the request, then, is to use PHP's file_get_contents function. (This requires that allow_url_fopen is enabled in your PHP config.)
$rawResponse = file_get_contents($reqUrl);


This will get you the JSON data from Google as a string. However, if allow_url_fopen is disabled, or if there is a problem with your network, or with Google's API, then this may return FALSE. You need to check for that.

Next you need to convert the JSON strong over to a PHP object if you want it to be of any use. The json_decode function can do that for you.
$response = json_decode($rawResponse);



Now you have Google's response to your query, and all you have to do is interpret it. According to the docs, the JSON object has two root elements: status and results. The first thing you want to do is check to see if the status is "OK". That indicates that your address is valid and that the data is available. If it reads "ZERO_RESULTS" then your address could not be located. Any other value indicates some sort of an error.

Now, assuming the status is "OK", then your data would be located at $response->results[0]->geometry->location as "lng" and "lat" properties.
Was This Post Helpful? 1
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,994
  • Joined: 08-June 10

Re: Google maps address conversion

Posted 16 June 2012 - 06:12 PM

One other thing, regarding the HTML you posted. You are using & chars by themselves in the URL. Those are special chars in HTML, so to use them safely in URLs and other text, you must encode them using the HTML entity: &amp;

So a link like this:
<a href="http://example.com/?p2=v1&p2=v2">Linkage</a>


Should be written like:
<a href="http://example.com/?p2=v1&amp;p2=v2">Linkage</a>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1