2016-01-13 2 views
1

Я пытаюсь закрыть POI InfoWindow, когда щелкните ссылку.Как закрыть POI InfoWindow?

Я перекрытая в setContent из InfoWindow вроде этого:

//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

'Uncaught ReferenceError: FX не defined' – geocodezip

+0

уаг FX = Google .maps.InfoWindow.prototype.setContent; –

ответ

1

В вашей функции переопределения зафиксируйте глобальную ссылку на инфо-окно, чтобы вы могли ссылаться на нее, чтобы закрыть ее.

Ваше переопределение инфо-окна не работает. Я взял рабочую версию из этого вопроса: 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

О, ладно спасибо! –

Смежные вопросы