vendredi 21 décembre 2018

Distance Finding in ASP.net No Error is Found But Also No Required result is Found

I used have this script code in asp.net mvc View file also included jquery and google maps reference files. It shows no error no warnings as well as no required result in Browser. Is it OK? What should i do? I need distance in kilometers as well as placeholder with location hints while typing address. Multiple locations also can be handled. Any API that can do this. Thanks For Your Precious Opinions. (*I found this is an easy way to find distance but it disappointed me.)

<script>
   (function () {
    google.maps.event.addDomListener(window, 'load', function () {
        var form_places = new google.maps.places.Autocomplete(document.getElementById('form_places'));
        var to_places = new google.maps.places.Autocomplete(document.getElementById('to_places'));

    google.maps.event.addListener(form_places, 'place_changed', function () {
        var form_place = form_place.getPlace();
        var form_address = form_place.formatted_address;
        $('origin').val(form_address);
    });
    google.maps.event.addListener(to_places, 'place_changed', function () {
        var to_place = to_place.getPlace();
        var to_address = to_place.formatted_address;
        $('destination').val(to_address);
    });
});

function CalculateDistance() {
    var origin = $('origin').val();
    var destination = $('destination').val();
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
        {
            origin: ['origin'],
            destination: ['destination'],
            travelmode: google.maps.TravelMode.DRIVING,
            unitSystem: google.maps.UnitSystem.metric,
            avoidHighways: false,
            avoidTolls: false
        }, callback);
}
   //get destance result
function callback(response, status) {
    if (!google.maps.DistanceMatrixStatus.OK) {
        $('result').html(err);
    } else {
        var origin = response.originAddresses[0];
        var destination = response.destinationAddresses[0];
        if (response.rows[0].elements[0].status === "ZERO_RESULTS") {
            $('result').html('No roads between' + origin + 'and ' + destination)
        } else {
            var distance = response.rows[0].elements[0].distance;
            var duration = response.rows[0].elements.duration;
            console.log(response.rows[0].elements[0].distance);
            var distance_in_Kilo = distance.value / 1000;
            var duration_text = duration.text;
            var duration_value = duration.value;
            $('#in_kilo').text(distance_in_kilo.ToFixed(2));
            $('#duration_text').text(duration_text.ToFixed(2));
            $('#duration_value').text(duration_value.ToFixed(2));
            $('#form').text(origin);
            $('#to').text(destination);

        }
    }
}
//submit button
$('#distance_form').submit(function (e) {
    e.preventDefault();
    CalculateDistance();
    });
});




Aucun commentaire:

Enregistrer un commentaire