从PHP获取经度和纬度的位置

时间:2015-04-14 09:45:07

标签: php google-maps geolocation

我希望在不同的变量中获得地址,城市,州国家/地区,以便我可以单独显示它,但由于latlong不同,我在某个地方得到整个地址在某个地方只有州和国家,所以由于改变了latlong,我无法得到具体的地址。

这是我的代码:

$geoLocation=array();
      $URL = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=8.407168,6.152344&sensor=false';
            $data = file_get_contents($URL);
            $geoAry = json_decode($data,true);
                for($i=0;$i<count($geoAry['results']);$i++){
                if($geoAry['results'][$i]['types'][0]=='sublocality_level_1'){
                    $address=$geoAry['results'][$i]['address_components'][0]['long_name'];
                    $city=$geoAry['results'][$i]['address_components'][1]['long_name'];
                    $state=$geoAry['results'][$i]['address_components'][3]['long_name'];
                    $country=$geoAry['results'][$i]['address_components'][4]['long_name'];
                    break;
                }else{
                    $address=$geoAry['results'][0]['address_components'][2]['long_name'];
                    $city=$geoAry['results'][0]['address_components'][3]['long_name'];
                    $state=$geoAry['results'][0]['address_components'][5]['long_name'];
                    $country=$geoAry['results'][0]['address_components'][6]['long_name'];
                    }
                }
            $geoLocation = array(
                'city'=>$city,
                'state'=>$state,
                'country'=>$country,
                'address'=>$address
            );
            print_r($geoLocation);

5 个答案:

答案 0 :(得分:8)

尝试下面的一个:

<?php 
$geolocation = $latitude.','.$longitude;
$request = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$geolocation.'&sensor=false'; 
$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; 
        }
    }

    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;
    }
    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;
    }
  }
?>

答案 1 :(得分:2)

使用Google Geocode API从纬度和经度获取位置。

这里是简单的PHP脚本。

$latitude = 'Insert Latitude';
$longitude = 'Insert Longitude';

$geocodeFromLatLong = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($latitude).','.trim($longitude).'&sensor=false'); 
$output = json_decode($geocodeFromLatLong);
$status = $output->status;
$address = ($status=="OK")?$output->results[1]->formatted_address:'';

上面的源代码可以在这里找到 - Get Address from Latitude and Longitude using Google Maps API and PHP

答案 2 :(得分:0)

点击此处查看Google地理编码API:https://developers.google.com/maps/documentation/geocoding

在您的情况下,location_type设置为“APPROXIMATE” 这意味着返回的结果不是精确的地理编码,这意味着您将没有整个地址。

答案 3 :(得分:0)

我正在使用codeigniter,并且有类似的要求,这就是我的获得方式

    <script>
                    var lt=pos.lat;
                    var lon=pos.lng;
                    var current_lat=$("#current_lat").val(lt);
                    var current_long=$("#current_long").val(lon);

    </script>

HTML:

   <input type="hidden" id="current_lat" name="current_lat" value="<?php echo $this- 
 >session->userdata('current_lat');?>">
                        <input type="hidden" id="current_long" name="current_long" 
 value="<?php echo $this->session->userdata('current_long');?>">

Php:

        $current_long= $this->input->post('current_long');
        $current_lat=$this->input->post('current_lat');
        $newdata = array(

                   'current_lat'     => $current_lat,
                   'current_long'     => $current_long
               );

               $this->session->set_userdata($newdata);  

答案 4 :(得分:-1)

上述解决方案无效,因为谷歌现在需要API KEY才能获取数据:

以下是一个简单的函数,它将返回latlong以及google_place_id

function geoLocate($address)
{
    try {
        $lat = 0;
        $lng = 0;

        $data_location = "https://maps.google.com/maps/api/geocode/json?key=".$GOOGLE_API_KEY_HERE."&address=".str_replace(" ", "+", $address)."&sensor=false";
        $data = file_get_contents($data_location);
        usleep(200000);
        // turn this on to see if we are being blocked
        // echo $data;
        $data = json_decode($data);
        if ($data->status=="OK") {
            $lat = $data->results[0]->geometry->location->lat;
            $lng = $data->results[0]->geometry->location->lng;

            if($lat && $lng) {
                return array(
                    'status' => true,
                    'lat' => $lat, 
                    'long' => $lng, 
                    'google_place_id' => $data->results[0]->place_id
                );
            }
        }
        if($data->status == 'OVER_QUERY_LIMIT') {
            return array(
                'status' => false, 
                'message' => 'Google Amp API OVER_QUERY_LIMIT, Please update your google map api key or try tomorrow'
            );
        }

    } catch (Exception $e) {

    }

    return array('lat' => null, 'long' => null, 'status' => false);
}
相关问题