如何在Google地图中的infoWindow中显示不同的内容? 我有谷歌地图和一个按钮。我用infowindow来显示内容。所以我怀疑的是,如果我将鼠标悬停在标记上它应该显示"你好"。但是当我点击一个按钮时它应该显示一些值(假设我已经编写了显示某些值的函数) 请注意:: hovering应在infowindow中显示hello,按钮应显示一些值
仅限JavaScript解决方案
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript">
var markers = [];
function initialize() {
var mapOptions = {
zoom: 10,
center: new google.maps.LatLng(40.714364, -74.005972),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("googlemap"), mapOptions);
var locations = [
['New York', 40.714364, -74.005972, 'http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png']
];
var marker, i;
var infowindow = new google.maps.InfoWindow({
content:"Hello World!"
});
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: locations[i][3]
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
markers.push(marker);
}
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.open(map,marker);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
function myClick(id){
google.maps.event.trigger(markers[id], 'click');
}
</script>
<div id="googlemap" style="width: 100%; height: 500px;"></div>
<a href="#" onclick="myClick(0);">Open Info Window</a>
以上解决方案的问题是::一旦你点击按钮,信息窗口的内容就会永久改变(它应该改回&#34;你好&#34;再次盘旋) < / p>
答案 0 :(得分:0)
如果您希望内容更改为&#34;您好&#34; on&#39; mouseover&#39;,将其更改为&#34; Hello&#34; on&#39; mouseover&#39;&#34;
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent("Hello");
infowindow.open(map, marker);
});
代码段
var markers = [];
function initialize() {
var mapOptions = {
zoom: 10,
center: new google.maps.LatLng(40.714364, -74.005972),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("googlemap"), mapOptions);
var locations = [
['New York', 40.714364, -74.005972, 'http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png']
];
var marker, i;
var infowindow = new google.maps.InfoWindow({
content: "Hello World!"
});
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: locations[i][3]
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
markers.push(marker);
}
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent("Hello");
infowindow.open(map, marker);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
function myClick(id) {
google.maps.event.trigger(markers[id], 'click');
}
&#13;
html,
body,
#googlemap {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<a href="#" onclick="myClick(0);">Open Info Window</a>
<div id="googlemap"></div>
&#13;