Jump to content
php.lv forumi

Google location API


Emis

Recommended Posts

Sveiki! 
Ir gatavs jQuery skrips, kurš no google matrix un place izvelk distanci, starp lokācijām. Tehniski skripts strādā, bet netieku skaidrībā, kadēļ šo aprēķināto distanci nevar izprintēt.
Ievadot datus laukos, pārlūka konsole datus izvada pareizi, bet iekš html neprintē. Varbūt, kāds var parādīt, kā pēc šī skripta var atsevišķi izprintēt distance.value.

Paldies!

$(function() {
        // add input listeners
        google.maps.event.addDomListener(window, 'load', function () {
            var from_places = new google.maps.places.Autocomplete(document.getElementById('from_places'));
            var to_places = new google.maps.places.Autocomplete(document.getElementById('to_places'));

            google.maps.event.addListener(from_places, 'place_changed', function () {
                var from_place = from_places.getPlace();
                var from_address = from_place.formatted_address;
                $('#origin').val(from_address);
            });

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

        });
        // calculate distance
        function calculateDistance() {
            var origin = $('#origin').val();
            var destination = $('#destination').val();
            var service = new google.maps.DistanceMatrixService();
            service.getDistanceMatrix(
                {
                    origins: [origin],
                    destinations: [destination],
                    travelMode: google.maps.Polyline.boolean,
                    //unitSystem: google.maps.UnitSystem.IMPERIAL, // miles and feet.
                    unitSystem: google.maps.UnitSystem.metric, // kilometers and meters.
                    avoidHighways: false,
                    avoidTolls: false
                }, callback);
        }
        // get distance results
        function callback(response, status) {
            if (status != 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("Better get on a plane. There are no roads between "  + origin + " and " + destination);
                } else {
                    var distance = response.rows[0].elements[0].distance;
                    var duration = response.rows[0].elements[0].duration;
                    console.log(response.rows[0].elements[0].distance);
                    var distance_in_kilo = distance.value / 1000; // the kilom
                    var distance_in_mile = distance.value / 1609.34; // the mile
                    var duration_text = duration.text;
                    var duration_value = duration.value;
                    $('#in_mile').text(distance_in_mile.toFixed(2));
                    $('#in_kilo').text(distance_in_kilo.toFixed(2));
                    $('#duration_text').text(duration_text);
                    $('#duration_value').text(duration_value);
                    $('#from').text(origin);
                    $('#to').text(destination);
                }
            }
        }
        // print results on submit the form
        $('#distance_form').submit(function(e){
            e.preventDefault();
            calculateDistance();
        });

    });

 

Link to comment
Share on other sites

14 hours ago, e-remit said:

No konsoles tos datu laukos ierakstīt vari? Kas ir tie '#in_mile ' un '#in_kilo'? Kādi DIV vai SPAN tagi, vai varbūt INPUT lauki? Pēdējā gadījumā to jāaizpilda ar val(), nevis text().

Paldies par atbildi. Es jau paspēju atrast citu, daudz vienkāršāku variantu izmantojot phpm kas pat manā situācija der labāk, kā arī strādā un viss pareizi. 

Bet varbūt vari padalīties ar info. Šobrīd situācija ir tāda, ka man distanci aprēķina izmantojot ceļus, šodejas utt, bet man vajadzētu lai izmēra pa taisnu līniju(pa gaisu).
Vai āķis ir tajā, kādu srciptu es embedoju no google API? respektīvi, šobrīd ir 

https://maps.googleapis.com/maps/api/js?libraries=places&language=en&.....

bet varbūt ir jāizmanto matrix API ar tam piederošo embedojamo skriptu?

Paldies!

 

P.s. screenshots no iepriekšējās sāpes.
 

 

Capture.JPG

Edited by Emis
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...