Getting Latitude And Longitude From Google Places Search Box
Solution 1:
Without the rest of your code, I can only show you what I have done using a form to collect an address. Using php 7 with json_decode(). There is a more efficient way to do this, but it works for me. Hope this helps.
//create string holding address to run through google geolocation API for Lat/Long//make sure to clean your posts, I use functions from another page that cleans htmls tags, trims and removes slashes and/or runs through a reg ex to replace and/or remove unwanted entries for the particular fields type.$address = clean_post($_POST['cust_street']).' '.clean_post($_POST['cust_city']).' '.clean_post($_POST['cust_state']).', '.clean_post($_POST['cust_zip']);
// Get JSON results from this request$url = 'https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false&key='.APIKEY;
$geo = file_get_contents($url);
// Convert the JSON to an array$geo = json_decode($geo, true);
if ($geo['status'] == 'OK') {
// Get Lat & Long$latitude = $geo['results'][0]['geometry']['location']['lat'];
$longitude = $geo['results'][0]['geometry']['location']['lng'];
}else{
$latitude = NULL;
$longitude = NULL;
$sessData['status']['type'] = 'alert alert-warning';
$sessData['status']['msg'] = 'Sorry, but it seems the address you entered is not being recognized by Google Maps API service.';
//die or exit, do something on failure
}
Then you can add these variables to your DB insert/update and/or echo them out as needed. This very code is run through an applet I am working on that takes a given address entered into a customer form and then processes that address, gets Lat and Long and then upon success, places that into the array I am running through the insert into my DB. I can then call on that to locate that customers lat and long on a map at a later date.
$custData = array(
'users_id' => $usr_id,
'cust_first_name' => clean_post( $cust_first_name ),
'cust_last_name' => clean_post( $cust_last_name ),
'cust_email' => clean_email( $cust_email ),
'cust_street' => clean_post( $cust_street ),
'cust_city' => clean_post( $cust_city ),
'cust_state' => clean_post( $cust_state ),
'cust_zip' => clean_post( $cust_zip ),
'cust_phone1' => clean_phone( $cust_phone2 ),
'cust_lat' => clean_phone( $latitude ),
'cust_long' => clean_float( $longitude ),
//etc,etc,etc
);
//$cust = new User(); further up in page //insertCust() function comes from user class stored in class.user.php$insert = $cust->insertCust($custData);
//set status based on data insertif($insert){
//set some session variables and store them before any header redirects.$_SESSION['status']['type'] = 'alert alert-success';
$_SESSION['status']['msg'] = 'You have registered '.$custName.' successfully!';
}
Btw, I am currently going through this document on XSS security and have found it to be most helpful: XSS_Filter_Evasion_Cheat_Sheet Hope this helps.
Post a Comment for "Getting Latitude And Longitude From Google Places Search Box"