遲到了,但我想添加爲我搜索高和低早早就與谷歌地圖以及如何動態收集lat和長很多東西很適合我OP描述的方式。請糾正我,如果我不正確,因爲我仍然在學習PHP和谷歌地圖一般,但再次,此代碼適合我的需要,我想與OP和其他人可能正在尋找工作代碼的例子OP的標準。
我創建了這個函數後,頭撞到鍵盤很多,這在PHP 7中適用於我。如果您正在收集一個地址的地理位置,我認爲不需要循環。
- 簡單地解碼來自谷歌API的HTTP請求,檢查驗證請求,然後分配lat和長的值需要在你的應用程序/項目中使用的變量。
我已經包括了一個例子,說明如何通過一個簡單的街道地址收集Lat和Long客戶的表單。
*#注意:我沒有顯示任何清潔此答案中的發佈變量的示例。請務必清理您不需要的HTML實體/斜線/空空間等職位... *
function get_lat_and_long($address){
// $address = The address string in a proper address format, we run through urlencode();
// Get JSON results from this request
// APIKEY = *Google API personal key* and is a constant coming from an external constants.php file.
// You can add your key statically though I recommend saving all sensitive variables/constants and arrays in a server side file that has no access by regular users.
$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false&key='.APIKEY);
// Convert JSON and validate
$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;
}
return $latitude;
return $longitude;
}
沒有的功能...
//create string holding address to run through google geolocation API for Lat/Long
$address = $_POST['usr_street'].' '.$_POST['usr_city'].' '.$_POST['usr_state'].', '.$_POST['usr_zip'];
// Get JSON results from this request
$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false&key='.APIKEY);
// Convert JSON
$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;
}
從這裏,我可以把$latitude
和$longitude
插入到我的數據庫數組中並將值保存在我的數據庫列中。覈實。 EX。
$userData = array(
'usr_first_name' => $_POST['usr_first_name'],
'usr_last_name' => $_POST['usr_last_name'],
'usr_password' => password_hash($_POST['usr_password'], PASSWORD_DEFAULT),
'usr_email' => $_POST['usr_email'],
'usr_company_name' => $_POST['usr_company_name'],
'usr_website' => $_POST['usr_website'],
'usr_street' => $_POST['usr_street'],
'usr_city' => $_POST['usr_city'],
'usr_state' => $_POST['usr_state'],
'usr_zip' => $_POST['usr_zip'],
'usr_phone1' => $_POST['usr_phone1'],
'usr_lat' => $latitude,
'usr_long' => $longitude,
//->etc...etc...etc...
);
//->$user is user object declared further up in document and comes from external user class -> user.class.php
//-> insert is function from external USER class that inserts query into DB.
$insert = $user->insert($userData);
希望這可以幫助那些正在苦苦掙扎Google Maps API地理位置的人使用PHP。就使用從表單條目構造的簡單地址字符串動態收集緯度和長度而言。
檢查$ data ['results'] [0] ['geometry']中的內容。該數組中的第二個值是'ROOFTOP'。因爲您正在回顯值lat和lng,但它們不存在,所以該值的第一個字母將被回顯(R)。爲了解決這個問題,檢查'$ value'中是否存在'lat'和'lng'鍵。 – Ruben
'geometry'有三個較低級別:'location,location_type,viewport'。也許你正在尋找的是'$ value ['location'] ['lat']'和'$ value ['location'] ['lng']'。 – alalp
@Ruben好的,謝謝你的解釋。但爲什麼他們不存在?我在陣列中看到他們.. – Robbert