我试图弄清楚如何将事件绑定到动态创建的元素.我需要事件在元素被销毁和重新生成后仍然存在.
显然,使用jQuery的live函数很容易,但是它们看起来像是用原生Javascript实现的呢?
要么我是白痴,要么就是谷歌地图团队的一个令人震惊的疏忽.
我试图在按钮单击事件上触发一个位置搜索请求,并结合标准的输入按键事件(当前正常工作).我已经梳理了与Google地方信息搜索框相关的文档,但没有找到任何可用的解决方案.
出于保密原因,我使用了演示中的示例.
function initialize() {
var map = new google.maps.Map(document.getElementById('map-canvas'), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-33.8902, 151.1759),
new google.maps.LatLng(-33.8474, 151.2631));
map.fitBounds(defaultBounds);
var input = /** @type {HTMLInputElement} */(document.getElementById('target'));
var searchBox = new google.maps.places.SearchBox(input);
var markers = [];
document.getElementById('button').addEventListener('click', function() {
var places = searchBox.getPlaces();
// places -> undefined
// The assumption is that I could just call getPlaces on searchBox
// but get nothing in 'places'
// Beyond that it doesn't even …Run Code Online (Sandbox Code Playgroud) javascript google-maps google-maps-api-3 google-maps-markers google-places-api