2016-01-13 61 views
1

我試圖在點擊鏈接時關閉POI的InfoWindow如何關閉POI的InfoWindow?

我overrided信息窗口的這樣的setContent:

//override the built-in setContent-method 
google.maps.InfoWindow.prototype.setContent = function (content) {    
    content = content.innerHTML + '<br/> <a href="#" onclick="onSavePlace();">Save place</a>'; 
    //run the original setContent-method 
    fx.apply(this, arguments); 
}; 

注:我沒有創建任何InfoWindow對象參考使用close()方法。

+0

'未捕獲的ReferenceError:FX不defined' – geocodezip

+0

VAR FX =谷歌.maps.InfoWindow.prototype.setContent; –

回答

1

在您的覆蓋函數中捕獲對infowindow的全局引用,以便您可以引用它來關閉它。

您對infowindow的覆寫不起作用。我參加了一個工作版本,從這樣一個問題:How to get a click event when a user clicks a (business) place on the map

proof of concept fiddle

代碼片段:

var geocoder; 
 
var map; 
 
var infowindow; 
 

 
//keep a reference to the original setPosition-function 
 
var fx = google.maps.InfoWindow.prototype.setPosition; 
 

 
// from https://stackoverflow.com/questions/24234106/how-to-get-a-click-event-when-a-user-clicks-a-business-place-on-the-map/24234818#24234818 
 
//override the built-in setPosition-method 
 
google.maps.InfoWindow.prototype.setPosition = function() { 
 
    //logAsInternal isn't documented, but as it seems 
 
    //it's only defined for InfoWindows opened on POI's 
 
    if (this.logAsInternal) { 
 

 
    // save reference in global infowindow variable. 
 
    infowindow = this; 
 

 
    google.maps.event.addListenerOnce(this, 'map_changed', function() { 
 
     var map = this.getMap(); 
 
     //the infoWindow will be opened, usually after a click on a POI 
 
     if (map) { 
 
     //trigger the click 
 
     google.maps.event.trigger(map, 'click', { 
 
      latLng: this.getPosition() 
 
     }); 
 
     } 
 
    }); 
 
    } 
 
    //call the original setPosition-method 
 
    fx.apply(this, arguments); 
 
}; 
 

 
function initialize() { 
 
    var map = new google.maps.Map(
 
    document.getElementById("map_canvas"), { 
 
     center: new google.maps.LatLng(37.4419, -122.1419), 
 
     zoom: 13, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 
    google.maps.event.addDomListener(document.getElementById('btn'), 'click', function(e) { 
 

 
    // close the last opened POI infowindow 
 
    infowindow.close(); 
 

 
    }); 
 

 
} 
 
google.maps.event.addDomListener(window, "load", initialize);
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<input id="btn" type="button" value="close IW" /> 
 
<div id="map_canvas"></div>

+0

哦,好的,謝謝! –

相關問題