Google Maps V3 InfoWindow ignores latLng position

There are no markers in my map. In response to a click on the map, a pop-up window appears with the Lat / long indicator, shown in decimal up to 5 dp and in degrees in minutes seconds. An open window is always closed before responding to a new click. The position is set by the position: latLng, but the info definition is always in the upper left corner. I spent a few days on it, and I feel that something is missing. The following is a snippet of code. Any ideas?

google.maps.event.addListener(map, 'click', function (event) {
    var lat = event.latLng.lat(),
        lng = event.latLng.lng(),
        latLng = event.latLng,
        strHtml;
    //
    // strHtml build code omitted but succesfully uses lat and lng to produce
    // e.g. Latitude : 51.72229 N (51° 43' 20'' N)
    //      Longitude : 1.45827 E (1° 27' 30'' E)
    //
    // If an infowindow is open, then close it
    if (openedInfoWindow != null) openedInfoWindow.close();
    // latLng monitored to check it is there before use in infowindow
    alert(latLng); // returns correct values which position then ignores!
    var infoWindow = new google.maps.InfoWindow({
        position: latLng, 
        content: strHtml,
        maxWidth: 420
    });
    // Open infoWindow
    infoWindow.open(map,this);
    // Remember the opened window
    openedInfoWindow = infoWindow;
    // Close it if X box clicked
    google.maps.event.addListener(infoWindow, 'closeclick', function() {
    openedInfoWindow = null; 
    });    
});
+4
source share
2 answers

It is assumed that the second (optional) argument infoWindow.open()will be MVCObject, which provides the position property.

this google.maps.Map -Instance (map), MVCObject, position -property.

infoWindow.open(map,this);.

+4

. infowindow open MVCObject Core API, Marker . infowindow null infowindow. , . , .

: http://jsfiddle.net/bryan_weaver/z3Cdg/

:

google.maps.event.addListener(map, 'click', function (evt) {
       var content = "<div class='infowindow'>";
       content += "Latitude: " + evt.latLng.lat() + "<br/>";
       content += "Longitude: " + evt.latLng.lng() + "</div>";
       HandleInfoWindow(evt.latLng, content);
});

function HandleInfoWindow(latLng, content) {
    infoWindow.setContent(content);
    infoWindow.setPosition(latLng);
    infoWindow.open(map);
}
+5

All Articles