Dan*_*ton 13 php google-maps google-maps-api-3
我最近开始查看Google Maps API以尝试在我的网站上发布新内容.我目前正在使用此代码:
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
它只是使用我定义的邮政编码并搜索谷歌数据库,然后返回城镇,县等.
如果可能的话,我不仅要展示最近的城镇,还要展示5-10英里范围内的任何城镇.有人能告诉我,我该怎么做呢?
谢谢你的帮助
mat*_*lie 54
更新:我在http://www.mullie.eu/geographic-searches/上写了一篇关于这个特定主题的更详细的博文.
-
使用Google Maps API循环浏览所有可用城镇以获取其纬度和经度.保存这些(数据库). - 请注意,Google不会接受大量的通话,因此请限制您的通话.
然后,在获取城镇时,您可以使用类似于以下代码的代码来获取具有特定范围的城市:
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;
}
Run Code Online (Sandbox Code Playgroud)
关于上述代码的说明:
我会试着说明一下:
_________________
| / \ |
| Y / \ |
| / \ |
|( X )|
| \ / |
| \ / |
|______\_/______|
Run Code Online (Sandbox Code Playgroud)
上面的圆(有点)是你想要在其中找到位置的实际半径,基于位置X.这太难以直接从你的数据库中完成,所以我们从数据库中实际获取的是周围的正方形.正如您所看到的,位置(如Y)可能落在这些最大和最小边界内,尽管它们实际上并没有满足请求的半径.这些可以在以后通过PHP过滤掉.
要解决最后一个问题,您可以循环所有结果并计算根位置和找到的近似匹配之间的确切距离,以计算它们是否实际位于您的半径范围内.为此,您可以使用此代码:
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));
}
Run Code Online (Sandbox Code Playgroud)
这将计算位置X和位置Y之间的(准)精确距离,然后您可以精确地过滤掉那些足以通过粗略的db-fetch的城市,但不仅仅是足够接近实际在您的范围内.
归档时间: |
|
查看次数: |
22955 次 |
最近记录: |