Geocoding address before adding to database – PHP

  php

Q(Question):

Hi all,

I was wondering if anyone had some PHP code which uses the Google Maps geocoder to get Long. and Latt. before adding the address (and Log./Latt.) to a MySQL database.

The following is Google’s example, which just updates these parameters for an entire database. I’d like to do it for each new addition to the database rather than waste doing the whole thing.

http://code.google.com/support/bin/a…64&ctx=sibling

Thanks!

A(Answer):

I doubt there will be someone with exactly what you want. And there is even more doubt someone will make it for you. Have you been through that google example? Can you not reverse engineer it to see how to do it for one record? I know it’s long but someone has to do it!

Once you have done that and have some code, post back here with what you have, what is not working, and what it should do.

A(Answer):

Thanks for the reply.

Sorry if I came across as being lazy…. I thought I would check first, rather than re-invent the wheel (I’m surprised that this isn’t a standard script)!

I’ll post something when I get it semi-functioning (I still regard myself as a beginner)

Thanks!

A(Answer):

So I’ve pieced together a simple PHP script, which takes the user’s address submission, appends it to a URL, and the resulting output is a XML coded webpage which includes the Latt. and Long. However, my host provider (GoDaddy) doesn’t have the PHP function "simplexml_load_file" to dump the XML output into a file – meaning I can’t parse out the Latt. and Long.

Any suggestions on getting round this would be gratefully appreciated:


<?php
$address = $_POST['address'];
define("MAPS_HOST", "maps.google.com");
define("KEY", "ABC123");
// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;
$request_url = $base_url . "&q=" . urlencode($address);
$xml = simplexml_load_file($request_url) or die("url not loading");
$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];
echo "Latt: $lat and Long: $lng";
?>

A(Answer):

Hi again,

After a lot of Googling, I managed to find an alternative to "simplexml_load_file" by using the code listed in http://www.shop24-7.info/32-0-simplexml-alternative-php4.html. My last problem is dissecting the resulting hierarchy of arrays within arrays. Could someone give me some pointers as to how to pull out the relevant array and assign a variable to the value "-51.925280,-14.235004,0" from the following. It’s making my head spin!:

[code]
Array ( [KML] => Array ( [0] => Array ( [child] => Array ( [RESPONSE] => Array ( [0] => Array ( [child] => Array ( [NAME] => Array ( [0] => Array ( [data] => brazil ) ) [STATUS] => Array ( [0] => Array ( [child] => Array (

=> Array ( [0] => Array ( [data] => 200 ) ) [REQUEST] => Array ( [0] => Array ( [data] => geocode ) ) ) ) ) [PLACEMARK] => Array ( [0] => Array ( [child] => Array ( [ADDRESS] => Array ( [0] => Array ( [data] => Brazil ) ) [ADDRESSDETAILS] => Array ( [0] => Array ( [child] => Array ( [COUNTRY] => Array ( [0] => Array ( [child] => Array ( [COUNTRYNAMECODE] => Array ( [0] => Array ( [data] => BR ) ) ) ) ) ) ) ) [POINT] => Array ( [0] => Array ( [child] => Array ( [COORDINATES] => Array ( [0] => Array ( [data] => -51.925280,-14.235004,0 ) ) ) ) ) ) ) ) ) ) ) ) ) ) )

Thanks!

A(Answer):

I had to do something similar.

Check out http://www.tomanthony.co.uk/blog/geo…oogle-map-api/

I had to change the javascript slightly so instead of displaying the latitdue and longitude in an alert box, it put them straight into a text box so when you submit the form, it is saved to the database

A(Answer):

Thanks for the reply.

I managed a work around using a script which flattens the array. I then use "end" to set the latt and long as a string.

It’s not a very elegant way, so I’ll check out the link you recommended.

Thanks again!

LEAVE A COMMENT