gpt4 book ai didi

javascript - AngularJS ui-gmap-google-map 删除行车路线

转载 作者:可可西里 更新时间:2023-11-01 02:25:15 24 4
gpt4 key购买 nike

使用 AngularJS 和 ui-gmap-google-map 指令 ( http://angular-ui.github.io/angular-google-maps/#!/ ) 我已经创建了一个谷歌地图来绘制驾驶路线。当我尝试绘制不同的路线时,第一条路线仍保留在 map 上。我试过 directionsDisplay.setMap(null);删除旧路线,但这不起作用。谁能帮忙?

这是我的 Controller 代码的相关部分:

uiGmapIsReady.promise().then(function (map_instances) {
$scope.mapRef = $scope.map.control.getGMap();
});

$scope.getDirections = function (lat, long, origin, type) {
uiGmapGoogleMapApi.then(function (maps) {
var directionsService = new maps.DirectionsService();
var directionsDisplay = new maps.DirectionsRenderer();

if (origin == "" || type == "geo") {
origin = $scope.geoPosition.coords.latitude + ", " + $scope.geoPosition.coords.longitude;
}

var request = {
origin: origin,
destination: lat + ", " + long,
travelMode: maps.TravelMode['DRIVING'],
optimizeWaypoints: true
};

directionsService.route(request, function (response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setMap(null);
directionsDisplay.setMap($scope.mapRef);
directionsDisplay.setDirections(response);

} else {
console.log('Directions request failed due to ' + status);
}
});


});
}

最佳答案

我知道您正在使用 ui-map,但使用 ngmap ,你的问题可能更少

以下是 ngmap 显示两个方向的示例,您可以切换它以显示和隐藏方向。

http://plnkr.co/edit/Sv9Q4A?p=preview

<!doctype html>
<html ng-app="ngMap">
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=weather,visualization,panoramio"></script>
<script src="http://code.angularjs.org/1.2.25/angular.js"></script>
<script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
<script>
angular.module('ngMap').controller('MyCtrl', function($scope){
var vm = this;
vm.toggle = function() {
vm.show = !vm.show;
$scope.map.directionsRenderers.d1.setMap(vm.show ? $scope.map : null);
}
})
</script>
<link rel="stylesheet" href="style.css"/>
</head>
<body ng-controller='MyCtrl as vm'>
<map zoom="14" center="37.7699298, -122.4469157">
<directions id="d1" origin="toronto" destination="markham"></directions>
<directions id="d2" origin="etobicoke" destination="vaughn, on"></directions>
</map>
<a href="" ng-click="vm.toggle()">show/hide direction</a>
</body>
</html>

顺便说一句,我是 ngMap 的创建者。

关于javascript - AngularJS ui-gmap-google-map 删除行车路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32264507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com