简体   繁体   中英

PHP Get Address from longitude and latitude

I create a code to get longitude and latitude of the user and this variables are stored in mysql database.

I'm using this code below to have the address from longitude and latitude.

After a search in stackoverflow i found this script but it didn't give me anything no error and no result.

I tried to print the variable $geolocation and i have a result printed like this 33.8943186,35.505271199999996 but i don't have the full address :(

Can any body please help me to solve my problem and get the exact address of user ??!!

 $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
$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 );

In order to get full address, try this.

$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;

That URL will return a JSON response when executed. You need to convert it into an object/array. In my case it is an object. Just traverse through the array and get the required key, which is formatted_address in this case.

You have to print out $response[] !
$geolocation is just long and lat as it is defined in the Code.
For more help have a look at the Google Documentation here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM