gpt4 book ai didi

javascript - 谷歌地图 v3 点击外部 html 链接打开信息窗口

转载 作者:行者123 更新时间:2023-11-28 20:37:11 27 4
gpt4 key购买 nike

不知道是否有人可以帮助我,我已经设置了一个谷歌地图,一切都很好。我唯一不知道该怎么做的就是根据来自外部 html 链接(不在 JS 中)的 ID 打开一个信息窗口。

function initialize() {
// Create the map
// No need to specify zoom and center as we fit the map further down.
var map = new google.maps.Map(document.getElementById("map"), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
});
infowindow = new google.maps.InfoWindow();
// Custom markers
var icon = "img/marker.png";

// Define the list of markers.
// This could be generated server-side with a script creating the array.
var markers = [
{ val:0, lat: -40.149049, lng: 172.033095, title: "Title", html: "<div style='text-align:left'><h4 style='color:#0068a6;font-size:16px;margin:0px 0px 10px 0px;'>Title</h4><strong>Telephone</strong><br /><br />Address</div>" },
{ val:1, lat: -41.185765, lng: 174.827516, title: "Title", html: "<div style='text-align:left'><h4 style='color:#0068a6;font-size:16px;margin:0px 0px 10px 0px;'>Title</h4><strong>Telephone</strong><br /><br />Address</div>" },
];
// Create the markers ad infowindows.
for (index in markers) addMarker(markers[index]);
function addMarker(data) {
// Create the marker
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.lat, data.lng),
map: map,
title: data.title,
icon: icon,
id: data.val
});

// Create the infowindow with two DIV placeholders
// One for a text string, the other for the StreetView panorama.
var content = document.createElement("DIV");
var title = document.createElement("DIV");
title.innerHTML = data.html;
content.appendChild(title);
// Open the infowindow on marker click
google.maps.event.addListener(marker, "click", function() {
infowindow.setContent(content);
infowindow.open(map, this);
map.setCenter(this.position);
console.log(this.id);
});
}

// Zoom and center the map to fit the markers
// This logic could be conbined with the marker creation.
// Just keeping it separate for code clarity.
var bounds = new google.maps.LatLngBounds();
for (index in markers) {
var data = markers[index];
bounds.extend(new google.maps.LatLng(data.lat, data.lng));
}
map.fitBounds(bounds);
}

<p id="1">link to open marker</p>

如有任何帮助,我们将不胜感激

理查德:)

最佳答案

<a href="javascript:show(7)">The Golden Goose</a>

然后在你的js中有一个打开信息窗口的函数(例如show()),它从该链接获取属性(打开id 7)。

<小时/>
function show(id){
myid = id;
if(markers[myid]){
map.panTo(markers[myid].getPoint());
setTimeout('GEvent.trigger(markers[myid], "click")',500);
map.hideControls();
}
}

这是我之前在 v2 中的标记管理器之一中使用的函数。您必须确保在设置每个标记时为其设置一个 id,然后才能调用它。

我确定的一件事(为了简化问题)是确保 map 标记集/数组与我在页面上使用的 sql 结果完全相同。这样,使用 id 就小菜一碟了。

关于javascript - 谷歌地图 v3 点击外部 html 链接打开信息窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15146789/

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