gpt4 book ai didi

javascript - 如何向标记上的信息窗口添加新按钮?

转载 作者:行者123 更新时间:2023-12-03 02:00:58 25 4
gpt4 key购买 nike

这段JS代码

function initMap() {
var uluru = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});

var contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
'<div id="bodyContent">'+
'<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
'sandstone rock formation in the southern part of the '+
'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '
</p>'+
'<p>Attribution: Uluru, <a href="https://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
'https://en.wikipedia.org/w/index.php?title=Uluru</a> '+
'(last visited June 22, 2009).</p>'+
'</div>'+
'</div>';

var infowindow = new google.maps.InfoWindow({
content: contentString
});

var marker = new google.maps.Marker({
position: uluru,
map: map,
title: 'Uluru (Ayers Rock)'
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
}

我想向信息窗口添加一个名为“uluru”的新按钮。该按钮应该像照片一样位于关闭键旁边。你能帮我吗?

enter image description here

最佳答案

您可以通过使用 contentString 设置内容来创建按钮,并将其放置到您想要的位置,如以下演示所示。

function initMap() {
var uluru = {
lat: -25.363,
lng: 131.044
};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});

var contentString = '<div id="content">' + '<button type="button" class="custom_button">!!!</button>' +
'<div id="siteNotice"></div>' +
'<h1 id="firstHeading" class="firstHeading">Uluru</h1>' +
'<div id="bodyContent">' +
'<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
'sandstone rock formation in the southern part of the ' +
'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi)</p>' +
`<p>Attribution: Uluru, <a href="https://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">` +
'https://en.wikipedia.org/w/index.php?title=Uluru</a> ' +
'(last visited June 22, 2009).</p>' +
'</div>' +
'</div>';

var infowindow = new google.maps.InfoWindow({
content: contentString
});

var marker = new google.maps.Marker({
position: uluru,
map: map,
title: 'Uluru (Ayers Rock)'
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
}

initMap()
#map {
height: 90vh;
}

.custom_button {
position: absolute;
top: 0px;
right: 20px;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>

关于javascript - 如何向标记上的信息窗口添加新按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50041363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com