I am trying to display an infoWindow on click of each marker that is placed using the places query. I am using Geolocation to find the location and then display all of the nearby pubs. I would like to be able to click each marker and it display the name of the pub (place.name).
The error I get in the console when clicking on the marker is " Cannot read property 'setContent' of undefined ".
// Google Places code not including my geolocation code which works fine //
function initMap() {
myLatLng = new google.maps.LatLng(lat,long);
var Options = {
scrollwheel: false,
zoom:15,
center: myLatLng,
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'),Options);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
label: "Y",
});
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: myLatLng, //Uses geolocation to find the following
radius: 500,
types: ['bar']
}, callback);
};
function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
I have found this code that has the exact same problem as me, the infowindow doesn't load and the same error is in the console.
https://jsfiddle.net/jimedelstein/bfdhjsyy/
and here is the error I get in the console:
http://i.stack.imgur.com/2Pkeb.png
Thank you!
The infowindow variable isn't defined. Try infoWindow = new google.maps.InfoWindow
somewhere before you call infowindow.setContent();
.
Have a look here for details on how to use infoWindows: https://developers.google.com/maps/documentation/javascript/3.exp/reference#InfoWindow
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.