gpt4 book ai didi

javascript - 谷歌地图 : Open specific Info window on button click

转载 作者:太空宇宙 更新时间:2023-11-04 01:32:04 24 4
gpt4 key购买 nike

我在信息窗口中使用了谷歌地图自定义标记。我需要添加功能以在用户单击特定按钮时打开信息窗口。有多个具有不同内容的标记。在这里,我找到了一个通过单击按钮打开单个信息窗口的解决方案:https://stackoverflow.com/a/18334899/6191987

我已经尝试过上述解决方案,但我不知道如何定位以在单击按钮时打开每个特定的信息窗口。

这是我试过的代码:

  html,
body {
height: 100%;
margin: 0;
padding: 0;
}

#map {
height: 100%;
}

.btns {
display: inline-block;
width: 100%;
margin-bottom: 20px
}

.btns a {
display: block;
padding: 10px;
float: left;
background: #eee;
margin-right: 5px;
text-decoration: none;
}
<div class="btns">
<a href="#" onclick="myClick(0);">Open Info Window 1</a>
<a href="#" onclick="myClick(0);">Open Info Window 2</a>
<a href="#" onclick="myClick(0);">Open Info Window 3</a>
</div>

<div id="map"></div>

<script>
var map;

function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 17,
center: new google.maps.LatLng(40.712696, -74.005019),
mapTypeId: 'roadmap',
styles: [{
elementType: 'geometry',
stylers: [{
color: '#242f3e'
}]
}, {
elementType: 'labels.text.stroke',
stylers: [{
color: '#242f3e'
}]
}, {
elementType: 'labels.text.fill',
stylers: [{
color: '#746855'
}]
}, {
featureType: 'administrative.locality',
elementType: 'labels.text.fill',
stylers: [{
color: '#d59563'
}]
}, {
featureType: 'poi',
elementType: 'labels.text.fill',
stylers: [{
color: '#d59563'
}]
}, {
featureType: 'poi.park',
elementType: 'geometry',
stylers: [{
color: '#263c3f'
}]
}, {
featureType: 'poi.park',
elementType: 'labels.text.fill',
stylers: [{
color: '#6b9a76'
}]
}, {
featureType: 'road',
elementType: 'geometry',
stylers: [{
color: '#38414e'
}]
}, {
featureType: 'road',
elementType: 'geometry.stroke',
stylers: [{
color: '#212a37'
}]
}, {
featureType: 'road',
elementType: 'labels.text.fill',
stylers: [{
color: '#9ca5b3'
}]
}, {
featureType: 'road.highway',
elementType: 'geometry',
stylers: [{
color: '#746855'
}]
}, {
featureType: 'road.highway',
elementType: 'geometry.stroke',
stylers: [{
color: '#1f2835'
}]
}, {
featureType: 'road.highway',
elementType: 'labels.text.fill',
stylers: [{
color: '#f3d19c'
}]
}, {
featureType: 'transit',
elementType: 'geometry',
stylers: [{
color: '#2f3948'
}]
}, {
featureType: 'transit.station',
elementType: 'labels.text.fill',
stylers: [{
color: '#d59563'
}]
}, {
featureType: 'water',
elementType: 'geometry',
stylers: [{
color: '#17263c'
}]
}, {
featureType: 'water',
elementType: 'labels.text.fill',
stylers: [{
color: '#515c6d'
}]
}, {
featureType: 'water',
elementType: 'labels.text.stroke',
stylers: [{
color: '#17263c'
}]
}]
});

var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
var icons = {
parking: {
icon: iconBase + 'parking_lot_maps.png'
},
library: {
icon: iconBase + 'library_maps.png'
},
info: {
icon: iconBase + 'info-i_maps.png'
}
};

var features = [{
position: new google.maps.LatLng(40.712696, -74.005019),
content: 'test content one',
type: 'parking'
}, {
position: new google.maps.LatLng(40.712753, -74.006081),
content: 'test content two',
type: 'parking'
}, {
position: new google.maps.LatLng(40.713664, -74.007819),
content: 'test content three <a href="http://www.google.com">A link to google</a>',
type: 'library'
}];

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

// Create markers.
features.forEach(function(feature) {
var marker = new google.maps.Marker({
position: feature.position,
content: feature.content,
icon: icons[feature.type].icon,
map: map
});
var content = "<a href='" + feature.content + "'>A link to google</a>";
marker.addListener('click', function() {
infowindow.setContent('<div><strong>' + marker.content + '</strong></div>');

infowindow.open(map, marker);
});
});
}
//on click function
function myClick(id) {
google.maps.event.trigger(markers[id], 'click');
}

</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDvkk7wNQcIYXZ7S8XNG8cG-elq0QE2v3k&callback=initMap">


</script>

JSFiddle:https://jsfiddle.net/vishnuprasadps/3wk7q3fd/

最佳答案

那是因为 markers 不是您代码中定义的变量。在代码的顶部,您可以简单地将 markers 声明为空数组,即:

var markers = [];

靠近代码底部,当您遍历 features 集合时,您必须将标记对象插入此数组,即:markers.push(marker );。在您的代码上下文中,它看起来像这样:

// Create markers.
features.forEach(function(feature) {
var marker = new google.maps.Marker({
position: feature.position,
content: feature.content,
icon: icons[feature.type].icon,
map: map
});
var content = "<a href='" + feature.content + "'>A link to google</a>";
marker.addListener('click', function() {
infowindow.setContent('<div><strong>' + marker.content + '</strong></div>');

infowindow.open(map, marker);
});

// Push your marker object into your array
markers.push(marker);
});

这应该有效:请参阅下面的概念验证。注意:我冒昧地更改了内联 JS 回调参数,这样您就不会为所有 3 个链接触发相同的标记信息窗口。

html,
body {
height: 100%;
margin: 0;
padding: 0;
}

#map {
height: 100%;
}

.btns {
display: inline-block;
width: 100%;
margin-bottom: 20px
}

.btns a {
display: block;
padding: 10px;
float: left;
background: #eee;
margin-right: 5px;
text-decoration: none;
}
<div class="btns">
<a href="#" onclick="myClick(0);">Open Info Window 1</a>
<a href="#" onclick="myClick(1);">Open Info Window 2</a>
<a href="#" onclick="myClick(2);">Open Info Window 3</a>
</div>

<div id="map"></div>

<script>
var map;
var markers = [];

function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 17,
center: new google.maps.LatLng(40.712696, -74.005019),
mapTypeId: 'roadmap',
styles: [{
elementType: 'geometry',
stylers: [{
color: '#242f3e'
}]
}, {
elementType: 'labels.text.stroke',
stylers: [{
color: '#242f3e'
}]
}, {
elementType: 'labels.text.fill',
stylers: [{
color: '#746855'
}]
}, {
featureType: 'administrative.locality',
elementType: 'labels.text.fill',
stylers: [{
color: '#d59563'
}]
}, {
featureType: 'poi',
elementType: 'labels.text.fill',
stylers: [{
color: '#d59563'
}]
}, {
featureType: 'poi.park',
elementType: 'geometry',
stylers: [{
color: '#263c3f'
}]
}, {
featureType: 'poi.park',
elementType: 'labels.text.fill',
stylers: [{
color: '#6b9a76'
}]
}, {
featureType: 'road',
elementType: 'geometry',
stylers: [{
color: '#38414e'
}]
}, {
featureType: 'road',
elementType: 'geometry.stroke',
stylers: [{
color: '#212a37'
}]
}, {
featureType: 'road',
elementType: 'labels.text.fill',
stylers: [{
color: '#9ca5b3'
}]
}, {
featureType: 'road.highway',
elementType: 'geometry',
stylers: [{
color: '#746855'
}]
}, {
featureType: 'road.highway',
elementType: 'geometry.stroke',
stylers: [{
color: '#1f2835'
}]
}, {
featureType: 'road.highway',
elementType: 'labels.text.fill',
stylers: [{
color: '#f3d19c'
}]
}, {
featureType: 'transit',
elementType: 'geometry',
stylers: [{
color: '#2f3948'
}]
}, {
featureType: 'transit.station',
elementType: 'labels.text.fill',
stylers: [{
color: '#d59563'
}]
}, {
featureType: 'water',
elementType: 'geometry',
stylers: [{
color: '#17263c'
}]
}, {
featureType: 'water',
elementType: 'labels.text.fill',
stylers: [{
color: '#515c6d'
}]
}, {
featureType: 'water',
elementType: 'labels.text.stroke',
stylers: [{
color: '#17263c'
}]
}]
});

var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
var icons = {
parking: {
icon: iconBase + 'parking_lot_maps.png'
},
library: {
icon: iconBase + 'library_maps.png'
},
info: {
icon: iconBase + 'info-i_maps.png'
}
};

var features = [{
position: new google.maps.LatLng(40.712696, -74.005019),
content: 'test content one',
type: 'parking'
}, {
position: new google.maps.LatLng(40.712753, -74.006081),
content: 'test content two',
type: 'parking'
}, {
position: new google.maps.LatLng(40.713664, -74.007819),
content: 'test content three <a href="http://www.google.com">A link to google</a>',
type: 'library'
}];

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

// Create markers.
features.forEach(function(feature) {
var marker = new google.maps.Marker({
position: feature.position,
content: feature.content,
icon: icons[feature.type].icon,
map: map
});
var content = "<a href='" + feature.content + "'>A link to google</a>";
marker.addListener('click', function() {
infowindow.setContent('<div><strong>' + marker.content + '</strong></div>');

infowindow.open(map, marker);
});

markers.push(marker);
});
}
//on click function
function myClick(id) {
google.maps.event.trigger(markers[id], 'click');
}

</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDvkk7wNQcIYXZ7S8XNG8cG-elq0QE2v3k&callback=initMap">


</script>


补充说明:

  • 不要使用内联 JS。您可以简单地遍历您的链接并使用 .addEventListener('click', function() {...}) 将事件监听器绑定(bind)到它们.
  • 如果您有任意数量的特征,它有助于您获取此集合的长度并动态生成链接

关于javascript - 谷歌地图 : Open specific Info window on button click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46906466/

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