本文介绍了删除先前的路线路线谷歌地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  

define directionsDisplay 只有一次(在点击 -handler之外)


element = document.getElementById('map-canvas');
map = new google.maps.Map(element, options);
var marker = new google.maps.Marker({
    position: new google.maps.LatLng(car.lat, car.long),
    map: map,
    title: car.make
});
google.maps.event.addListener(marker, 'click', directionsCallback(car));
function directionsCallback(car) {
    return function (e) {
        if( infowindow != null ) {
            infowindow.close();
        }
        var directionsService = new google.maps.DirectionsService();
        directionsDisplay = new google.maps.DirectionsRenderer();
        directionsDisplay.setMap(null);
        console.log(directionsDisplay);
        calcRoute = function(){
            var startPoint = document.getElementById('clientAddress').value;
            var finishPoint = new google.maps.LatLng(car.lat, car.long);
            var request = {
                origin: startPoint,
                destination: finishPoint,
                travelMode: google.maps.TravelMode.DRIVING
            };
            directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setMap(map);
                    directionsDisplay.setDirections(response);
                    console.log(directionsDisplay)
                    console.log(response.routes.length)
                    infowindow.close();
                }
                else{
                    alert('Sorry ! ' + givenAddress + ' not recognized')
                }
            });
        }
        infowindow = new google.maps.InfoWindow();
        infowindow.setContent("\n"
            + 'Directions to here : <input id = \'clientAddress\'       placeholder=\"Postcode or City\" type=\'text\'>'
            + ' <input type=\'button\' onClick=calcRoute() value=\'Go!\'>')
        infowindow.open(map, this);


    }
}
解决方案

define directionsDisplay only 1 time(outside of the click-handler)

这篇关于删除先前的路线路线谷歌地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:05
查看更多