|
| 1 | +// Gmaps |
| 2 | +if (jQuery('#map').length) { |
| 3 | + var infoWindow = new google.maps.InfoWindow(), |
| 4 | + jsonData = [], |
| 5 | + markers = [], |
| 6 | + markersPoints = [], |
| 7 | + markersPos = [], |
| 8 | + noms = [], |
| 9 | + iterator = 0, |
| 10 | + mapOptions = { |
| 11 | + mapTypeId: google.maps.MapTypeId.ROADMAP, |
| 12 | + center: new google.maps.LatLng(50.85,4.36), |
| 13 | + mapTypeControl: false, |
| 14 | + streetViewControl: false, |
| 15 | + zoom: 12 |
| 16 | + }, |
| 17 | + markerShape = { |
| 18 | + coord: [0,0, 27,0, 27,37, 0,37], |
| 19 | + type: 'poly' |
| 20 | + }, |
| 21 | + map = new google.maps.Map(document.getElementById('map'), mapOptions); |
| 22 | + // var markerCluster = new MarkerClusterer(map); |
| 23 | + |
| 24 | + google.maps.event.addListener(map, 'click', function() { |
| 25 | + infoWindow.close(); |
| 26 | + }); |
| 27 | + |
| 28 | + function getMarkerIcon(shape) { |
| 29 | + if ( ! shape) { |
| 30 | + shape = '/images/marker.png'; |
| 31 | + } |
| 32 | + return new google.maps.MarkerImage('/images/'+shape+'.png', |
| 33 | + new google.maps.Size(34, 44), |
| 34 | + new google.maps.Point(0, 0), |
| 35 | + new google.maps.Point(14, 36) |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + function drop() { |
| 40 | + if (markers.length <= 3) { |
| 41 | + for (var i = 0; i < markers.length; i++) { |
| 42 | + setTimeout(function() { |
| 43 | + addMarker(google.maps.Animation.DROP); |
| 44 | + }, i * 200); |
| 45 | + } |
| 46 | + } else { |
| 47 | + for (var i = 0; i < markers.length; i++) { |
| 48 | + addMarker(false); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + function addMarker(animation) { |
| 54 | + |
| 55 | + // decaler un marker s'il y a déjà un point à la même position |
| 56 | + var dedans = 0; |
| 57 | + for (var i = markers.length - 1; i >= 0; i--){ |
| 58 | + // console.log(markers[iterator].lat()); |
| 59 | + if (markersPos[iterator].lat() == markersPos[i].lat() && markersPos[iterator].lng() == markersPos[i].lng()) { |
| 60 | + // markers[i]['html'] += markers[iterator]['html']; |
| 61 | + dedans++; |
| 62 | + } |
| 63 | + }; |
| 64 | + var latLng; |
| 65 | + if (dedans >= 2) { |
| 66 | + // console.log(dedans+' '+iterator); |
| 67 | + // Il y a au moins deux points ayant la même position, alors on décale un des deux |
| 68 | + latLng = new google.maps.LatLng(markersPos[iterator].lat(), markersPos[iterator].lng() + Math.random() / 4000 + 0.00001); |
| 69 | + } else { |
| 70 | + latLng = markersPos[iterator]; |
| 71 | + } |
| 72 | + |
| 73 | + markersPoints[iterator] = new google.maps.Marker({ |
| 74 | + // icon: getMarkerIcon(markers[iterator]['shape']), |
| 75 | + icon: getMarkerIcon(), |
| 76 | + shape: markerShape, |
| 77 | + position: latLng, |
| 78 | + map: map, |
| 79 | + draggable: false, |
| 80 | + animation: animation |
| 81 | + }); |
| 82 | + // console.log(markers[iterator]); |
| 83 | + markersPoints[iterator].html = markers[iterator]['html']; |
| 84 | + markersPoints[iterator].id = markers[iterator]['id']; |
| 85 | + google.maps.event.addListener(markersPoints[iterator], 'click', onMarkerClick); |
| 86 | + if (markers.length == 1) { |
| 87 | + google.maps.event.trigger(markersPoints[iterator], 'click'); |
| 88 | + } |
| 89 | + iterator++; |
| 90 | + } |
| 91 | + |
| 92 | + function highlightAddress(markerId) { |
| 93 | + // console.log('marker : '+markerId); |
| 94 | + var item = jQuery('#item-' + markerId); |
| 95 | + if (item.length && ! item.hasClass('active')) { |
| 96 | + jQuery('.list-agencies .active').removeClass('active'); |
| 97 | + item.addClass('active'); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + function AutoCenter() { |
| 102 | + var bounds = new google.maps.LatLngBounds(); |
| 103 | + jQuery.each(markers, function (index, marker) { |
| 104 | + bounds.extend(markersPos[index]); |
| 105 | + }); |
| 106 | + map.fitBounds(bounds); |
| 107 | + var listener = google.maps.event.addListener(map, 'idle', function() { |
| 108 | + if (map.getZoom() > 17) map.setZoom(17); |
| 109 | + google.maps.event.removeListener(listener); |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + var onMarkerClick = function() { |
| 114 | + // console.log(this); |
| 115 | + highlightAddress(this.id); |
| 116 | + infoWindow.setContent(this.html); |
| 117 | + infoWindow.open(map, this); |
| 118 | + }; |
| 119 | + |
| 120 | + var apiUrl = '/api/places', |
| 121 | + placeId = $('#place').data('id'); |
| 122 | + if (placeId) { |
| 123 | + apiUrl = apiUrl + '/' + placeId; |
| 124 | + } |
| 125 | + jQuery.getJSON(apiUrl + location.search, function(data) { |
| 126 | + if ( ! jQuery.isArray(data) ) { |
| 127 | + jsonData = [data]; |
| 128 | + } else { |
| 129 | + jsonData = data; |
| 130 | + } |
| 131 | + for (var i = 0; i < jsonData.length; i++) { |
| 132 | + if ( jsonData[i].latitude > 0 && jsonData[i].longitude > 0) { |
| 133 | + markersPos[i] = new google.maps.LatLng(jsonData[i].latitude, jsonData[i].longitude); |
| 134 | + markers[i] = {}; |
| 135 | + var coords = []; |
| 136 | + markers[i]['id'] = jsonData[i].id; |
| 137 | + markers[i]['shape'] = jsonData[i].shape; |
| 138 | + markers[i]['html'] = '<h4>' + jsonData[i].title + '</h4>'; |
| 139 | + markers[i]['html'] += '<p>'; |
| 140 | + if (jsonData[i].address) coords.push(jsonData[i].address); |
| 141 | + if (jsonData[i].phone) coords.push('T ' + jsonData[i].phone); |
| 142 | + if (jsonData[i].fax) coords.push('F ' + jsonData[i].fax); |
| 143 | + if (jsonData[i].email) coords.push('<a href="mailto:' + jsonData[i].email + '">' + jsonData[i].email + '</a>'); |
| 144 | + if (jsonData[i].website) coords.push('<a href="' + jsonData[i].website + '" target="_blank">' + data[i].website + '</a>'); |
| 145 | + markers[i]['html'] += coords.join('<br>'); |
| 146 | + markers[i]['html'] += '</p>'; |
| 147 | + } |
| 148 | + } |
| 149 | + // console.log(markers); |
| 150 | + if (markers.length > 0) { |
| 151 | + drop(); |
| 152 | + AutoCenter(); |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + function getMarkerIcon(shape) { |
| 157 | + return new google.maps.MarkerImage('/img/marker-microstart.png', |
| 158 | + new google.maps.Size(34, 44), |
| 159 | + new google.maps.Point(0, 0), |
| 160 | + new google.maps.Point(14, 36) |
| 161 | + ) |
| 162 | + } |
| 163 | + |
| 164 | + // Search Postcode |
| 165 | + $('#search-cp button').click(function(){ |
| 166 | + |
| 167 | + var cp = $('#search-cp input').val(); |
| 168 | + |
| 169 | + if( ! cp.length) |
| 170 | + return false; |
| 171 | + |
| 172 | + var geocoder = new google.maps.Geocoder(); |
| 173 | + geocoder.geocode( |
| 174 | + { |
| 175 | + address: cp + ' Belgique' |
| 176 | + }, |
| 177 | + function(results_array, status) { |
| 178 | + var p = results_array[0].geometry.location; |
| 179 | + var lat = p.lat(); |
| 180 | + var lng = p.lng(); |
| 181 | + setDistancesForMarkers(lat, lng); |
| 182 | + } |
| 183 | + ); |
| 184 | + |
| 185 | + return false; |
| 186 | + }); |
| 187 | + |
| 188 | + // Compute distance between two points |
| 189 | + var rad = function(x) { |
| 190 | + return x * Math.PI / 180; |
| 191 | + }; |
| 192 | + |
| 193 | + function getDistance(p1_lat, p1_lng, p2_lat, p2_lng) { |
| 194 | + var R = 6378137; // Earth’s mean radius in meter |
| 195 | + var dLat = rad(p2_lat - p1_lat); |
| 196 | + var dLong = rad(p2_lng - p1_lng); |
| 197 | + var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + |
| 198 | + Math.cos(rad(p1_lat)) * Math.cos(rad(p2_lat)) * |
| 199 | + Math.sin(dLong / 2) * Math.sin(dLong / 2); |
| 200 | + var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); |
| 201 | + var d = R * c; |
| 202 | + return d; // returns the distance in meter |
| 203 | + }; |
| 204 | + |
| 205 | + // Get the distance between a given point and the markers |
| 206 | + function setDistancesForMarkers(lat, lng){ |
| 207 | + for (var i = 0; i < jsonData.length; i++) { |
| 208 | + if ( jsonData[i].latitude > 0 && jsonData[i].longitude > 0) { |
| 209 | + jsonData[i].distance = getDistance(lat, lng, jsonData[i].latitude, jsonData[i].longitude); |
| 210 | + // var km = (jsonData[i].distance/1000).toFixed(1); |
| 211 | + // var km = (jsonData[i].distance/1000).toFixed(0); |
| 212 | + // $('#item-'+jsonData[i].id).data('data-distance', km); |
| 213 | + // $('#item-'+jsonData[i].id+' .distance').html('(' + km + ' km)'); |
| 214 | + jsonData.sort(function(a,b) { |
| 215 | + return parseFloat(a.distance) - parseFloat(b.distance); |
| 216 | + }); |
| 217 | + // sortAgenciesList(); |
| 218 | + } |
| 219 | + } |
| 220 | + openMarkerId(jsonData[0].id); |
| 221 | + }; |
| 222 | + |
| 223 | + // function sortAgenciesList() { |
| 224 | + // var items = $('#list-agencies li').get(); |
| 225 | + // items.sort(function(a,b){ |
| 226 | + // var keyA = parseFloat($(a).data('data-distance')); |
| 227 | + // var keyB = parseFloat($(b).data('data-distance')); |
| 228 | + // if (keyA < keyB) return -1; |
| 229 | + // if (keyA > keyB) return 1; |
| 230 | + // return 0; |
| 231 | + // }); |
| 232 | + |
| 233 | + // var ul = $('#list-agencies'); |
| 234 | + // $.each(items, function(i, li) { |
| 235 | + // ul.append(li); |
| 236 | + // }); |
| 237 | + // } |
| 238 | + |
| 239 | +} |
| 240 | + |
| 241 | +function openMarkerId(id) { |
| 242 | + // console.log( 'click : '+id ); |
| 243 | + for (var i = markers.length - 1; i >= 0; i--){ |
| 244 | + // console.log( markers[i].id ); |
| 245 | + if (markers[i].id == id) { |
| 246 | + var latLng = new google.maps.LatLng(markersPos[i].lat(),markersPos[i].lng()); |
| 247 | + map.panTo( latLng ); |
| 248 | + map.setZoom(13); |
| 249 | + google.maps.event.trigger(markersPoints[i], 'click'); |
| 250 | + } |
| 251 | + } |
| 252 | + return false; |
| 253 | +} |
0 commit comments