Reputation: 1685
I was wondering if you could help me. Via angular js v1, I have a map embedded into my web page, which:
The Problem: When I click on my markers, my InfoWindow doesn't appear. It seems to naturally appear for origin and destination by default, but I can't figure out how to make them appear for all the restaurants that appear as markers. (Used PlaceSearch for this).
I've googled alot, but as I'm a newby to JS/Angular, I can't figure out the best approach.
My directive code is below, with some infoWindow code, but as you can see I'm stumped. Not sure if I need a click handler?
googleMap.$inject = [];
function googleMap() {
return {
restrict: 'E',
template: '<div class="google-map"></div>',
replace: true,
scope: {
center: '=',
zoom: '=',
origin: '=',
destination: '=',
travelMode: '='
},
link($scope, $element) {
const map = new google.maps.Map($element[0], {
zoom: $scope.zoom,
center: $scope.center
});
const directionsService = new google.maps.DirectionsService();
const directionsDisplay = new google.maps.DirectionsRenderer();
const placesService = new google.maps.places.PlacesService(map);
// const infoWindows = [];
// const infowindow = new google.maps.InfoWindow();
// let marker = new google.maps.Marker;
directionsDisplay.setMap(map);
$scope.$watch('center', () => map.setCenter($scope.center), true);
$scope.$watchGroup(['origin', 'destination', 'travelMode'],
displayRoute);
// DISPLAY ROUTE
function displayRoute() {
if(!$scope.origin || !$scope.destination || !$scope.travelMode)
return false;
directionsService.route({
origin: $scope.origin,
destination: $scope.destination,
travelMode: $scope.travelMode
}, (response) => {
directionsDisplay.setDirections(response);
// beginning of this form
// response.routes[0].legs[0].steps.map(step => {
const steps = response.routes[0].legs[0].steps
const lookup = [steps[0], steps[Math.round(steps.length / 2)],
steps[steps.length - 1]]
lookup.map(step => {
placesService.nearbySearch({
location: step.start_point,
radius: 50,
type: ['restaurant'],
openNow: true
}, (results) => {
results.map(place => {
console.log(place.name);
return new google.maps.Marker({
map: map,
position: place.geometry.location,
// label: '⭐️',
title: place.name
}); //google maps marker
});
results.map(place => {
console.log(place.vicinity);
const contentString = place.name;
return new google.maps.InfoWindow({
title: place.name,
content: contentString
}); //google maps marker
// infoWindows.push(infowindow);
});
});
}); //end of this function
}); //end return directionsdisplay
} //display route ends
} //link scope ends
};
}
export default googleMap;
Thank you!
Upvotes: 1
Views: 60
Reputation: 7338
One way to do this is to create the infowindow and marker - then bind a click event to open/close the infowindow -
var marker = new google.maps.Marker({
position: latlng,
map: mapObject,
title: "MARKER"
});
var infoWindow = new google.maps.InfoWindow({
content: "<h1>Hello World</h1>"
});
google.maps.event.addListener(marker, "click", function () {
infoWindow.open(mapObject, marker);
});
EDIT - In your case your are plotting an Array/List of markers and each marker has it's own info Window - so you could change the Plotting of Markers(results) code to something like this:
Note: compiler not used, may contain syntax errors
// keep reference of plotted markers, so we can clear them if required
var markers = [];
for (var i = 0; i < results.length; i++) {
var place = results[i];
// create marker
var marker = new google.maps.Marker({
map: mapObject,
position: place.geometry.location,
title: place.name
});
// create info window
var infoWindow = new google.maps.InfoWindow({
content: ''
});
//adding an extra property to marker, (infoWindow - so we can get it inside click event of marker
marker.infoWindow = infoWindow;
// click event handler
google.maps.event.addListener(marker, "click", function() {
// this == marker
var map = this.infoWindow.getMap();
if (map !== null && typeof map !== "undefined")
this.infoWindow.close();
else {
// open info window at marker position
this.infoWindow.open(mapObject, this);
}
});
markers.push(marker);
}
Upvotes: 1