【发布时间】:2018-02-15 22:33:28
【问题描述】:
我在 AngularJS 应用程序中使用 GoogleMaps 自动完成功能,当我调用时...
autocomplete.getPlace();
当我尝试使用地点时,有一半时间说几何为空 而且一半的时间都在工作......
似乎无法弄清楚...我唯一的想法是我的代码在 getPlace() 返回之前继续运行,但我不确定如何等待它完成?
我的图书馆包括...
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=MyKey&libraries=imagery,places,geometry">
</script>
正在创建自动完成...
this.autocomplete = null;
$scope.initAutoComplete = function() {
// Create the autocomplete object, restricting the search to geographical
// location types.
webMapValues.autocomplete = new google.maps.places.Autocomplete( /** @type {!HTMLInputElement} */ (document.getElementById('autocomplete')), {
types: ['geocode']
});
// When the user selects an address from the dropdown, populate the address
// fields in the form.
webMapValues.autocomplete.addListener('place_changed', rcisMapService.dropPin);
};
我的 DropPin 功能...
mapSVC.dropPin = function() {
var place = webMapValues.autocomplete.getPlace();
webMapValues.mapObj.getView().setCenter(ol.proj.transform([place.geometry.location.lng(), place.geometry.location.lat()], 'EPSG:4326', 'EPSG:3857'));
webMapValues.mapObj.getView().setZoom(17);
webMapValues.marker = new google.maps.Marker({
map: webMapValues.gmapObj,
anchorPoint: new google.maps.Point(0, -29)
});
webMapValues.marker.setIcon( /** @type {google.maps.Icon} */ ({
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(35, 35)
}));
webMapValues.marker.setPosition(place.geometry.location);
webMapValues.marker.setVisible(true);
};
自动完成功能很好,但是当我调用“getPlace()”时
一半的时间......
下一行中的“几何”未定义。 place.geometry.location.lng()
非常感谢您提供的任何帮助!
【问题讨论】:
-
当 getPlace “不起作用”时,您是否在自动完成查询(用户输入)中看到任何模式?
-
请提供一个工作示例/小提琴并给出一些无法返回几何的查询示例。或者,使用this fiddle 并在它不起作用时举例说明。
-
MrUpsidown 感谢您的回复,我周末外出无法访问我的电脑...我将在这个星期一创建一个 Plunkr 或 Fiddle。
-
betofarina ,是的,它间歇性地工作......这意味着我可以运行它并且未定义“几何”,然后我立即再次运行它并定义几何......我认为这是某种计时的事情我的代码在 autocomplete.getPlace() 返回之前继续运行。
标签: javascript google-maps google-maps-api-3 google-maps-markers