1

我有信息窗口的问题。当我点击地图时我创建了标记,但我也想显示信息窗口。此代码不适用于此部分:

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,
        });
    }
}
4

1 回答 1

3

当它不起作用时,您应该查看 javascript 错误。您正试图在“标记”存在之前使用它。如果将标记的“单击”侦听器移动到它所在的 placeMarker 函数中,并使信息窗口全局可访问,它应该可以工作。您发布的代码没有在地图上放置任何标记。

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);
   });
 }
}

工作示例

于 2013-08-12T16:40:00.507 回答