Compare if there is a point on a given route

Asked

Viewed 676 times

2

I have a page that calculates the distance between the Point A and the Point B, but I need to compute tolls on this route.

I think of having a bank with the coordinates (lat and lng) of the toll and check if it exists on the route, but do not know how to.

I need to return the amount of tolls and values of them in the interval Point A - Point B. Any help will be valid.

Rota ponto A ao ponto B

Javascript:

var options = {
			componentRestrictions: {country: 'br'}
		};	
        var source, destination;
        var directionsDisplay;
        var directionsService = new google.maps.DirectionsService();
        google.maps.event.addDomListener(window, 'load', function () {
            new google.maps.places.Autocomplete(document.getElementById('txtSource'), options);
            new google.maps.places.Autocomplete(document.getElementById('txtDestination'), options);
            directionsDisplay = new google.maps.DirectionsRenderer({ 'draggable': true });
        });
		
		    var mumbai = new google.maps.LatLng(-19.9166813, -43.9344931);
            var mapOptions = {
                zoom: 5,
                center: mumbai
            };
			
            map = new google.maps.Map(document.getElementById('dvMap'), mapOptions);


        function GetRoute() {
            directionsDisplay.setMap(map);
            //directionsDisplay.setPanel(document.getElementById('dvPanel'));

            //*********DIRECOES E ROTAS**********************//
            source = document.getElementById("txtSource").value;
            destination = document.getElementById("txtDestination").value;
			
			
            var request = {
                origin: source,
                destination: destination,
                travelMode: google.maps.TravelMode.DRIVING
            };
            directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                }
            });

            //*********DISTANCIA E DURACAO**********************//
            var service = new google.maps.DistanceMatrixService();
            service.getDistanceMatrix({
                origins: [source],
                destinations: [destination],
                travelMode: google.maps.TravelMode.DRIVING,
                unitSystem: google.maps.UnitSystem.METRIC,
                avoidHighways: false,
                avoidTolls: false
            }, 
			
			function (response, status) {
                if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
                    var distance = response.rows[0].elements[0].distance.text;
                    var duration = response.rows[0].elements[0].duration.text;
                    var dvDistance = document.getElementById("dvDistance");
                    dvDistance.innerHTML = "";
                    dvDistance.innerHTML += "Distancia: " + distance + "<br />";
                    dvDistance.innerHTML += "Duração:" + duration + "<br />";

                } else {
                    alert("Não foi possível traçar essa rota.");
                }
            });
        }

1 answer

1

Google Maps does not provide the option to see how many tolls there are on a route. Unlike various Google Maps conditions, you cannot enter within a code for it to calculate the amount of x locations on a specific route.

However, you can enter a condition for ALL the routes it calculates to avoid tolls. Just enter avoid=tolls (Toll, in English, is toll) on the API call link. In this case, the link will look like this:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&libraries=places,geometry&avoid=tolls" async defer></script>

Remembering that Google will not prohibit showing roads by tolls, will only give priority to routes that have no tolls.

Complete documentation of the resource at this link

Ah, by increasing the response, Google allows you to avoid, in addition to tolls, highways and ferries. In this case, just put one or all items here: avoid=tolls|highways|ferries

  • 1

    I need to calculate the toll values on the route, I searched and saw that only with the Google API this is not possible, so I think of using an external database with the location, price, etc. of tolls and cross-reference this information with the route of Google Maps. Thank you.

  • @J. Arc therefore, it would take another database to have this data. At the moment, I don’t know any available for free use, but check this site here: http://www.abcr.org.br/TarifasPedagio/TarifaPedagio.aspx

  • 1

    Gabriel Polidoro, thank you so much for your help, I’m researching the subject.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.