Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何调用函数getInfoWindowEvent(marker)_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 如何调用函数getInfoWindowEvent(marker)

Javascript 如何调用函数getInfoWindowEvent(marker),javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,和许多在StackOverflow工作的人一样,我对Javascript和GoogleMapsAPI有些陌生。几年前,杰米萨·哈维(jamesaharvey)发布了一段代码,我被如何调用这段代码难住了 我有一个版本的项目使用infowindows,但它们必须手动关闭。这是一种更优雅的方式。我就是不能让它工作。我正在使用下面的 var infowindow = new google.maps.InfoWindow(); function getInfoWindowEvent(marker) {

和许多在StackOverflow工作的人一样,我对Javascript和GoogleMapsAPI有些陌生。几年前,杰米萨·哈维(jamesaharvey)发布了一段代码,我被如何调用这段代码难住了

我有一个版本的项目使用infowindows,但它们必须手动关闭。这是一种更优雅的方式。我就是不能让它工作。我正在使用下面的

var infowindow = new google.maps.InfoWindow();

function getInfoWindowEvent(marker) {
    infowindow.close()
    infowindow.setContent("huh?");
    infowindow.open(map, marker);
}
我有多个正确显示的标记

var MAPA = new google.maps.Marker({position: {lat: 12.3400, lng: -12.3450}, map: map, title: 'A Marker'});
var MAPB = new google.maps.Marker({position: {lat: 12.3450, lng: -12.3455}, map: map, title: 'B Marker'});
var MAPC = new google.maps.Marker({position: {lat: 12.3460, lng: -12.3460}, map: map, title: 'C Marker'});
我的假设是,我需要为每个标记添加一个侦听器,以便在单击标记时调用getInfoWindowEvent。像这样的

MAPA.addListener('click', getInfoWindowEvent(MAPA));
MAPB.addListener('click', getInfoWindowEvent(MAPB));
MAPC.addListener('click', getInfoWindowEvent(MAPC));

我知道我在这一路上错过了一些东西。谢谢

函数的第二个参数必须是函数指针,而不一定是函数。或者使用接受
MouseEvent
参数的函数,如下所示:

MAPA.addListener('click', getInfoWindowEvent);
或从函数返回函数(将执行第二个参数中的函数,其返回值用作单击函数):


function getInfoWindowEvent(marker) {
  return function(evt) {
    infowindow.close()
    infowindow.setContent("huh?<br>"+this.getPosition().toUrlValue(6));
    infowindow.open(map, marker);
  }
}