1var magasins = []; 2 var clientLocation = {}; 3 var magasins_proche = []; 4 var rayon = 140; //rayon de recherche d'un magasin en km 5 var nb_mags_affiches = 4; 6 7 magasins.push({nom:'',cp:'',url:'?page=findmag&code_magasin=',coord:{lat:0,lon:0,distance:0}}); 8 9 10 11 12 var rad = function(x) { 13 return x * Math.PI / 180; 14 }; 15 16 var getDistance = function(p1, p2) { 17 var R = 6378; // rayon approx de la terre en km 18 var dLat = rad(p2.lat - p1.lat); 19 var dLong = rad(p2.lng - p1.lng); 20 var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + 21 Math.cos(rad(p1.lat)) * Math.cos(rad(p2.lat)) * 22 Math.sin(dLong / 2) * Math.sin(dLong / 2); 23 var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 24 var d = R * c; 25 return d; // la distance en km 26 }; 27 28 function is_he_nearby(coord_mag, coord_customer) { 29 var distance = getDistance({ 30 lat: coord_mag.lat, 31 lng: coord_mag.lon 32 }, { 33 lat: coord_customer.lat(), 34 lng: coord_customer.lng() 35 }); 36 37 return { 38 distance: distance, 39 near: (distance < rayon) 40 }; 41 } 42 43 function tri_mag(a, b) { 44 if (a.distance < b.distance) return -1; 45 else if (a.distance == b.distance) return 0; 46 else return 1; 47 } 48 49 function find_mag(coord_custom,cible) { 50 clientLocation = {lat:coord_custom.lat(), long:coord_custom.lng()} 51 magasins_proche = [] 52 for (var i in magasins) { 53 var isi_near = is_he_nearby(magasins[i].coord, coord_custom) 54 if (isi_near.near) { 55 magasins[i].distance = isi_near.distance 56 magasins_proche.push(magasins[i]) 57 } 58 } 59 magasins_proche.sort(tri_mag) 60 draw(cible) 61 } 62 63 function reset(){ 64 input_adresse1.blur(); 65 setTimeout(function(){input_adresse1.val = '';},10); 66 } 67 68 function draw(cible) { 69 var retour_html = ""; 70 var nb_mags = 0; 71 for (var i in magasins_proche) { 72 nb_mags++; 73 if(nb_mags > nb_mags_affiches) break; 74 var mag = magasins_proche[i]; 75 retour_html += "<p class='magasins-proches-item'><a href='" + mag.url + "'><strong>" + mag.nom + "</strong> - "+mag.cp +" <span>"+Math.round(mag.distance) + "km</span></a></p>"; 76 } 77 document.getElementById(cible).innerHTML = retour_html; 78 } 79 80 /** 81 * Initialise un autocomplete google place et renvoi une référence 82 * id_recherche : id de l'input pour la recherche (ex: 'dptmag') 83 */ 84 function initGooglePlacesAutocomplete(id_recherche) { 85 return new google.maps.places.Autocomplete(document.getElementById(id_recherche), { 86 types: '(regions)', 87 componentRestrictions: { 88 country: 'fr' 89 } 90 }); 91 } 92 93 /** 94 * Initialise la cible des résultats de google place 95 * autocomplete : référence autocomplete google place renvoyé par initGooglePlacesAutocomplete 96 * id_resultats : id de la cible pour les résultats (ex : 'contenu-recherche') 97 */ 98 function initGooglePlacesResults (autocomplete, id_resultats, callback){ 99 if(typeof autocomplete !== 'undefined'){ 100 google.maps.event.addListener(autocomplete, 'place_changed', function() { 101 place = autocomplete.getPlace(); 102 find_mag(place.geometry.location,id_resultats); 103 if(callback) callback(); 104 }); 105 } 106 } 107 108 var cible_resultats = 'contenu-recherche'; 109 if ( typeof autocomplete != 'undefined') { 110 google.maps.event.addListener(autocomplete, 'place_changed', function() { 111 place = autocomplete.getPlace(); 112 find_mag(place.geometry.location,cible_resultats); 113 }); 114 }
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.