問題描述
我最近開始研究 Google Maps API 以在我的網站中嘗試一些新的東西.我目前正在使用此代碼:
I've recently started looking at the Google Maps API to try out something new in my websites. I am currently using this code:
<?php
$postcode = $_REQUEST['postcode'];
$url = 'http://maps.googleapis.com/maps/api/geocode/xml?address='.$postcode.'&sensor=false';
$parsedXML = simplexml_load_file($url);
if($parsedXML->status != "OK") {
echo "There has been a problem: " . $parsedXML->status;
}
$myAddress = array();
foreach($parsedXML->result->address_component as $component) {
if(is_array($component->type)) $type = (string)$component->type[0];
else $type = (string)$component->type;
$myAddress[$type] = (string)$component->long_name;
}
header('Content-Type: application/json');
echo json_encode($myAddress);
?>
它只使用我定義的郵政編碼并搜索 Google 數據庫,然后返回城鎮、縣等.
which simply uses a postcode that I define and searches the Google database and then returns the town, county etc.
如果可能,我不僅想顯示最近的城鎮,還想顯示 5-10 英里范圍內的任何城鎮.有人能告訴我我會怎么做嗎?
If possible, I would like to not only show the nearest town but also any within a 5-10 mile radius. Could someone tell me how I would go about doing this please?
感謝您的幫助
推薦答案
更新:我在 http://www.mullie.eu/geographic-searches/
--
使用 Google Maps API 遍歷所有可用城鎮以獲取其緯度和經度.將這些保存在某處(數據庫).- 請注意,Google 不會接聽大量電話,因此請限制您的電話.
Loop through all available towns using the Google Maps API to fetch their latitude & longitude. Save these somewhere (database). - Beware, Google will not accept an enormous amount of calls, so throttle your calls.
然后,在抓取城鎮時,可以使用類似于下面代碼的代碼抓取一定范圍內的城市:
Then, when fetching a town, you can use code similar to the code below to grab the cities withing a certain range:
public static function getNearby($lat, $lng, $type = 'cities', $limit = 50, $distance = 50, $unit = 'km')
{
// radius of earth; @note: the earth is not perfectly spherical, but this is considered the 'mean radius'
if ($unit == 'km') $radius = 6371.009; // in kilometers
elseif ($unit == 'mi') $radius = 3958.761; // in miles
// latitude boundaries
$maxLat = (float) $lat + rad2deg($distance / $radius);
$minLat = (float) $lat - rad2deg($distance / $radius);
// longitude boundaries (longitude gets smaller when latitude increases)
$maxLng = (float) $lng + rad2deg($distance / $radius / cos(deg2rad((float) $lat)));
$minLng = (float) $lng - rad2deg($distance / $radius / cos(deg2rad((float) $lat)));
// get results ordered by distance (approx)
$nearby = (array) FrontendDB::getDB()->retrieve('SELECT *
FROM table
WHERE lat > ? AND lat < ? AND lng > ? AND lng < ?
ORDER BY ABS(lat - ?) + ABS(lng - ?) ASC
LIMIT ?;',
array($minLat, $maxLat, $minLng, $maxLng, (float) $lat, (float) $lng, (int) $limit));
return $nearby;
}
上述代碼注意事項:
- 使用自己的數據庫包裝器,因此轉換為 mysql_query、PDO、...
- 這不會是準確的.我們無法在 DB 中進行精確的球面計算,因此我們采用了上面的 &低緯度經度限制.這基本上意味著一個位置比您的距離稍遠(例如在遠東北,就在實際半徑之外(實際上幾乎是一個圓),但仍在最大緯度和經度內(因為我們將其與數據庫中的平方限制進行比較).這將只是粗略但 100% 準確地選擇您半徑范圍內的城市.
我將嘗試說明這一點:
_________________
| / |
| Y / |
| / |
|( X )|
| / |
| / |
|______\_/______|
上面的圓(有點)是你想要在其中找到位置的實際半徑,基于位置 X.這很難直接從你的數據庫中完成,所以我們實際上從數據庫中獲取的是周圍的正方形.如您所見,位置(如 Y)有可能落在這些 max & 范圍內.最小邊界,盡管它們實際上并不在請求的半徑范圍內.不過這些可以稍后通過 PHP 過濾掉.
The above circle (somewhat) is the actual radius where you want to find locations within, based upon location X. This is too hard to accomplish straight out of your DB, so what we actually fetch from the DB is the surrounding square. As you can see, it's possible that locations (like Y) fall within these max & min boundaries, though they aren't actually withing the requested radius. These can later be filtered out through PHP though.
為了解決最后一個問題,您可以循環所有結果并計算您的根位置和找到的接近匹配之間的確切距離,以計算它們是否實際上在您的半徑內.為此,您可以使用以下代碼:
To tackle this last issue, you could loop all results and calculate the exact distance between both your root location, and the close matches found, to calculate if they're actually within your radius. For that, you could use this code:
public static function getDistance($lat1, $lng1, $lat2, $lng2, $unit = 'km')
{
// radius of earth; @note: the earth is not perfectly spherical, but this is considered the 'mean radius'
if ($unit == 'km') $radius = 6371.009; // in kilometers
elseif ($unit == 'mi') $radius = 3958.761; // in miles
// convert degrees to radians
$lat1 = deg2rad((float) $lat1);
$lng1 = deg2rad((float) $lng1);
$lat2 = deg2rad((float) $lat2);
$lng2 = deg2rad((float) $lng2);
// great circle distance formula
return $radius * acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($lng1 - $lng2));
}
這將計算位置 X 和位置 Y 之間的(準)精確距離,然后您可以準確地過濾掉那些足夠近以通過粗略數據庫提取的城市,但又不只是足夠近以實際在您的范圍內邊界.
This will calculate the (quasi) exact distance between location X and location Y, and then you can filter out exactly those cities that were near enough to pass the rough db-fetch, but not just near enough to actually be within your bounds.
這篇關于查找郵政編碼 10 英里范圍內的城鎮.谷歌地圖API的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!