0

I've searched through a number of support forums and I still can't find the answer I'm looking for. Essentially, I have a user registration form on my website. When the user clicks "submit" it saves their data in a mySQL database and redirects them to another page. Simple enough.

But I want to have another page that has a map with a marker for each registered users' approximate location (city, state, country).

This isn't too hard to do if users were required to input their own lattitude and longitude. But who has that information readily available???

When the user clicks "submit" three input fields are combined into one variable ($address). How can I geocode that variable ($address), and have it output to two other variables ($lat and $lng)??? Looking to do this BEFORE mysql_connect, that way I have everything ready to insert into mySQL database table.


$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$status=$_POST['status'];
$service=$_POST['service'];
$rank=$_POST['rank'];
$specialty=$_POST['specialty'];
$address=$_POST['city'] . ',' . $_POST['state'] . ',' . $_POST['country'];
$city=$_POST['city'];
$state=$_POST['state'];
$country=$_POST['country'];
$email=$_POST['email'];

mysql_connect($host,$username,$password) or die("Unable to connect to database"); @mysql_select_db($database) or die("Unable to select database");

$query = "INSERT INTO $table VALUES ('','$first_name','$last_name','$status','$service','$rank','$speciality','$address','$city','$state','$country','','','$email')"; mysql_query($query);

Any help would be MUCH appreciated!!! Running with PHP and mySQL.

Jeremy
  • 19
  • 2

2 Answers2

7

You can use Google's Geocoding API. This is fairly straightforward in PHP. Do a file_get_contents() (doc) on the API URL with the parameters you need, and then a json_decode() (doc) on the resulting data.

Note that their TOS says this data must eventually be displayed on a Google Map. Otherwise you will need to use another Geocoding service.

Finally, don't forget error trapping, handling timeouts, and other goodies. You don't want to wait indefinitely for Google to reply to you with data. While their service is very fast, connection troubles and other issues do arise.

Edit: Apparently more help is needed beyond my explanation above, so here is some quick code to help you.

<?php
$address="1600 Amphitheatre Pkwy, Mountain View, CA";
$result=file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" . urlencode($address) );
$geocodedinfo=json_decode($result);

print_r($geocodedinfo);
?>

Again, this is just to get you started. In the output, you will see everything that was decoded. What you are looking for should be in these:

$geocodedinfo['results'][0]['geometry']['location']['lat']
$geocodedinfo['results'][0]['geometry']['location']['lng']
Brad
  • 159,648
  • 54
  • 349
  • 530
  • Do you know of any examples that I might be able to use to walk me through the process? I've been through their website about 100 times but can't find anything that can help me get this done. – Jeremy Nov 15 '10 at 17:55
  • thanks for the tip! I'll do some work and give a shout if I have more questions. Thanks again! – Jeremy Nov 15 '10 at 20:01
  • This a great help! I had the same problem. Thanks! –  Mar 14 '13 at 11:39
2

Use Yahoo! Placefinder or one of the other geocoding APIs.

robertc
  • 74,533
  • 18
  • 193
  • 177
  • I know I need to use one of the geocoding APIs, just not quite how to incorporate it all. – Jeremy Nov 15 '10 at 17:56
  • @Jeremy If you go to the link I posted, click on the documentation link (step 3 of 'Getting started') then click on Examples section. Or are you actually looking for help making HTTP requests and parsing XML with PHP? – robertc Nov 15 '10 at 18:05