如何在Angularjs的Google Map Api中使用placeid获取州和城市 [英] How to get state and city using placeid in Google Map Api in Angularjs

查看:314
本文介绍了如何在Angularjs的Google Map Api中使用placeid获取州和城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用place_id获取州和城市。我想用place_id获取州和城市。 t

请参阅以下链接 Google_map ,我想获得以下place_id place_id的状态和城市:ChIJLfyY2E4UrjsRVq4AjI7zgRY,

解决方案

Google Place Details响应包含 address_components []字段,它表示用于组成给定地址的单独地址组件的数组,您可以从此属性提取城市和州。

根据地址组件类型



  • administrative_area_level_1 - 表示国家一级以下的一级民事实体。在美国,这些
    行政级别是州。并非所有国家都具有这些
    管理级别。
  • 镇政治实体。

示例



 ;';} if(item.types.indexOf(locality)> -1){info + ='< div> City:'+ item.long_name +'< / div>';}}); infowindow.setContent(INFO); infowindow.setPosition(place.geometry.location); infowindow.open(map);}  

<身高:100%;保证金:0; padding:0;}#map {height:100%;}

< div id =map>< / div>< script src =https://maps.googleapis.com/maps/api/js?libraries=places&callback=initMapasync defer> < / script>


How to get state and city using place_id. I want to get state and city using place_id. t

Please see the following link Google_map, I want to get state and city of following place_id "place_id" : "ChIJLfyY2E4UrjsRVq4AjI7zgRY",

解决方案

Google Place Details response contains address_components[] field which represents an array of separate address components used to compose a given address, you could extract city and state from this property.

According to Address Component Types:

  • administrative_area_level_1 - indicates a first-order civil entity below the country level. Within the United States, these administrative levels are states. Not all nations exhibit these administrative levels.
  • locality - indicates an incorporated city or town political entity.

Example

function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10
    });

    var infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);

    service.getDetails({
        placeId: 'ChIJLfyY2E4UrjsRVq4AjI7zgRY'
    }, function (place, status) {
        if (status === google.maps.places.PlacesServiceStatus.OK) {
            map.setCenter(place.geometry.location);
            /*var marker = new google.maps.Marker({
                map: map,
                position: place.geometry.location
            });*/
            showPlaceDetails(map, place, infowindow);
        }
    });
}


function showPlaceDetails(map, place, infowindow) {
    //parse address details
    var info = '';
    place.address_components.forEach(function (item) {
        if (item.types.indexOf("administrative_area_level_1") > -1) {
            info += '<div>State: ' + item.long_name + '</div>';
        }
        if (item.types.indexOf("locality") > -1) {
            info += '<div>City: ' + item.long_name + '</div>';
        }
    });
    infowindow.setContent(info);
    infowindow.setPosition(place.geometry.location);
    infowindow.open(map);
}

html, body {
   height: 100%;
   margin: 0;
   padding: 0;
}

#map {
   height: 100%;
}

<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initMap" async defer></script>

这篇关于如何在Angularjs的Google Map Api中使用placeid获取州和城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆