简体   繁体   中英

Using Google API get Latitude, Longitude from places and get specific country for the same(using PHP)

Using Google API get Latitude, Longitude from places and get specific country for the same. How can I do it? (using PHP)

$formattedAddr = str_replace(' ','+',$address);
//Sending request and receive json data by address

$geocodeFromAddr=file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$formattedAddr.'&sensor=false');
$output = json_decode($geocodeFromAddr);

//Get latitude and longitute from json data

$data['latitude']  = $output->results[0]->geometry->location->lat;
$data['longitude'] = $output->results[0]->geometry->location->lng;

Grab it from formatted_address property. It contains full address, comma separeted with country after last comma. Try with:

$formatted_address = $output->results[0]->formatted_address;

$country = trim(end(explode(',',$formatted_address)));

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