我对infowindow有疑问。我点击地图时创建标记,但我也想显示信息窗口。此代码不适用于此部分:
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
以下是代码..感谢您的帮助
var pridat;
var map;
function initialize() {
var locc = new google.maps.LatLng(49.938682,17.903331);
var mapOptions = {
zoom: 14,
center: locc,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
var contentwindow = '<div>your point</div>'
var infowindow = new google.maps.InfoWindow({
content: contentwindow
});
// IF I REMOVE THIS PART -> IT WORKS, BUT WITHOUT INFOWINDOW
google.maps.event.addListener(marker, 'click', function(){
infowindow.open(map, marker);
});
// END OF PART
google.maps.event.addListener(map, 'rightclick', function(event) {
placeMarker(event.latLng);
});
}
function placeMarker(location) {
if (marker) {
marker.setPosition(location);
} else {
marker = new google.maps.Marker({
position: location,
map: map,
title: 'My point',
draggable: true,
});
}
}
答案 0 :(得分:3)
当它无法正常工作时,您应该查看javascript错误。您正在尝试使用&#34; marker&#34;在它存在之前。如果您移动“点击”按钮监听器将标记放入placeMarker函数所在的位置,并使infowindow全局可访问,它应该可以工作。您发布的代码并未在地图上放置任何标记。
var pridat;
var map;
var marker = null;
var infowindow = null;
function initialize() {
var locc = new google.maps.LatLng(49.938682,17.903331);
var mapOptions = {
zoom: 14,
center: locc,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
var contentwindow = '<div>your point</div>'
infowindow = new google.maps.InfoWindow({
content: contentwindow
});
google.maps.event.addListener(map, 'rightclick', function(event) {
placeMarker(event.latLng);
});
}
function placeMarker(location) {
if (marker) {
marker.setPosition(location);
} else {
marker = new google.maps.Marker({
position: location,
map: map,
title: 'My point',
draggable: true,
});
// IF I REMOVE THIS PART -> IT WORKS, BUT WITHOUT INFOWINDOW
google.maps.event.addListener(marker, 'click', function(){
infowindow.open(map, marker);
});
}
}