- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我最近开始研究 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 英里半径范围内的任何城镇。有人可以告诉我如何去做吗?
感谢您的帮助
最佳答案
更新:我在 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;
}
关于上述代码的注意事项:
我将尝试说明这一点:
_________________
| / \ |
| 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 之间的(准)精确距离,然后您可以准确过滤掉那些距离足够近以通过粗略的 db-fetch,但又不够近以实际位于您的范围内的城市边界。
关于php - 查找邮政编码 10 英里半径范围内的城镇。谷歌地图 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8135243/
我有三个 td,并且正在尝试将每个内部的函数限制为仅该 td。我该怎么做呢?使用此代码,它会获取所有 3 个图像并将它们全部插入到 h2 之前: jQuery("td.frontpage_news")
这是所需的通用公式:if((b2-b1)=c1,True,False但是,我需要 b2-b1约等于 c1 , 在 5 内大约单位(在本例中为秒)。有没有可以处理这个的函数? 最佳答案 你也可以试试这个
我有三个整数,作为命令行参数传入后赋值给变量。我想验证每个整数都在 1-5 范围内。有没有一种方法可以在不使用如下所示的 if 语句的情况下在 Java 中完成此操作?我想避免这样做(注意伪代码):
检查某个变量 X 是否在某个变量 Z 的 n 个数字之内的最简洁方法是什么。n 是任意定义的数字(即 3)。 所以我想要 if (z {something} x){ // run code i
我的顶级 build.gradle (Gradle 2.2) 中有类似的东西 ext.repo = "https://my-artifactory-repo" buildscript { re
我只是在我的 jsp 页面中进行随机技巧和测试。我想使用 Attributes 将 request 范围对象存储在 session 范围对象中。存储后,当尝试从请求属性中提取值(存储在 session
我正在使用 Spring 。我有一个外部化属性文件。我正在按如下方式加载它。 现在我如何将 session 中的属性作为键值对保存? 我尝试编写一个扩展 ServletContextListene
我有以下范围: scope :billable, -> (range_start = nil, range_end = nil) { joins(:bids) .where("au
请看我的示例代码: var testObject = new SomeClass(); using (testObject) { //At this point how can the te
我目前在保持在 vector 范围内时遇到一些问题。 在下面的代码中,我试图检查正在检查的数字是否小于或等于它后面的数字 #include #include #include bool fun(
有人可以帮我解决下面的(简化的)代码吗?我试图从幻灯片事件函数中调用 doTheSlide() 函数。我对 JS 范围的理解仍然有点可疑。 实现这一目标的正确方法是什么?我收到此错误: Uncaugh
如何在 Swift 中检查时间是否在下午 6 点到晚上 11 点之间?我在使用 NSDateFormatter 时遇到了困难,我觉得一定有更简单的方法。 最佳答案 使用NSCalendar: let
我目前正在尝试创建一个 2D 横向滚动条,并且我目前有我的“世界”绘图(暂时是一个大白框),但我无法弄清楚世界地图的边缘与边缘之间的任何关系确保视口(viewport)始终完全被 map 覆盖。 我的
我正在学习李普曼,而且我只是在学习。我在这里尝试编写一个代码,该代码将返回 vector 中的最小元素。当我在 Codeblocks 中编译我的代码时,它说:“模板声明不能出现在 block 范围内”
我有三个日期对象。我该如何比较它们才能确定它们之间的相对差异。 oldDate = newDate() - 5; midDate = newDate() - 2.5; newDate = newDat
我需要检查对象“objCR”是否存在于当前范围内。我尝试使用以下代码。 if(objCR == null) alert("object is not defined"); 让我知道哪里错了。 最佳答案
如何检查 IP 地址是否属于私有(private)类别? if(isPrivateIPAddress(ipAddress)) { //do something } 如有
我正在开发一个 Firefox 插件,它可以转换用户通过用户选择突出显示的屏幕温度。转换后,用户选择将替换为 ID 为 alreadyconverted 的 span HTML 元素,其中包含原始温度
我正在开发一个邮资应用程序,该应用程序需要根据多个邮政编码范围检查整数邮政编码,并根据邮政编码匹配的范围返回不同的代码。 每个代码都有多个邮政编码范围。例如,如果邮政编码在 1000-2429、254
我正在使用 excel 范围进行连接:Set rng = Range("A1:A8")如果范围内的单元格之一为空,则会添加一个空格。 你如何阻止这个空间被添加? 最佳答案 假设您在那些非空单元格中有常
我是一名优秀的程序员,十分优秀!