php - 如何在服务器上的同构应用程序中获取某个位置的经纬度

标签 php mysql google-maps isomorphic-javascript

我有在服务器和客户端上加载的同构应用程序。我担心的是服务器上应用程序的加载结果,我需要调用 google maps api 服务来获取前“www.mysite.com/search/New-York--NY--United-States”的位置的纬度和经度然后我需要在 php 中使用以下代码从 google maps api 中找到它的纬度和经度(我的 api 在 php 中)

 $address= $this->input->get('address');
 $geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false');
            $geo = json_decode($geo, true);
            if ($geo['status'] = 'OK') {
              $lat = $geo['results'][0]['geometry']['location']['lat'];
              $lng = $geo['results'][0]['geometry']['location']['lng'];
            }

然后根据这个纬度和经度我搜索我的结果如下

$radius = 6371;
$distance = 250;
// latitude boundaries
$maxlat = $lat + rad2deg($distance / $radius);
$minlat = $lat - rad2deg($distance / $radius);
// longitude boundaries (longitude gets smaller when latitude increases)
$maxlng = $lng + rad2deg($distance / $radius / cos(deg2rad($lat)));
$minlng = $lng - rad2deg($distance / $radius / cos(deg2rad($lat)));

使用上面的代码,我计算了最大和最小纬度和经度,然后通过应用如下条件来查找我的结果。

$this->db->where('rv.LATITUDE >=', $minlat);
$this->db->where('rv.LATITUDE <=', $maxlat);
$this->db->where('rv.LONGTITUDE >=', $minlng);
$this->db->where('rv.LONGTITUDE <=', $maxlng);

如何在不调用 google maps api 的情况下获取经纬度以及如何优化它以更快地获得结果。请提供有关此问题的建议。

最佳答案

如果您不想确定街道地址,而只对离散的地名感兴趣,例如城市、地标等(在您的示例中,美国纽约),您可以查看各种 GeoNames exportables .您可以将它们加载到本地数据存储中并按名称执行查找。

关于php - 如何在服务器上的同构应用程序中获取某个位置的经纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42235025/

相关文章:

php - 在 yii2 中使用 memcache 缓存一个值

php - foreach($_SESSION AS $value){...} -> 获取 session 的数组键

php - 在 PHP 中从数据库构造外部类

php - 跨多个页面重用 session 变量

mysql - Spark MySql 连接器 Jar

google-maps - 我可以更改 Google map 嵌入 map (iframe) 中的标记吗?

javascript - 在 Jquery 中从外部文件加载数据

php - Chrome favicon.ico GET 请求

javascript - 基于地理编码的 IP 居中谷歌地图

ios - 捕获适用于 ios 的 googlemaps 的异常