简体   繁体   English

如何在谷歌地图信息窗口中放置一个 html div

[英]How to place a html div in a google maps InfoWindow

I have a google maps application written in JavaScript.我有一个用 JavaScript 编写的谷歌地图应用程序。 I click on an area in maps to create an InfoWindow.我点击地图中的一个区域来创建一个信息窗口。

In this info window I want to link a div from my html file.在这个信息窗口中,我想从我的 html 文件链接一个 div。 This is so that I can use jquery to add functions and events to the div.这样我就可以使用 jquery 向 div 添加函数和事件。

in the InfoWindow, I would like the string 'details...' to appear, and when clicking on it, some function to execute.在信息窗口中,我希望出现字符串“详细信息...”,并在单击它时执行一些函数。 (for example a simple alert) (例如一个简单的警报)

In my JS:在我的 JS 中:

...
...
google.maps.event.addListener(ccg_area[j], 'click', showTB);
...
function showTB(event) {

    var contentString = #details;        //somehow I want to link this to details div


    infoWindow.setContent(contentString);

    infoWindow.open(map);
}

in my html:在我的 html 中:

<div id="map-canvas">
    <div id="details">details...</div>
</div>

You could use something like the following to get the contents from a given div:您可以使用以下内容从给定的 div 中获取内容:

/*
* @return google.maps.InfoWindow
* @global $
*/

function showTB(event) {
    // jQuery object containing the div
    var $details = $("#details");
    // $details[] is the actual DOM node
    infoWindow.setContent($details[0]);
    infoWindow.open(map);
    infoWindow.$details = $details;
    return infoWindow;
}

// Delegate click events on links in div
infoWindow.$details.live('click','a', function(){
  alert('You´re not going anywhere!');
  return false;
}); 

Added:添加:


Make sure that your code runs when the DOM is ready.确保您的代码在DOM准备就绪时运行。 Otherwise your selector will return nil.否则你的选择器将返回 nil。

$(document).ready(function(){
  console.log($('#details'));
});

I took the following approach on a recent class exercise.我在最近的课堂练习中采用了以下方法。

First, I create the infowindow with default content pointing to a spinner graphic:首先,我使用指向微调图形的默认内容创建信息窗口:

var infowindow = new google.maps.InfoWindow(
    { content: '<img class="spinner" src="images/loaderTransparent.gif">' }
);

The "spinner" class allows me to size and style the spinner with CSS. “微调器”类允许我使用 CSS 调整微调器的大小和样式。 I should probably put the source URL there as well, come to think of it.我可能也应该把源 URL 放在那里,想想看。

At the end of the HTML file containing the map, I have a hidden DIV which contains the content intended for the info window:在包含地图的 HTML 文件的末尾,我有一个隐藏的 DIV,其中包含用于信息窗口的内容:

<div id="iw-content" style="display: none">
    <h3>This is my spiffy infowindow</h3>
</div>

And then on the "click" listener, I open the infowindow and set the content to the inner HTML of the hidden div (jQuery syntax shown):然后在“单击”侦听器上,我打开信息窗口并将内容设置为隐藏 div 的内部 HTML(显示了 jQuery 语法):

google.maps.event.addListener(marker, 'click',
    function() {
        infowindow.open(map, marker);
        infowindow.setContent($("#iw-content").prop("innerHTML"));
    }
);

The innerHTML property query returns a string containing: innerHTML 属性查询返回一个包含以下内容的字符串:

<h3>This is my spiffy infowindow</h3>

And that becomes the InfoWindow content which is displayed to the user as formatted text.这将成为 InfoWindow 内容,以格式化文本的形式显示给用户。 I found this useful since it allows the content to be carried with the HTML file rather than as a string within the JavaScript.我发现这很有用,因为它允许内容与 HTML 文件一起携带,而不是作为 JavaScript 中的字符串。 And you can have multiple content divs in the same HTML file to apply to different infowindows.并且您可以在同一个 HTML 文件中有多个内容 div 以应用于不同的信息窗口。

I tried using a jQuery .load() call from "# #iw-content" to a DIV established within the default infowindow content using the infowindow.open() completion callback, but couldn't get it to work.我尝试使用从“##iw-content”到使用 infowindow.open() 完成回调在默认信息窗口内容中建立的 DIV 的 jQuery .load() 调用,但无法使其工作。 I think that was because it was grabbing the outerHTML, in effect, which includes the display:none attribute.我认为那是因为它实际上抓取了包含 display:none 属性的 outerHTML。

Just add the "text" to the EventListener of the marker @ the InfoWindow只需将“文本”添加到标记的 EventListener@InfoWindow

var text = ['<div id="infotext">' "do something here" '</div>'].join('');

var marker = new google.maps.Marker({position: pos, map: map, icon: customIcons.marker});

google.maps.event.addListener(marker, "click", function() {
    if (infowindow) infowindow.close();     
    infowindow = new google.maps.InfoWindow({content: text});
    infowindow.open(map, marker);
});

best最好的事物

M

I too was facing similar problem.我也面临着类似的问题。 Not sure, if it will help or make relevance?不确定,它是否会有所帮助或具有相关性? For me it got resolved by document.getElementById('').innerHTML.对我来说,它是由 document.getElementById('').innerHTML 解决的。

    google.maps.event.addListener(marker, 'click', function(){
    marker.content = document.getElementById('infoWindow').innerHTML;
    infoWindow.setContent('<h2>' + marker.title + '</h2>' + marker.content);
      infoWindow.open($scope.map, marker);
  });

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

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