gpt4 book ai didi

php - Google map - 从经度和纬度获取国家、地区和城市名称(PHP)

转载 作者:可可西里 更新时间:2023-10-31 22:53:11 25 4
gpt4 key购买 nike

我想通过 Google map API(V3?)从经度和纬度获取国家、地区和城市名称 - 使用 PHP。我只是无法为每个县都获得它,我设法获得了这段代码,但它不适用于其他县,因为返回的结果不同(唯一正确的信息是国家,但即使是以本地名称返回的信息也不是英文名称):

<?
$lok1 = $_REQUEST['lok1'];
$lok2 = $_REQUEST['lok2'];

$url = 'http://maps.google.com/maps/geo?q='.$lok2.','.$lok1.'&output=json';
$data = @file_get_contents($url);
$jsondata = json_decode($data,true);

if(is_array($jsondata )&& $jsondata ['Status']['code']==200)
{
$addr = $jsondata ['Placemark'][0]['AddressDetails']['Country']['CountryName'];
$addr2 = $jsondata ['Placemark'][0]['AddressDetails']['Country']['Locality']['LocalityName'];
$addr3 = $jsondata ['Placemark'][0]['AddressDetails']['Country']['Locality']['DependentLocality']['DependentLocalityName'];
}
echo "Country: " . $addr . " | Region: " . $addr2 . " | City: " . $addr3;
?>

这对于从我的国家/地区获取正确的数据非常有效,但对其他国家/地区则不然...

最佳答案

您可以像这样使用第二个 api(Thomas 建议):

$geocode=file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=48.283273,14.295041&sensor=false');

$output= json_decode($geocode);

echo $output->results[0]->formatted_address;

试试这个..

编辑::::::

$geocode=file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=48.283273,14.295041&sensor=false');

$output= json_decode($geocode);

for($j=0;$j<count($output->results[0]->address_components);$j++){
echo '<b>'.$output->results[0]->address_components[$j]->types[0].': </b> '.$output->results[0]->address_components[$j]->long_name.'<br/>';
}

关于php - Google map - 从经度和纬度获取国家、地区和城市名称(PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9991224/

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