如何使用Google Maps API获取城市名称

时间:2019-02-04 17:31:29

标签: google-maps google-maps-api-3

在伦敦,城市名称在postal_town类型的对象中,而在阿姆斯特丹,城市名称在locality类型的对象中,在东京,{{1}类型在对象中}}。

那么当城市之间变化时,我应该如何决定选择哪个对象来提取城市名称?

目前,我有一半可以用的东西:

administrative_area_level_1

但是,使用此代码我没有获得正确的东京城市名称,而是得到东京都区区港区。

东京的结果:

const checkCity = data.address_components.find((component) =>
    component.types.includes('locality')
);
const checkTown = data.address_components.find((component) =>
    component.types.includes('postal_town')
);

if (checkCity) {
    city = checkCity.long_name
} else if (checkTown) {
    city = checkTown.long_name
}

伦敦的结果:

address_components: Array(8)
    0: {long_name: "Sotobori Dori", short_name: "都道405号線", types: Array(1)}
    1: {long_name: "1", short_name: "1", types: Array(3)}
    2: {long_name: "2 Chome", short_name: "2 Chome", types: Array(3)}
    3: {long_name: "Motoakasaka", short_name: "Motoakasaka", types: Array(3)}
    4:
        long_name: "Minato-ku"
        short_name: "Minato-ku"
        types: (2) ["locality", "political"]
        __proto__: Object
    5:
        long_name: "Tōkyō-to"
        short_name: "Tōkyō-to"
        types: (2) ["administrative_area_level_1", "political"]

阿姆斯特丹的结果:

address_components: Array(7)
    0: {long_name: "27-29", short_name: "27-29", types: Array(1)}
    1: {long_name: "King Street", short_name: "King St", types: Array(1)}
    2:
        long_name: "London"
        short_name: "London"
        types: ["postal_town"]
        __proto__: Object
    3:
        long_name: "Greater London"
        short_name: "Greater London"
        types: (2) ["administrative_area_level_2", "political"]
        __proto__: Object
    4: {long_name: "England", short_name: "England", types: Array(2)}
    5: {long_name: "United Kingdom", short_name: "GB", types: Array(2)}
    6: {long_name: "WC2E 8JB", short_name: "WC2E 8JB", types: Array(1)}

1 个答案:

答案 0 :(得分:1)

遇到了尝试在GeoCoder响应中轻松查找特定项目的问题,我倾向于使用以下功能-它对我来说效果很好,但是我可以想象在某些情况下可能会产生不正确的结果。

/*
    calculate the intersection of two arrays - return result as a `Set` object
    and use the `size` method of the `Set` to determine if we made a match when
    testing the arrays..
*/
const intersect=function(a,b){
    return new Set( a.filter( v => ~b.indexOf( v ) ) );
};

const gettowncity=function( addcomp ){
    if( typeof( addcomp )=='object' && addcomp instanceof Array ){

        let order=[ 'sublocality_level_1', 'neighborhood', 'locality', 'postal_town' ];

        for( let i=0; i < addcomp.length; i++ ){
            let obj=addcomp[ i ];
            let types=obj.types;
            if( intersect( order, types ).size > 0 )return obj;
        }
    }
    return false;
};



In the callback function of the Geocoder request:

if( status == google.maps.GeocoderStatus.OK ){
    let addcomp = results[0].address_components;
    let obj = gettowncity( addcomp );

    if( obj ) console.info( 'Town/City: %o', obj.long_name );

    /* ... other code ... */
}

gettowncity的变体添加了要在响应对象中查找的类型的第二个参数

const findcomponent=function( addcomp, arr ){
    if( typeof( addcomp )=='object' && addcomp instanceof Array ){
        for( let i=0; i < addcomp.length; i++ ){
            let obj=addcomp[ i ];
            let types=obj.types;
            if( intersect( arr, types ).size > 0 )return obj;
        }
    }
    return false;
};

let obj=findcomponent( addcomp, [ 'postal_code' ] );
if( obj ) console.info( 'Postcode: %s', obj.long_name )

Example query to find Town of Lenzie, Scotland

相关问题