简体   繁体   English

Google地图单击Div以触发InfoWindow

[英]Google Maps Clicking on a Div to Trigger InfoWindow

OK, I've tried a ton of the examples on here and I just can't seem to get the Info Windows to get the Element Properly Appended. 好的,我在这里尝试了大量的例子,我似乎无法获得Info Windows以正确附加元素。 I'm sure this is something with How I've structured this that is simply wrong. 我确信这是我的结构,这是完全错误的。

I've made my own Sidebar, and looking at this: http://koti.mbnet.fi/ojalesa/boundsbox/makemarker_sidebar.htm it should be possible. 我已经制作了自己的补充工具栏,看看这个: http//koti.mbnet.fi/ojalesa/boundsbox/makemarker_sidebar.htm它应该是可能的。 But I can't see what's different than my code. 但我看不出与我的代码有什么不同。

So here's my AddMarker and Add Sidebar Functions 所以这是我的AddMarker和Add Sidebar Functions

function addMarker(location, name, content) {

       console.log("Adding Marker: "+location)
        if (name === "Patient"){
            var patient_icon = '<?php echo base_url();?>images/Profile.png';
            var marker = new google.maps.Marker({
                position: location,
                map: map,
                title: name,
                icon: patient_icon
            });
        }else{
            var marker = new google.maps.Marker({
                position: location,
                map: map,
                title: name
            });
        }
        markersArray.push(marker);
        var info = new google.maps.InfoWindow ({content: content})
        google.maps.event.addListener(marker, 'click', function(){info.open(map,marker);});
        if (name != 'Patient'){
            sidebar(name, marker, content)
        }
    }

function sidebar(name, marker, content){
    name = name.replace(/\s+/g, '');
    $("<div id='"+name+"'class='even' onclick=' function(){google.maps.event.trigger("+marker+", \"click\")' >"+content+"</div>").appendTo($('#prov_list'));
}

What am I missing that the Onclick Function on the Sidebar elements does Nothing? 我错过了侧边栏元素上的Onclick功能什么都没有?

Thanks! 谢谢!

You can try something like this, i think the problem was with the string concatenation. 你可以尝试这样的东西,我认为问题在于字符串连接。

Here's another way to create dom elements: 这是创建dom元素的另一种方法:

        $("<div/>", {
            'id': name,
            'class': 'even',
            'html': content
        }).click(function(){
            google.maps.event.trigger(marker, 'click');
        }).appendTo($('#prov_list'));

Plain old DHTML: 普通老DHTML:

function sidebar(name, marker, content){
  var container = document.getElementById('prov_list');
  var myDiv = document.createElement('DIV');
  myDiv.onclick = function(){
    google.maps.event.trigger(marker, 'click');
  }
  myDiv.innerHTML =  content;
  container.appendChild(myDiv);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM