最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps API v3 marker on click with infowindow - Stack Overflow

programmeradmin5浏览0评论

I have problem with infowindow. I create marker when i click on map but i want to show infowindow too. This code not working with this part:

google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map, marker);
});

Here is code below.. Thank for help

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

I have problem with infowindow. I create marker when i click on map but i want to show infowindow too. This code not working with this part:

google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map, marker);
});

Here is code below.. Thank for help

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,
        });
    }
}
Share Improve this question edited Sep 23, 2013 at 12:46 Darius Kucinskas 10.7k12 gold badges60 silver badges80 bronze badges asked Aug 12, 2013 at 16:07 RaoldRaold 1,4934 gold badges22 silver badges34 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

You should look at the javascript errors when it doesn't work. You are attempting to use "marker" before it exists. If you move the 'click' listener for the marker into the placeMarker function where it exists, and make the infowindow globally accessible, it should work. The code you posted doesn't put any markers on the map.

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

working example

发布评论

评论列表(0)

  1. 暂无评论