从地址解析器获取地址,例如自动完成的Google地图

时间:2018-11-04 11:56:19

标签: angular typescript google-maps autocomplete angular-cli

我使用AutoComplete的Google地图。

当我从列表中选择位置时,我从autoComplete.getPlace()函数获得了位置,并且有一个名为adr_address的变量,它的值看起来像这样:

"<span class="street-address">xxx</span>, <span class="locality">xxx</span>, <span class="country-name">xxx</span>"

我的问题是,当我自己写地址而不从列表中选择地址时,我在adr_address函数中没有getPlace变量,并且如果{状态确定,我想获得与Geocoder中相同的结果。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我找到了一种通过place_id和PlacesService从Geocoder获取adr_address的方法。

new google.maps.Geocoder().geocode({"address":text }, function(results, status){
                        if (status == google.maps.GeocoderStatus.OK) {
                            var service = new google.maps.places.PlacesService(document.createElement('div'));
                            service.getDetails({
                                placeId: results[0].place_id
                            }, function(place, status) {
                                if (status === google.maps.places.PlacesServiceStatus.OK) {
                                    doSomething(place.adr_address);
                                }
                            });
                        }
                        else {
                            //geocode fail
                        }
                    });