最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON google geocode API get country - Stack Overflow

programmeradmin1浏览0评论

I have a JSON: ,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.

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.

Share Improve this question asked Oct 8, 2013 at 12:42 user2858540user2858540 311 gold badge1 silver badge4 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 19

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

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);
});
发布评论

评论列表(0)

  1. 暂无评论