【发布时间】:2018-03-19 15:52:53
【问题描述】:
希望你能帮助我。我的网站上出现了谷歌地图,使用 Angular JS 1。(虽然很多代码是 vanilla js)。
在我的地图上,如果您搜索起点和终点,标记会下降并显示从起点到终点的谷歌路线。伟大的! :)
不过,我还编写了代码让餐厅也出现在此结果中。但是,由于 google 配额,在经过一定数量的 WayPoints(路线/腿/步)后,餐厅停止出现。
我只想在搜索结果中显示最多 3 个航点的餐厅结果(由于配额/API 限制)。
我该怎么做?
这是我的指令代码。
/* global google */
googleMap.$inject = ['Directions'];
function googleMap(Directions) {
console.log('map directive loaded');
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
});
$scope.$watch('center', () => map.setCenter($scope.center), true);
$scope.$watchGroup(['origin', 'destination', 'travelMode'],
displayRoute, displayRestaurants);
// DISPLAY ROUTE
function displayRoute() {
if (!$scope.origin || !$scope.destination || !$scope.travelMode)
return false;
const directionsDisplay = new google.maps.DirectionsRenderer();
const placesService = new google.maps.places.PlacesService(map);
directionsDisplay.setMap(map);
Directions.route({
origin: $scope.origin,
destination: $scope.destination,
travelMode: $scope.travelMode
}, (response) => {
directionsDisplay.setDirections(response);
response.routes[0].legs[0].steps.map(step => {
placesService.nearbySearch({
location: step.start_point,
radius: 50,
type: ['restaurant']
}, (results) => {
results.map(place => {
return new google.maps.Marker({
map: map,
position: place.geometry.location
});
});
});
});
});
}
// DISPLAY RESTAURANTS
function displayRestaurants() {
console.log('display restaurants function');
}
}
};
}
export default googleMap;
【问题讨论】:
标签: javascript google-maps google-places-api