【问题标题】:PHP Get Address from longitude and latitudePHP从经度和纬度获取地址
【发布时间】:2016-11-11 07:15:30
【问题描述】:

我创建了一个代码来获取用户的经度和纬度,这些变量存储在 mysql 数据库中。

我正在使用下面的代码来获取经度和纬度的地址。

stackoverflow 中搜索后,我找到了这个脚本,但它没有给我任何错误,也没有结果。

我尝试打印变量 $geolocation,我打印的结果像这样 33.8943186,35.505271199999996 但我没有完整的地址:(

任何人都可以帮我解决我的问题并获得用户的确切地址吗??!!

 $geolocation = $latitude.','.$longitude;
     $request ='http://maps.googleapis.com/maps/api/geocode/json?latlng='.$geolocation.'';
    $file_contents = file_get_contents($request);
    $json_decode = json_decode($file_contents);
    if(isset($json_decode->results[0])) {
        $response = array();
        foreach($json_decode->results[0]->address_components as $addressComponet) {
            if(in_array('political', $addressComponet->types)) {
                    $response[] = $addressComponet->long_name;
print_r($response); // no error
            }
        }

        if(isset($response[0])){ $first  =  $response[0];  } else { $first  = 'null'; }
        if(isset($response[1])){ $second =  $response[1];  } else { $second = 'null'; } 
        if(isset($response[2])){ $third  =  $response[2];  } else { $third  = 'null'; }
        if(isset($response[3])){ $fourth =  $response[3];  } else { $fourth = 'null'; }
        if(isset($response[4])){ $fifth  =  $response[4];  } else { $fifth  = 'null'; }

        if( $first != 'null' && $second != 'null' && $third != 'null' && $fourth != 'null' && $fifth != 'null' ) {
            echo "<br/>Address:: ".$first;
            echo "<br/>City:: ".$second;
            echo "<br/>State:: ".$fourth;
            echo "<br/>Country:: ".$fifth;
print_r($response);// No error
        }
        else if ( $first != 'null' && $second != 'null' && $third != 'null' && $fourth != 'null' && $fifth == 'null'  ) {
            echo "<br/>Address:: ".$first;
            echo "<br/>City:: ".$second;
            echo "<br/>State:: ".$third;
            echo "<br/>Country:: ".$fourth;
        }
        else if ( $first != 'null' && $second != 'null' && $third != 'null' && $fourth == 'null' && $fifth == 'null' ) {
            echo "<br/>City:: ".$first;
            echo "<br/>State:: ".$second;
            echo "<br/>Country:: ".$third;
        }
        else if ( $first != 'null' && $second != 'null' && $third == 'null' && $fourth == 'null' && $fifth == 'null'  ) {
            echo "<br/>State:: ".$first;
            echo "<br/>Country:: ".$second;
        }
        else if ( $first != 'null' && $second == 'null' && $third == 'null' && $fourth == 'null' && $fifth == 'null'  ) {
            echo "<br/>Country:: ".$first;
        }
      }
print_r($response);// error

【问题讨论】:

  • 你的问题是什么?
  • @Blueblazer172 我想从存储在我的数据库中的经度和纬度获取地址
  • 这是一个声明:P
  • @Blueblazer172 你是什么意思?!
  • 您的代码在我的机器上运行良好...

标签: php google-maps


【解决方案1】:

为了得到完整的地址,试试这个。

$latitude = 38.872292;
$longitude = -76.9698557;
$geocode=file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?latlng=38.872292,-76.9698557&sensor=false&key=YOUR_KEY_HERE');
$output= json_decode($geocode);
$formattedAddress = @$output->results[0]->formatted_address;

该 URL 将在执行时返回 JSON 响应。您需要将其转换为对象/数组。在我的情况下,它是一个对象。只需遍历数组并获取所需的密钥,在本例中为formatted_address

【讨论】:

  • 我就这样累了,但我没有任何输出 $geocode=file_get_contents('maps.googleapis.com/maps/api/geocode/…); $output= json_decode($geocode); $formattedAddress = @$output->results[0]->formatted_address; print_r($formattedAddress);
  • 我在 ogofiddle 上试过,它可以工作,但我不知道为什么在我的机器上不能工作你有什么建议
  • 我在本地主机上累了,它的工作,但问题是在线
  • stdClass Object ( [error_message] => 您已超出此 API 的每日请求配额。我们建议您在 Google Developers Console 中注册密钥:console.developers.google.com/... [结果] => Array ( ) [status] => OVER_QUERY_LIMIT ) stdClass Object ( [error_message] => 您已超出此 API 的每日请求配额。我们建议在 Google Developers Console 中注册密钥:console.developers.google。 com/... [结果] => 数组 ( ) [状态] => OVER_QUERY_LIMIT )
【解决方案2】:
$lat=52.722452;
$lng=1.099504;

$geolocation = $lat.','.$lng;
$request ='http://maps.googleapis.com/maps/api/geocode/json?latlng='.$geolocation.'';
$json = json_decode( file_get_contents( $request ) );

$results  = $json->results[0];
$addr = $results->formatted_address;
$comp = $results->address_components;

$response=array();

foreach( $comp as $i => $obj ){
    if( in_array( 'political', $obj->types ) ) $response[]=$obj->long_name;
}

echo $addr;
print_r( $response );

【讨论】:

  • 我有这个错误 Undefined offset: 0 Notice: Trying to get property of non-object Notice: Trying to get property of non-object Warning: Invalid argument provided for foreach()
【解决方案3】:

您必须打印出 $response[]!
$geolocation 只是代码中定义的 long 和 lat。
如需更多帮助,请查看 Google 文档here

【讨论】:

  • 我厌倦了像这样打印 $response[] print_r($response[]);就像这样 print_r($response['0']);就像这样 print_r($response);但我有这个消息注意:未定义的变量:响应
  • 你在哪里 print_r() ?
  • @Blueblazer172 检查上面我尝试打印的地方_r
猜你喜欢
  • 1970-01-01
  • 2014-06-06
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
相关资源
最近更新 更多