javascript - 如何在laravel中找到给定城市的给定地址?
问题描述
我在一个表中有两个字段,一个是城市,另一个是地址。我必须找出地址是否在城市内。我怎么能找到这个。
用于获取城市的 lat 和 lng
$city_lat_lng =json_decode(file_get_contents("http://maps.google.com/maps/api/geocode/json?address={$ogl_city}&sensor=false"));
$city_lat = $city_lat_lng->results[0]->geometry->location->lat;
$city_long = $city_lat_lng->results[0]->geometry->location->lng;
用于获取地址的 lat 和 lng
$address_lat_lng = json_decode(file_get_contents("http://maps.google.com/maps/api/geocode/json?address={$ogl_address}&sensor=false"));
$address_lat = $address_lat_lng->results[0]->geometry->location->lat;
$address_long = $address_lat_lng->results[0]->geometry->location->lng;
在上面的代码中,我们可以找到地址和城市的经纬度。但是对于如何查找特定地址是否位于特定城市内,没有任何正确的想法。城市和地址都在这里动态。我是 Laravel 的新手。也在谷歌上搜索但没有得到任何适当的解决方案。
解决方案
您可以使用 API 响应中的 address_components 来获取城市:
$response = json_decode(file_get_contents("http://maps.google.com/maps/api/geocode/json?address={$ogl_city}&sensor=false"));
$firstOption = $response[0];
for($i=0;$i<count($firstOption['address_components']);$i++)
{
if (in_array ("administrative_area_level_1", $firstOption['address_components'][$i]['types']))
{
echo "city = ". $firstOption['address_components'][$i]['long_name'].PHP_EOL;
}
}
administrative_area_level_1
是一种行政区域,请在此处查看哪些类型符合您对城市的想法: https ://developers.google.com/places/supported_types
推荐阅读
- matlab - 在 Matlab 中表示电路
- javascript - MySQL JSON 列在 Linux 但不是 Windows 上解析
- laravel - Laravel 显示帖子的最新评论
- r - R:替换缩写词\单词
- hyperledger-fabric - 如何将自定义业务逻辑应用于 Hyperledger 上的背书策略?
- mysql - Bash Script MYSQL 从行中获取列
- c# - 在 WCF 服务 C#\ 中获取服务器端客户端的 Mac 地址
- powershell - 如何使用 PowerShell 自动登录 Chrome。我有一个使用 IE 运行但需要使用 Chrome 的脚本
- java - 错误 [org.hibernate.util.JDBCExceptionReporter] -
我是 docker 新手,运行 docker-compose 文件时出现此错误:错误 [org.hibernate.util.JDBCExceptionReporter] - <Communications link f
- c# - PostgresException:23503:更新或删除表违反外键约束