简体   繁体   中英

Calculating the driving distance between two addresses Google API

This was always working with no problem until about a month ago when it does not calculate the distance anymore and show the error message instead. Any ideas on what is going on with this code and how to fix it?

function calcRoute() {
            var start = document.getElementById("start").value;
            var end = document.getElementById("end").value;
            var distanceInput = document.getElementById("distance");

            var request = {
                origin:start, 
                destination:end,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            };
            directionsService.route(request, function(response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                    distanceInput.value = 
                    response.routes[0].legs[0].distance.value / 1000;
                } else {
                //Show error message
                alert("Can't find road! Please try again!");
                clearRoute();
                }
            });
        } 

They started charging for it. You have to register at the following site, and start paying fees to use it:

https://developers.google.com/maps/documentation

I found the solution. I needed to enable (Directions API) to have it working again. Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM