-2

Here,I am using Some basic code for draw the route in php laravel.While I run the map getting that errors because of multiple requests sent at one time to google maps javascript API.

<script>
function initMap(item) {

    /* Convert php variable to json*/    
    var data = <?php echo json_encode($tracking['live_tracking']); ?>
    var locdata = <?php echo json_encode($tracking['location_tracking']); ?>

    var startPoint;
    var endPoint;
    var waypts = [];
    var map = new google.maps.Map(document.getElementById("map"), {
        zoom: 15,
        mapTypeId: 'terrain'
    });
    var length = data.length - 1
    var loclength = locdata.length

    for (let j = 0; j < loclength; j++) {

        markers = new google.maps.Marker({
            position: {
                lat: Number(locdata[j].lat),
                lng: Number(locdata[j].long)
            },
            map,
            title: "Hello World!",
        });    
    }
    for (let i = 0; i < length; i++) {
        startPoint = new google.maps.LatLng(data[i].lat, data[i].long)/* convert to latlng*/
        endPoint = new google.maps.LatLng(data[i + 1].lat, data[i + 1].long)/* convert to latlng*/

        waypts.push({
            location: new google.maps.LatLng(data[i].lat, data[i].long),/* convert to latlng*/
            stopover: true
        });

        setTimeout(function() {
            route(startPoint, endPoint, waypts, map, data)
        }, 1000)
    }
}

function route(startPoint, endPoint, waypts, map, data) {

    var directionsService = new google.maps.DirectionsService;
    var directionsDisplay = new google.maps.DirectionsRenderer;
    directionsDisplay.setMap(map);
    directionsService.route({
        origin: startPoint,
        destination: endPoint,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    }, function(response, status) {
        if (status === 'OK') {
            $('#map').show();
            directionsDisplay.setDirections(response);
        } else {
            $('#map').hide();
            initMap(data)
        }
    });
}

window.initMap = initMap;
</script>
RiggsFolly
  • 89,708
  • 20
  • 100
  • 143

0 Answers0