[英]I need to obtain Latitude and Longitude from an Address
我是新來的。 這是我的第一篇文章,但我多年來一直是讀者。
我多年來一直在使用 HTML、CSS、JS 和 PHP,但這讓我很難過,我在網上尋求幫助。 我的問題是我需要獲取我傳遞給我的代碼的街道地址的緯度和經度坐標(例如,77 Massachusetts Ave, Cambridge, MA 02139)。
我環顧四周(或幾個月)並嘗試了幾個人的建議,這些建議通常是用 JS 或 PHP 編寫的。 我有一個有效的 Google API,它應該可以正常工作(我團隊中的其他人已經成功地在 Android 中使用它,並且它被標記為能夠在我們的網站上工作)。 我在多個網站上找到了很多例子,但沒有一個工作。
我嘗試過的一切都沒有產生任何結果。 我意識到谷歌對使用 API 的要求是新的,有些示例沒有使用,但即使我應用了我的,也沒有任何效果。 我正在尋找一個干凈、現代的例子來說明如何讓這個函數工作。
這是我嘗試過的幾個例子。 他們什么也沒產生,我不確定我是否可以做錯誤報告來找出原因:
<?php
function getLatLong($address){
if (!is_string($address))die("All Addresses must be passed as a string");
$_url = sprintf('http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=true&key=[MY_API_KEY]',rawurlencode($address));
if($_result = file_get_contents($_url)) {
$_result = json_decode($_result);
if($_result !== false || !empty($_result->results)) {
return $_result->results[0]->geometry->location;
}
}
return false;
}
$address = "77 Massachusetts Ave, Cambridge, MA 02139";
print_r(getLatLong($address));
$coordinates = getLatLong($address);
$lat = $coordinates['lat'];
$long = $coordinates['long'];
echo $lat.", ".$long;
echo "--";
?>
<?php
$address = '77 Massachusetts Ave, Cambridge, MA 02139'; // Address
$apiKey = '[MY_API_KEY]'; // Google maps now requires an API key.
// 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);
$geo = json_decode($geo, true); // Convert the JSON to an array
if (isset($geo['status']) && ($geo['status'] == 'OK')) {
$latitude = $geo['results'][0]['geometry']['location']['lat']; // Latitude
$longitude = $geo['results'][0]['geometry']['location']['lng']; // Longitude
}
?>
<?php
$dlocation = "77 Massachusetts Ave, Cambridge, MA 02139";
// Get lat and long by address
$address = $dlocation; // Google HQ
$prepAddr = str_replace(' ','+',$address);
$geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false&key=[MY_API_KEY]');
$output= json_decode($geocode);
$latitude = $output->results[0]->geometry->location->lat;
$longitude = $output->results[0]->geometry->location->lng;
echo $latitude;
?>
任何援助將不勝感激! 如果您有任何可以執行此操作的示例代碼,我當然希望看到它。
非常感謝,BDP
我最近在我的項目中使用了 google api 從下車和上車地址獲取 lat lngs。 這是代碼片段。 希望它可以幫助任何人
// url encode the address
$address = urlencode($address);
// google map geocode api url
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=YOUR_API_KEY";
// get the json response
$resp_json = file_get_contents($url);
// decode the json
$resp = json_decode($resp_json, true);
// response status will be 'OK', if able to geocode given address
if($resp['status']=='OK'){
// get the important data
$lati = isset($resp['results'][0]['geometry']['location']['lat']) ? $resp['results'][0]['geometry']['location']['lat'] : "";
$longi = isset($resp['results'][0]['geometry']['location']['lng']) ? $resp['results'][0]['geometry']['location']['lng'] : "";
$formatted_address = isset($resp['results'][0]['formatted_address']) ? $resp['results'][0]['formatted_address'] : "";
// verify if data is complete
if($lati && $longi && $formatted_address){
// put the data in the array
$data_arr = array();
array_push(
$data_arr,
$lati,
$longi,
$formatted_address
);
return $data_arr;
}else{
return false;
}
}
else{
echo "<strong>ERROR: {$resp['status']}</strong>";
return false;
}
}
我使用這個 api 來獲取用戶輸入的城市的緯度和經度。 請參閱文檔以獲得正確的預期結果。
使用帳戶注冊,您將獲得您的唯一密鑰。 鏈接: https : //account.mapbox.com/auth/signup/
https://api.mapbox.com/geocoding/v5/mapbox.places/ {city}.json?access_token={key}&limit=1`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.