javascript - Json Google maps matrix data with JS -
i want actual duration of specific route on googlemaps. dont quite it. code in js far:
$(document).ready(function duration() { var d = new date(); $.ajax({ url: 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=91058%20haupt%20str.%6+de&destinations=91058%20cauerstr.%208+de&mode=driving&language=de-de&sensor=false&traffic_model=best_guess&departure_time='+(d.gettime()+300000), datatype: "json" }).done(function(data) { var dur = data.rows[0].elements[0].duration.text; $("#duration").text(dur); }); });
in html want display
<div id="duration"></div>
can please me?! :) anyways
to want follow google's docs. cross-domain rules wont allow load resources requesting. code work: https://jsfiddle.net/lw3ls4yd/
first should register google maps api key: https://developers.google.com/maps/documentation/javascript/get-api-key
then add resource page, adding key in correct spot.
<script async defer src="https://maps.googleapis.com/maps/api/js?key'key here' type="text/javascript"></script>
then local js trick:
var distanceservice = new google.maps.distancematrixservice(); var d= new date(); distanceservice.getdistancematrix({ origins: [ "cauerstraße 8, 91058 erlangen, deutschland" ], destinations: [ "hauptstraße 6, 48607 ochtrup, deutschland" ], travelmode: google.maps.travelmode.driving, unitsystem: google.maps.unitsystem.metric, transitoptions: { departuretime:d }, durationintraffic: true, avoidhighways: false, avoidtolls: false }, function (response, status) { if (status !== google.maps.distancematrixstatus.ok) { console.log('error:', status); } else { console.log(response); $('#duration').text(response.rows[0].elements[0].duration.text); } });
Comments
Post a Comment