Reputation: 29
So right clicking creates the markers but when i click on the marker the infowindow does not display.The commented alert gives the coordinates though. What could i be doing wrong. Is there a problem with the logic or the syntax. I can't find my way around this problem. Here is my code:
// create marker on right click
google.maps.event.addListener(map,'rightclick', function(e) {
marker = new google.maps.Marker({
position: e.latLng,
map: map
});
alert('coord: ' + marker.getPosition().toUrlValue(3));
});
// display info window on marker click
google.maps.event.addListener(marker,'click', function(event){
infowindow = new google.maps.InfoWindow({
map:map,
content:"coordinates:"+event.latLng.toUrlValue(),
position:event.latLng
});
infowindow.open(map,marker);
});
Upvotes: 0
Views: 967
Reputation: 5303
You should place the second event on same context of first one:
google.maps.event.addListener(map,'rightclick', function(e) {
var marker = new google.maps.Marker({
position: e.latLng,
map: map
});
google.maps.event.addListener(marker,'click', function(event){
infowindow = new google.maps.InfoWindow({
map: map,
content: "coordinates:"+event.latLng.toUrlValue(),
position: event.latLng
});
infowindow.open(map, marker);
});
});
Hope this helps.
Upvotes: 1
Reputation: 30855
you didn't open the infowindow by calling the open() of infowindow object. You initializing every time when you click on marker.
UPDATED
infowindow = new google.maps.InfoWindow({
map:map,
content:"coordinates:"+event.latLng.toUrlValue()
});
// display info window on marker click
google.maps.event.addListener(marker, 'click', function(event) {
infowindow.setPosition(event.latLng);
infowindow.open(marker);
});
try this code
https://developers.google.com/maps/documentation/javascript/overlays#InfoWindows
Upvotes: 0