1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| var directions_response = new Array();
function callDirectionAPIRequest(adress, mode_transport, destinations){
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [adress],
destinations: destinations,
travelMode: google.maps.TravelMode[mode_transport],
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function(response, status) {
console.log(status);
if (status == google.maps.DistanceMatrixStatus.OK) {
directions_response.push(response.rows);
}
});
}
$('#btn_distance').click(function() {
if ( $('#useradress').length > 0 ) {
var adress = $('#useradress').val();
var mode_transport = $('#travelmode').val();
// On charge le geojson contenant tous les points des PAVs
$.getJSON( "geojson/points.geojson").done(function( data ) {
var destinations1 = new Array();
var destinations2 = new Array();
var destinations3 = new Array();
var destinations4 = new Array();
var destinations5 = new Array();
var destinations6 = new Array();
$.each( data["features"], function( key, val ) {
var longitude = val["geometry"]["coordinates"][0];
var latitude = val["geometry"]["coordinates"][1];
if ( key < 25 ) {
destinations1.push(new google.maps.LatLng(latitude,longitude));
} else if ( key >= 25 && key < 50 ) {
destinations2.push(new google.maps.LatLng(latitude,longitude));
} else if ( key >= 50 && key < 75 ) {
destinations3.push(new google.maps.LatLng(latitude,longitude));
} else if ( key >= 75 && key < 100 ) {
destinations4.push(new google.maps.LatLng(latitude,longitude));
} else if ( key >= 100 && key < 125 ) {
destinations5.push(new google.maps.LatLng(latitude,longitude));
} else if ( key >= 125 && key < 150 ) {
destinations6.push(new google.maps.LatLng(latitude,longitude));
}
});
var obj = {
key1: destinations1,
key2: destinations2,
key3: destinations3,
key4: destinations4,
key5: destinations5,
key6: destinations6
};
$.each( obj , function( key, val ) {
callDirectionAPIRequest(adress, mode_transport, val);
});
});
} else {
// Le champ adresse est vide
}
}); |
Partager