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

Zar*_*rif 4 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);
Run Code Online (Sandbox Code Playgroud)

SHA*_*HAZ 9

尝试下面一个:

<?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;
    }
  }
?>
Run Code Online (Sandbox Code Playgroud)