【发布时间】:2018-03-20 04:00:33
【问题描述】:
我想知道你是否可以帮助我。通过 angular js v1,我的网页中嵌入了一张地图,其中:
- 允许用户输入出发地和目的地。
- 绘制标记并显示从起点到终点的路线。
- 显示餐厅,(作为标记)用于起点、目的地和中间航点的航点。 (所以 3 分,我不要过度使用 google api 请求/限制。
问题: 当我点击我的标记时,我的信息窗口没有出现。 默认情况下,它似乎自然地出现在起点和目的地,但我不知道如何让它们出现在所有显示为标记的餐厅中。 (为此使用 PlaceSearch)。
我用谷歌搜索了很多,但由于我是 JS/Angular 的新手,我无法找出最佳方法。
我的指令代码在下面,带有一些 infoWindow 代码,但正如您所见,我被难住了。不确定我是否需要点击处理程序?
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;
谢谢!
【问题讨论】:
标签: javascript angularjs google-maps infowindow