gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:04:22 25 4
gpt4 key购买 nike

我有在服务器和客户端上加载的同构应用程序。我担心的是服务器上应用程序的加载结果,我需要调用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com