因此,我尝试将https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform的搜索栏添加到我的Meteor应用.首先,我需要加载Google商家信息库.但是,该链接还尝试直接写入DOM以获取另一个链接.Meteor不允许这样,所以我决定像这样加载两个js文件.
Template.listingSubmit.rendered = function(){
if (!this.rendered){
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places";
document.body.appendChild(script);
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://maps.gstatic.com/cat_js/maps-api-v3/api/js/17/13/%7Bmain,places%7D.js";
document.body.appendChild(script);
this.rendered = true;
}
};
Run Code Online (Sandbox Code Playgroud)
那样有用吗?我的下一个问题是如何初始化自动填充文本字段?相应模板中的html很简单.
<div id="locationField">
<input id="autocomplete" placeholder="Enter your address" type="text">
</div>
Run Code Online (Sandbox Code Playgroud)
现在我尝试添加
var autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),{types: ['geocode'] }
);
Run Code Online (Sandbox Code Playgroud)
到Template.listingSubmit.rendered但没有任何反应.我得到了谷歌未定义的错误.什么地方出了错?