php - Geocode on form submit -


i've searched through number of support forums , still can't find answer i'm looking for. essentially, have user registration form on website. when user clicks "submit" saves data in mysql database , redirects them page. simple enough.

but want have page has map marker each registered users' approximate location (city, state, country).

this isn't hard if users required input own lattitude , longitude. has information readily available???

when user clicks "submit" 3 input fields combined 1 variable ($address). how can geocode variable ($address), , have output 2 other variables ($lat , $lng)??? looking before mysql_connect, way have ready insert 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 connect database"); @mysql_select_db($database) or die("unable select database");

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

any appreciated!!! running php , mysql.

you can use google's geocoding api. straightforward in php. file_get_contents() (doc) on api url parameters need, , json_decode() (doc) on resulting data.

note tos says data must displayed on google map. otherwise need use geocoding service.

finally, don't forget error trapping, handling timeouts, , other goodies. don't want wait indefinitely google reply data. while service fast, connection troubles , other issues arise.

edit: apparently more needed beyond explanation above, here quick code 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, started. in output, see decoded. looking should in these:

$geocodedinfo['results'][0]['geometry']['location']['lat'] $geocodedinfo['results'][0]['geometry']['location']['lng'] 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -