添加事件处理程序在信息窗口内的元素谷歌地图v3

发布于 2024-11-14 21:25:10 字数 927 浏览 4 评论 0原文

您好,我正在尝试添加到信息窗口内的按钮,我尝试过这样的事件

var infoWindow = new google.maps.InfoWindow({
    content: '<div></br><span class="formatText">Ubicacion del Marcador: </span>' + event.latLng.toString() 
            + '</br> <input type="button" id="btnPrueba" value="prueba"/></div>'
});

google.maps.event.addListener(marker, 'click', function () {
    infoWindow.open(map, this);
    google.maps.event.addDomListener(document.getElementById('btnPrueba'), 'click', removeMarker);
});

function removeMarker(){

    alert('it works');
}

我做错了什么?或者这是另一种方法来做到这一点?谢谢

编辑

我也尝试过像这样的jquery,但是尽管它是通过函数获取的事件,但警报没有显示。当我用 chrome 进行调试时,它可以工作:(

google.maps.event.addListener(marker, 'click', function () {
    infoWindow.open(map, this);
    $("#btnPrueba").click(function () {

        alert('funciona');
    });
});

hi I'm trying to add to button inside an info window an event i tried like this

var infoWindow = new google.maps.InfoWindow({
    content: '<div></br><span class="formatText">Ubicacion del Marcador: </span>' + event.latLng.toString() 
            + '</br> <input type="button" id="btnPrueba" value="prueba"/></div>'
});

google.maps.event.addListener(marker, 'click', function () {
    infoWindow.open(map, this);
    google.maps.event.addDomListener(document.getElementById('btnPrueba'), 'click', removeMarker);
});

function removeMarker(){

    alert('it works');
}

What am i doing wrong? or it's another way to do this?. Thanks

Edit

I'm also tried with jquery like this but although the event it's get by the function the alert doesn't show. When i do the debug with chrome it's works :(

google.maps.event.addListener(marker, 'click', function () {
    infoWindow.open(map, this);
    $("#btnPrueba").click(function () {

        alert('funciona');
    });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↘紸啶 2024-11-21 21:25:10

尝试按钮的“onclick”属性。

'<input type="button" id="btnPrueba" value="prueba" onclick="removeMarker()"/>'

Try the "onclick" attribute for the button.

'<input type="button" id="btnPrueba" value="prueba" onclick="removeMarker()"/>'
冷情 2024-11-21 21:25:10

调用像 @DeeV 所示的全局函数确实看起来是最直接的方法(除了稍微弄脏全局命名空间,这在当今被看不起)。 jQuery 事件委托确实可以在没有任何额外的 setTimeout 体操的情况下工作,但感觉就像一个更重的函数调用堆栈来完成相同的目标。例如

$("#myMapContainer").on("click", "#btnPrueba", function() {alert "it works";});

,但是,坚持使用全局方法,除了@DeeV的答案之外,通常的技巧是获取一些可以在该事件处理程序中执行操作的上下文值:

  • 一种方法是将参数值嵌入到函数调用中
    InfoWindow 内容字符串
  • 沿着这些线,当您生成每个标记时,推送
    它是对数组的引用(也必须是全局的),从而方便
    稍后在处理程序中调用标记 API(例如 setIcon() 等),

例如 JS 伪代码

var myMarkerArray = [];

function removeMarker(markerIndex) {
  myMarkerArray[markerIndex].setMap(null);
}

for(int i=0; i<myLocationData.length; i++) {
  var marker = new google.maps.Marker({...});
  myMarkerArray.push(marker);
  var popup = new google.maps.InfoWindow({ content: '<input type="button" id="btnPrueba" value="prueba" onclick="removeMarker('+i+')"/>' });
}

Calling a global function like @DeeV's shown really does seem like the straightest shot at this (except for dirtying the global namespace a smidge which is looked down on these days). jQuery event delegation does work without any additional setTimeout gymnastics but feels like a heavier function call stack to accomplish the same goal. e.g.

$("#myMapContainer").on("click", "#btnPrueba", function() {alert "it works";});

However, sticking with the global approach, on top of @DeeV's answer, often the trick is getting some contextual values that you can act upon in that event handler:

  • one way is embedding parameter values to the function call in the
    InfoWindow content string
  • along those lines, as you generate each Marker, push
    it's reference to an array (also necessarily global) and thereby facilitate
    Marker API calls later in the handler (e.g. setIcon(), etc.)

e.g. JS Pseudocode

var myMarkerArray = [];

function removeMarker(markerIndex) {
  myMarkerArray[markerIndex].setMap(null);
}

for(int i=0; i<myLocationData.length; i++) {
  var marker = new google.maps.Marker({...});
  myMarkerArray.push(marker);
  var popup = new google.maps.InfoWindow({ content: '<input type="button" id="btnPrueba" value="prueba" onclick="removeMarker('+i+')"/>' });
}
随心而道 2024-11-21 21:25:10

我的解决方法是使用较短的超时。

var infoWindow = ...  (as in your question) ...
infoWindow.open(...);

setTimeout(function(){
  $('#btnPrueba').on('click', function(e){
    alert("It works at last!");
    });
  },50);  //Wait for 0.05s

当我不使用 setTimeout 包装器时,它不起作用。即使 setContent()open() 已经被调用。我认为这意味着创建是异步完成的。

注意:“50”是一个神奇的数字,因此可能很脆弱。

注意:使用以下内容对我来说也不起作用,我仍然觉得很奇怪并且无法解释:

$('#btnPrueba').on('click', document, function(e){
  alert("No luck...");
  });

My workaround was to use a short timeout.

var infoWindow = ...  (as in your question) ...
infoWindow.open(...);

setTimeout(function(){
  $('#btnPrueba').on('click', function(e){
    alert("It works at last!");
    });
  },50);  //Wait for 0.05s

When I didn't use the setTimeout wrapper it did not work. Even though setContent() and open() had already been called. I assume this means the creation is done asynchronously.

NOTE: the "50" is a magic number, so potentially brittle.

NOTE: using the following also did not work for me, which I still find strange and cannot explain:

$('#btnPrueba').on('click', document, function(e){
  alert("No luck...");
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文