469,642 Members | 1,211 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,642 developers. It's quick & easy.

Geocoding address before adding to database

127 100+
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!
Apr 21 '08 #1
6 2213
TheServant
1,168 Expert 1GB
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.
Apr 21 '08 #2
phub11
127 100+
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!
Apr 22 '08 #3
phub11
127 100+
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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $address = $_POST['address'];
  4.  
  5. define("MAPS_HOST", "maps.google.com");
  6. define("KEY", "ABC123");
  7.  
  8. // Initialize delay in geocode speed
  9. $delay = 0;
  10. $base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;
  11.  
  12. $request_url = $base_url . "&q=" . urlencode($address);
  13. $xml = simplexml_load_file($request_url) or die("url not loading");
  14.  
  15. $coordinates = $xml->Response->Placemark->Point->coordinates;
  16. $coordinatesSplit = split(",", $coordinates);
  17. // Format: Longitude, Latitude, Altitude
  18. $lat = $coordinatesSplit[1];
  19. $lng = $coordinatesSplit[0];
  20.  
  21. echo "Latt: $lat and Long: $lng";
  22.  
  23. ?>
  24.  
Apr 22 '08 #4
phub11
127 100+
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 (
Expand|Select|Wrap|Line Numbers
  1.  => 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 ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) 
  2.  
Thanks!
Apr 22 '08 #5
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
Apr 23 '08 #6
phub11
127 100+
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!
Apr 23 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by MikeM | last post: by
5 posts views Thread by Ignacio Domínguez | last post: by
3 posts views Thread by Jim Heavey | last post: by
5 posts views Thread by Kevin Newman | last post: by
3 posts views Thread by Tom Anderson | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.