通过使用谷歌反向地理编码获得街道,城市和国家 [英] Getting street,city and country by reverse geocoding using google

查看:111
本文介绍了通过使用谷歌反向地理编码获得街道,城市和国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从google json中获取$ street,$ city和$ country字符串。
适用于我的家庭住址:
http://maps.googleapis.com/maps/api/geocode/json?latlng=52.108662,6.307370&sensor=true

  $ url =http://maps.googleapis.com/maps/api/geocode/json?latlng=\".$lat.\",\".$lng.\"&传感器=真; 
$ data = @file_get_contents($ url);
$ jsondata = json_decode($ data,true);
if(is_array($ jsondata)&& $ jsondata ['status'] ==OK)
{
$ city = $ jsondata ['results'] ['0 '] [' address_components '] [' 2 '] [' LONG_NAME'];
$ country = $ jsondata ['results'] ['0'] ['address_components'] ['5'] ['long_name'];
$ street = $ jsondata ['results'] ['0'] ['address_components'] ['1'] ['long_name'];
}

但是对于数组中更多数据的不同地址,例如:
http://maps.googleapis .com / maps / api / geocode / json?latlng = 52.154184,6.199592& sensor = true
它不起作用,因为json数组中有更多的数据,并且它使省国家。



如何选择我需要的类型(long_name)?
$ b


  • for street:long_name其中types:[route]

  • long_name其中types:[locality,political]

  • 适用于country:long_name其中types:[country,political]



地理编码JSON的输出示例:

  {
结果:[
{
address_components:[
{
long_name:89,
short_name:89 ,
types:[street_number]
},
{
long_name:Wieck De,
short_name:Wieck De ,
types:[establishment]
},
{
long_name:Industrieweg,
short_name:Industrieweg,
types:[route]
},
{
l ong_name:Zutphen,
short_name:Zutphen,
types:[locality,political]
},
{
long_name:Zutphen,
short_name:Zutphen,
types:[administrative_area_level_2,political]
},
{
long_name:Gelderland,
short_name:GE,
types:[administrative_area_level_1,political]
},
{
long_name:Nederland,
short_name:NL,
types:[country,political]
},
{
long_name:7202 CA,
short_name:7202 CA,
types:[postal_code]
}

我认为我自己修改了它,因此我的代码:

  // street 
foreach($ jsondata [results] as $ result){
foreach($ result [ address_components]为$ address){
if(in_array(route,$ address [types])){
$ street = $ address [long_name];
}
}
}
// city
foreach($ jsondata [results] as $ result){
foreach($ result [ address_components]为$ address){
if(in_array(locality,$ address [types])){
$ city = $ address [long_name];
}
}
}
// country
foreach($ jsondata [results] as $ result){
foreach($ result [ address_components]为$ address){
if(in_array(country,$ address [types])){
$ country = $ address [long_name];




解决方案

您可以将数据转换为关联数组并像它一样工作,如

  $ data = array(); 
foreach($ jsondata ['results'] ['0'] ['address_components'] as $ element){
$ data [implode('',$ element ['types'])] = $元件[ 'LONG_NAME'];
}
print_r($ data);

echo'route:'。 $ data ['route']。 \\\
;
echo'country:'。 $ data ['country political'];


I'm trying to getting the $street, $city and $country string from google json. It works for my home address : http://maps.googleapis.com/maps/api/geocode/json?latlng=52.108662,6.307370&sensor=true

$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lng."&sensor=true";
    $data = @file_get_contents($url);
    $jsondata = json_decode($data,true);
    if(is_array($jsondata) && $jsondata['status'] == "OK")
    {
          $city = $jsondata['results']['0']['address_components']['2']['long_name'];
          $country = $jsondata['results']['0']['address_components']['5']['long_name'];
          $street = $jsondata['results']['0']['address_components']['1']['long_name'];
    }

But for a different address with more data in the arrays like this example: http://maps.googleapis.com/maps/api/geocode/json?latlng=52.154184,6.199592&sensor=true it doesn't work, because there is more data in the json array and it makes the province the country.

How can I select the type that I need (long_name)?

  • for street : long_name where "types" : [ "route" ]
  • for city : long_name where "types" : [ "locality", "political" ]
  • for country : long_name where "types" : [ "country", "political" ]

Example output from the geocode JSON:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "89",
               "short_name" : "89",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Wieck De",
               "short_name" : "Wieck De",
               "types" : [ "establishment" ]
            },
            {
               "long_name" : "Industrieweg",
               "short_name" : "Industrieweg",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Zutphen",
               "short_name" : "Zutphen",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Zutphen",
               "short_name" : "Zutphen",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Gelderland",
               "short_name" : "GE",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Nederland",
               "short_name" : "NL",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "7202 CA",
               "short_name" : "7202 CA",
               "types" : [ "postal_code" ]
            }

I think I fixed it myself, hereby my code:

// street
foreach ($jsondata["results"] as $result) {
    foreach ($result["address_components"] as $address) {
        if (in_array("route", $address["types"])) {
            $street = $address["long_name"];
        }
    }
}
// city
foreach ($jsondata["results"] as $result) {
    foreach ($result["address_components"] as $address) {
        if (in_array("locality", $address["types"])) {
            $city = $address["long_name"];
        }
    }
}
// country
foreach ($jsondata["results"] as $result) {
    foreach ($result["address_components"] as $address) {
        if (in_array("country", $address["types"])) {
            $country = $address["long_name"];
        }
    }
}

解决方案

You could convert the data to the associative array and work with it like

 $data = array();
 foreach($jsondata['results']['0']['address_components'] as $element){
     $data[ implode(' ',$element['types']) ] = $element['long_name'];
 }
 print_r($data);

 echo 'route: ' . $data['route'] . "\n";
 echo 'country: ' . $data['country political'];

这篇关于通过使用谷歌反向地理编码获得街道,城市和国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆