寻找邮政编码半径10英里范围内的城镇。谷歌地图API

时间:2011-11-15 10:57:25

标签: 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);


?>

只使用我定义的邮政编码并搜索Google数据库,然后返回城镇,县等。

如果可能的话,我不仅要展示最近的城镇,还要展示5-10英里范围内的任何城镇。有人可以告诉我,我该怎么做呢?

感谢您的帮助

1 个答案:

答案 0 :(得分:53)

更新:我在http://www.mullie.eu/geographic-searches/

上写了一篇关于这个特定主题的更详细的博客文章

-

使用Google Maps API循环浏览所有可用城镇,以获取他们的纬度&amp;经度。保存这些(数据库)。 - 请注意,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;
}

关于上述代码的说明:

  • 使用自己的数据库包装器,因此转换为mysql_query,PDO,...
  • 这不完全正确。我们不能在DB中进行精确的球形计算,所以我们采用了上限和下限。低纬度和低纬度经度限制。这基本上意味着一个比你的距离稍远的位置(例如在远东北方,实际半径之外(实际上几乎是一个圆圈),但仍然在最大纬度和经度之内(因为我们将它与数据库中的平方限制进行比较)。这将给出一个粗略但坚实的100%精确选择具有半径的城市。

我会试着说明一下:

_________________
|      / \      |
| Y  /     \    |
|  /         \  |
|(      X      )|
|  \         /  |
|    \     /    |
|______\_/______|

上面的圆圈(有点)是您想要根据位置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));
}

这将计算位置X和位置Y之间的(准)精确距离,然后您可以精确地过滤掉那些足以通过粗略的数据库提取的城市,但不仅仅是足够接近实际存在于您的范围内界限。