简体   繁体   English

在Google Map信息窗口中显示动态内容

[英]Display dynamic content in Google Map infowindow

I'd like to display Tide Times in an infowindow on my Google Map. 我想在Google地图的信息窗口中显示“ 潮汐时间”

I have tried it with a Weather widget and it works great: http://jsfiddle.net/nvwjnrhy/ 我已经用Weather小部件尝试了它,效果很好: http : //jsfiddle.net/nvwjnrhy/

However, trying similar with Tide Times doesn't work: http://jsfiddle.net/nvwjnrhy/1/ 但是,尝试与“潮汐时间”相似是行不通的: http : //jsfiddle.net/nvwjnrhy/1/

Here's my current full code: 这是我当前的完整代码:

 var geocoder = new google.maps.Geocoder(); var marker; var infoWindow; var map; function initialize() { var mapOptions = { zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); setLocation(); } function setLocation() { var address = '2349 Marlton Pike W, Cherry Hill, NJ 08002'; geocoder.geocode({ 'address': address }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { var position = results[0].geometry.location; marker = new google.maps.Marker({ map: map, position: position, title: 'Venue Name' }); map.setCenter(marker.getPosition()); var content = document.createElement('div'); var script = document.createElement('script'); script.src = "https://www.tidetimes.org.uk/grimsby-tide-times.js"; content.appendChild(script); infoWindow = new google.maps.InfoWindow({ content: content }); google.maps.event.addListener(marker, 'click', function() { infoWindow.open(map, marker); }); //infoWindow.open(map, marker); doesn't work google.maps.event.trigger(marker, 'click'); //still doesn't work } else { // } }); } initialize(); //google.maps.event.addDomListener(window, 'load', initialize); 
 html, body { width: 100%; height: 100%; } #map-canvas { width: 100%; height: 100%; margin: 10px 0; color: #000; } 
 <script src="http://maps.google.com/maps/api/js?sensor=false&.js"></script> <div id="map-canvas"></div> 

Thank you for any help with this :) 谢谢您的帮助:)

I'm not sure adding what's essentially just a <script> tag as the content of an infowindow is going to work. 我不确定添加什么实际上只是一个<script>标记,因为信息窗口的内容将起作用。 Instead I think you'd need to add that script file into your page (perhaps hidden somehow), then read the content of that div, and add that into the infowindow's content. 相反,我认为您需要将该脚本文件添加到页面中(也许以某种方式隐藏),然后读取该div的内容,并将其添加到信息窗口的内容中。 Something like: 就像是:

var script = document.createElement('script');
    script.src = "https://www.tidetimes.org.uk/grimsby-tide-times.js";
    $('#someDiv').appendChild(script);

infoWindow = new google.maps.InfoWindow({
    content: $('#someDiv').html();
});

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

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