【问题标题】:Google Map Direction service Route谷歌地图方向服务路线
【发布时间】:2017-02-23 17:48:56
【问题描述】:

我想在两点之间绘制最短路径地图路线英里数。使用 Javascript-directionsService.route

【问题讨论】:

    标签: dictionary service routes direction


    【解决方案1】:

    点击第一次地图会创建起点,第二次点击地图会创建第二个点并绘制路线

     var map;
        var infowindow = new google.maps.InfoWindow();
        var wayA;[![enter image description here][1]][1]
        var wayB;
        var geocoder = new google.maps.Geocoder();
        var directionsDisplay = new google.maps.DirectionsRenderer({
            suppressMarkers: true,
            panel: document.getElementById('right-panel'),
            draggable: true
        });
        var directionsService = new google.maps.DirectionsService();
        var data = {};
        initMap();
        function initMap() {
            debugger;
            map = new google.maps.Map(document.getElementById('rmap'), {
                center: new google.maps.LatLng(23.030357, 72.517845),
                zoom: 15
            });
            google.maps.event.addListener(map, "click", function (event) {
                if (!wayA) {
                    wayA = new google.maps.Marker({
                        position: event.latLng,
                        map: map,
                        icon: "https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=S|00FF00|000000"
                    });
                } else {
                    if (!wayB) {
                        debugger;
                        wayB = new google.maps.Marker({
                            position: event.latLng,
                            map: map,
                            icon: "https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=E|FF0000|000000"
                        });
                        calculateAndDisplayRoute(directionsService, directionsDisplay, wayA, wayB);
                    }
                }
            });
        }
        function computeTotalDistance(result) {
            var total = 0;
            var myroute = result.routes[0];
            for (var i = 0; i < myroute.legs.length; i++) {
                total += myroute.legs[i].distance.value;
            }
            total = total / 1000;
            return total;
        }
        function computeTotalDuration(result) {
            var total = 0;
            var myroute = result.routes[0].legs[0].duration.text;
            return myroute;
        }
        function calculateAndDisplayRoute(directionsService, directionsDisplay, wayA, wayB) {
            debugger;
            directionsDisplay.setMap(map);
            google.maps.event.addListener(directionsDisplay, 'directions_changed', function () {
                debugger;
                calculateAndDisplayRoute(directionsService, directionsDisplay.getDirections(), wayA, wayB);
            });
            directionsService.route({
                origin: wayA.getPosition(),
                destination: wayB.getPosition(),
                optimizeWaypoints: true,
                travelMode: 'DRIVING'
            }, function (response, status) {
                if (status === 'OK') {
                    debugger;
                    var route = response.routes[0];
                    wayA.setMap(null);
                    wayB.setMap(null);
                    pinA = new google.maps.Marker({
                        position: route.legs[0].start_location,
                        map: map,
                        icon: "https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=S|00FF00|000000"
                    }),
                    pinB = new google.maps.Marker({
                        position: route.legs[0].end_location,
                        map: map,
                        icon: "https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=E|FF0000|000000"
                    });
                    google.maps.event.addListener(pinA, 'click', function () {
                        infowindow.setContent("<b>Route Start Address = </b>" + route.legs[0].start_address + " <br/>" + route.legs[0].start_location);
                        infowindow.open(map, this);
                    });
                    google.maps.event.addListener(pinB, 'click', function () {
                        debugger;
                        infowindow.setContent("<b>Route End Address = </b>" + route.legs[0].end_address + " <br/><b>Distance=</b> " + computeTotalDistance(directionsDisplay.getDirections()) + " Km <br/><b>Travel time=</b> " + computeTotalDuration(directionsDisplay.getDirections()) + " <br/> " + route.legs[0].end_location);
                        infowindow.open(map, this);
                    });
                } else {
                    window.alert('Directions request failed due to ' + status);
                }
                directionsDisplay.setDirections(response);
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 2016-09-18
      相关资源
      最近更新 更多