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.
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.
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)
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:
$address = $_POST['address'];
// 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;
$lng = $coordinatesSplit;
echo "Latt: $lat and Long: $lng";
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!:
Array ( [KML] => Array (  => Array ( [child] => Array ( [RESPONSE] => Array (  => Array ( [child] => Array ( [NAME] => Array (  => Array ( [data] => brazil ) ) [STATUS] => Array (  => Array ( [child] => Array (
=> Array (  => Array ( [data] => 200 ) ) [REQUEST] => Array (  => Array ( [data] => geocode ) ) ) ) ) [PLACEMARK] => Array (  => Array ( [child] => Array ( [ADDRESS] => Array (  => Array ( [data] => Brazil ) ) [ADDRESSDETAILS] => Array (  => Array ( [child] => Array ( [COUNTRY] => Array (  => Array ( [child] => Array ( [COUNTRYNAMECODE] => Array (  => Array ( [data] => BR ) ) ) ) ) ) ) ) [POINT] => Array (  => Array ( [child] => Array ( [COORDINATES] => Array (  => Array ( [data] => -51.925280,-14.235004,0 ) ) ) ) ) ) ) ) ) ) ) ) ) ) )
I had to do something similar.
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.