user2858540
user2858540

Reputation: 31

JSON google geocode API get country

I have a JSON: http://maps.googleapis.com/maps/api/geocode/json?latlng=49,19&sensor=false

and I need get only short_name of country. (For this example 'SK'). Therefore I get the short_name which the attribute "types" contains [ "country", "political" ].

I have...

 data.results[0].address_components

thanks.

Upvotes: 3

Views: 10227

Answers (3)

balram bochliya
balram bochliya

Reputation: 9

<?php
$address = '77-379 North End road, London London SW61NP, United Kingdom'; // Your address(Please USe exist address)
$prepAddr = str_replace(' ','+',$address);

$geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&key=API_KEY&sensor=false');
$output= json_decode($geocode);


if ( isset($output->results) ) {
    if ( isset($output->results[0]) ) {
        if ( isset($output->results[0]->address_components) ) {
            foreach ($output->results[0]->address_components as $key => $value) {   
                if ( isset($value->types) ) {
                    if ( isset($value->types[0]) ) {
                        if($value->types[0] == 'country'){
                            print_r($value->short_name); //GB
                        }
                    }
                }
            }
        }
    }
} ?>

Upvotes: 1

CarlosZ
CarlosZ

Reputation: 1124

This one is to look for the address, city name, and Province

200 Dufferin St, Toronto, ON

geocoder.geocode(function (responses) {
    res = responses[0].address_components[0].short_name.concat(' ')
        .concat(responses[0].address_components[1].short_name).concat(', ')

    for (var i = 0; i < responses[0].address_components.length; i++) {
        if (responses[0].address_components[i].types[0] == "locality") {
            res = res.concat(responses[0].address_components[i].short_name).concat(', ');
        }
    }

    for (var i = 0; i < responses[0].address_components.length; i++) {
        if (responses[0].address_components[i].types[0] == "administrative_area_level_1") {
            res = res.concat(responses[0].address_components[i].short_name);
        }
    }
    alert(res);
});

Upvotes: 0

SamV
SamV

Reputation: 7586

This will loop through the address components and look for the country type then political. If you only want the country to be returned if both appear then let me know. Although its not hard to modify this code as the main helping point was the for loop.

// extract country short name (e.g. GB for Great Britain) from google geocode API result
function getCountry(addrComponents) {
    for (var i = 0; i < addrComponents.length; i++) {
        if (addrComponents[i].types[0] == "country") {
            return addrComponents[i].short_name;
        }
        if (addrComponents[i].types.length == 2) {
            if (addrComponents[i].types[0] == "political") {
                return addrComponents[i].short_name;
            }
        }
    }
    return false;
}

console.log(getCountry(data.results[0].address_components));

Upvotes: 19

Related Questions