1// -------------------------------------------------- 2// MAPS PLUGIN v4.0 3// -------------------------------------------------- 4// MAKES GOOGLE MAPS API EASIER AND QUICKER TO USE 5// BUILT BY Matthew Pickard 6// Go to matt-pickard.dev/plugins to see full docs 7// Last edited on the 25/10/2016 8// -------------------------------------------------- 9var map; 10 11(function ( $ ) { 12 13 $.fn.mapCreate = function( options ) { 14 15 var $this = this; 16 17 // default options. 18 var settings = $.extend({ 19 // These are the defaults. 20 id: $this.attr('id'), 21 center: [52.551066, -0.320095], 22 zoom: 16, 23 load: 'scroll', 24 defaultMarker: { 25 position: undefined, 26 icon : 'https://mt0.google.com/vt/icon/name=assets/icons/spotlight/spotlight_poi-1-small.png&color=ff000000?scale=1', 27 animation: google.maps.Animation.DROP, 28 click: undefined 29 }, 30 arrMarkers: undefined, 31 styles : undefined, 32 mapOptions: { 33 disableDefaultUI: false, 34 scrollwheel: false, 35 panControl: false, 36 overviewMapControl: false, 37 mapTypeControl: false, 38 zoomControl: true, 39 mapTypeId: google.maps.MapTypeId.ROADMAP, 40 zoomControlOptions: {} 41 }, 42 loaderImg: undefined, 43 type: undefined, 44 selectTarget: undefined 45 }, options ); 46 47 48 $this.on('click', function(){ 49 !$this.hasClass('maploaded') && mapInitialize() 50 }) 51 /*if (settings.load == 'scroll') { 52 // check if timeoutScroll Plugin is enabled 53 var strScrollType = 'scroll'; 54 if (typeof(timoutScroll) == undefined) { 55 strScrollType = 'timoutScroll'; 56 } 57 $(window).on(strScrollType + ' load debouncedresize', function() { 58 if (!blnCreated && checkPlacement($this)) { 59 mapInitialize(); 60 } 61 }); 62 } else if (settings.load == 'click') { 63 $this.on('click', function(){ 64 !$this.hasClass('maploaded') && mapInitialize() 65 }) 66 } else { 67 mapInitialize(); 68 }*/ 69 70 var mapOptions, 71 blnCreated = false, 72 arrMarkers = [], 73 arrDirection = [], 74 mapElement = document.getElementById(settings.id), 75 arrBounds = new google.maps.LatLngBounds(); 76 77 if (settings.loaderImg != undefined) { 78 mapElement.innerHTML =('<div class="mapLoading"><img src="' + settings.loaderImg + '" /></div>'); 79 $('.mapLoading').css({ 80 'height': '100%', 81 'display': 'flex', 82 'align-items': 'center', 83 'justify-content': 'center' 84 }); 85 } 86 87 function mapInitialize() { 88 $this.addClass('maploaded') 89 blnCreated = true; 90 mapOptions = settings.mapOptions; 91 mapOptions['zoom'] = settings.zoom; 92 mapOptions['center'] = new google.maps.LatLng(settings.center[0], settings.center[1]); 93 94 // change defaults depending on settings 95 if (settings.zoom == 'bounds') delete mapOptions.zoom; 96 97 // SETUP MAP 98 map = new google.maps.Map(mapElement, mapOptions); 99 100 map.panBy(300,0); 101 102 103 // MARKERS 104 if (settings.defaultMarker != undefined && settings.arrMarkers == undefined) { 105 106 if (settings.zoom == 'bounds') arrBounds.extend(settings.defaultMarker.position); 107 settings.defaultMarker.position = validateLocation(settings.defaultMarker.position, settings.center); 108 109 if (settings.defaultMarker != '') { 110 var marker = new google.maps.Marker(settings.defaultMarker); 111 marker.setMap(map); 112 } 113 114 } else if (settings.defaultMarker != undefined && settings.arrMarkers != undefined) { 115 116 for (var intK = 0; intK < settings.arrMarkers.length; intK++) { 117 118 // check for field 119 var itemMarker = settings.arrMarkers[intK]; 120 itemMarker.position = validateLocation(itemMarker.position, settings.center); 121
122 if (itemMarker.icon == undefined) itemMarker['icon'] = settings.defaultMarker.icon; 123 if (itemMarker.id == undefined) itemMarker['id'] = intK; 124 if (itemMarker.animation == undefined) itemMarker['animation'] = google.maps.Animation.DROP; 125 126 var marker = new google.maps.Marker(itemMarker); 127 marker.setMap(map); 128 129 // SETUP DIRECTION ARRAY 130 if (itemMarker.direction != undefined) { 131 var strColour; 132 if (arrDirection[itemMarker.direction[0]] == undefined) { 133 arrDirection[itemMarker.direction[0]] = []; 134 strColour = itemMarker.direction[2]; 135 if (strColour == undefined) strColour = '#0088FF'; 136 arrDirection[itemMarker.direction[0]]['colour'] = strColour; 137 } 138 arrDirection[itemMarker.direction[0]][itemMarker.direction[1]] = itemMarker.position; 139 } 140 141 // RUN CLICK FUNCTION 142 var arrMarkerClick; 143 if (itemMarker.click !== undefined || settings.defaultMarker.click !== undefined) { 144 arrMarkerClick = { 145 'id': itemMarker['
145id'], 146 'title': itemMarker['title'], 147 'position': itemMarker['position'], 148 'icon': itemMarker['icon'], 149 'animation': itemMarker['animation'] 150 } 151 } 152 if (itemMarker.click !== undefined) { 153 marker.addListener('click', function() { 154 settings.arrMarkers[this.id].click(this.id, arrMarkerClick); 155 }); 156 } else if (settings.defaultMarker.click !== undefined) { 157 marker.addListener('click', function() { 158 settings.defaultMarker.click(this.id, arrMarkerClick); 159 }); 160 } 161 162 if (settings.zoom == 'bounds') arrBounds.extend(itemMarker.position); 163 } 164 165 } 166 var arrDirectionKeys = Object.keys(arrDirection); 167 if (arrDirectionKeys.length > 0) { 168 for (var intK = 0; intK < arrDirectionKeys.length; intK++) { 169 var strDirectionKey = arrDirectionKeys[intK], 170 arrDirectionCurrent = arrDirection[strDirectionKey], 171 strColour = arrDirectionCurrent['colour']; 172 delete arrDirectionCurrent['colour']; 173 createDirections(map, arrDirectionCurrent, strDirectionKey, strColour); 174 } 175 } 176 177 178 179 if (settings.styles != undefined) { 180 var styledMap = new google.maps.StyledMapType(settings.styles, {name: "Styled Map"}); 181 map.mapTypes.set('map_style', styledMap); 182 map.setMapTypeId('map_style'); 183 } 184 if (settings.zoom == 'bounds') map.fitBounds(arrBounds); 185
186 // SETUP CLICK EVENT IF TYPE OF MAP IS SELECT (RETURNS LOACTION OF LAT AND LONG) 187 if (settings.type == 'select') { 188 var strTargetType = typeof(settings.selectTarget); 189 190 if (strTargetType != undefined && strTargetType == 'string') { 191 var targetType = $(settings.selectTarget) 192 strTargetType = 'element'; 193 } else if (strTargetType != undefined && strTargetType == 'object') { 194 var targetType = []; 195 targetType[0] = $(settings.selectTarget[0]); 196 targetType[1] = $(settings.selectTarget[1]); 197 strTargetType = 'object'; 198 } 199 var MarkerSelect = new google.maps.Marker(); 200 MarkerSelect.setMap(map); 201 202 if (strTargetType == 'element' || strTargetType == 'object') { 203 if (strTargetType == 'element') { 204 var selectLoc = targetType.val().replace(/ /g, '').split(','); 205 } else if (strTargetType == 'object') { 206 var selectLoc = []; 207 selectLoc[0] = targetType[0].val(); 208 selectLoc[1] = targetType[1].val(); 209 }; 210 if (selectLoc[0] != '' && selectLoc[1] != '') { 211 map.setCenter(new google.maps.LatLng( selectLoc[0], selectLoc[1] ) ); 212 MarkerSelect.setPosition(new google.maps.LatLng( selectLoc[0], selectLoc[1] )); 213 } 214 } 215 216 google.maps.event.addListener(map, 'click', function(event) { 217 var lat = event.latLng.lat(); 218 var lng = event.latLng.lng(); 219 console.info('lat', lat); 220 console.info('lng', lng); 221 if (strTargetType == 'element') { 222 targetType.val(lat + ', ' + lng); 223 } else if (strTargetType == 'object') { 224 targetType[0].val(lat); 225 targetType[1].val(lng); 226 }; 227 228 MarkerSelect.setPosition(new google.maps.LatLng(lat, lng)); 229 }); 230 } 231 } 232 } 233 234 function validateLocation(arrPos, arrDefault) { 235 if (arrPos == undefined) { 236 returnMarker = new google.maps.LatLng(arrDefault[0], arrDefault[1]); 237 } else { 238 returnMarker = new google.maps.LatLng(arrPos[0], arrPos[1]); 239 } 240 return returnMarker; 241 } 242 243 function checkPlacement($this) { 244 var $window = $(window), 245 scrollTop = $window.scrollTop() + $window.height(), 246 thisTop = $this.offset().top; 247 if (scrollTop > thisTop) { 248 return true; 249 } 250 return false; 251 } 252 253 var directionsRoutes = [], 254 directionsBounds = new google.maps.LatLngBounds(); 255 function createDirections(map, arrLocation, strName, strColour) { 256 var directionsDisplay = new google.maps.DirectionsRenderer({ 257 suppressMarkers: true, 258 preserveViewport: true, 259 polylineOptions: { strokeColor: strColour } 260 }), 261 directionsService = new google.maps.DirectionsService; 262 var intArrLength = arrLocation.length, 263 intStart = arrLocation[0], 264 intEnd = arrLocation[intArrLength - 1], 265 arrWaypoint = [], 266 travel; 267 directionsBounds.extend(intStart); 268 if (intArrLength > 2) { 269 for (var intK = 1; intK < intArrLength - 1; intK++) { 270 arrWaypoint.push({ 271 location: arrLocation[intK], 272 stopover: true 273 }); 274 directionsBounds.extend(arrLocation[intK]); 275 } 276 } 277 directionsBounds.extend(intEnd); 278 directionsDisplay.setMap(map); 279 if (travel == undefined) travel = 'DRIVING'; 280 directionsService.route({ 281 origin: intStart, 282 waypoints: arrWaypoint, 283 destination: intEnd, 284 travelMode: google.maps.TravelMode[travel] 285 }, function(response, status) { 286 if (status == 'OK') { 287 directionsDisplay.setDirections(response); 288 } else { 289 console.log('Directions request failed due to ' + status); 290 } 291 }); 292 directionsRoutes[strName] = directionsDisplay; 293 } 294 295}( jQuery ));
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.