gpt4 book ai didi

javascript - 如何获取点击标记的信息窗口

转载 作者:行者123 更新时间:2023-11-30 06:32:19 25 4
gpt4 key购买 nike

我想知道如何在单击标记时打开信息窗口,我认为这是正确的,但不起作用。这是脚本

var map;
function initialize() {
navigator.geolocation.getCurrentPosition(function(position){
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var coords = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
disableDefaultUI: true,
center: coords,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
placeMarker();
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(marker, 'click', function() {
getComment(marker, infowindow);
});
});
}
function getComment(marker, infowindow){
var message = "Hello";
infowindow.setContent(message);
infowindow.open(map, marker);
}
function placeMarker() {
//place some marker
}
google.maps.event.addDomListener(window, 'load', initialize);

感谢任何建议!

最佳答案

没有在 jsfiddle 中运行它,但下面的代码应该可以工作。

您必须在函数 placeMarker() 之外定义 var marker,就像您在 infoWindow 中那样。

var map;
function initialize() {
navigator.geolocation.getCurrentPosition(function(position){
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var coords = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
disableDefaultUI: true,
center: coords,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

//position of marker = coords in this example
var marker = new google.maps.Marker({
position: coords,
map: map,
title: "randomTitle",
zIndex: Math.round(latlng.lat()*-100000)<<5
});
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(marker, 'click', function() {
getComment(marker, infowindow);
});
});
}
function getComment(marker, infowindow){
var message = "Hello";
infowindow.setContent(message);
infowindow.open(map, marker);
}
google.maps.event.addDomListener(window, 'load', initialize);

关于javascript - 如何获取点击标记的信息窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16792353/

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