var MeteoraMap = (function() { 'use strict'; var initCallbacks = [], metersPerMile = 0.0006213711922373339, checkForInit = function() { if(window.google && window.google.maps) { Map.Init(); return; } setTimeout(checkForInit, 150); }, checked = false; function Map(ele, cb) { this._init = false; this._onload = null; if (typeof cb === 'function') cb = cb.bind(this); initCallbacks.push(this.init.bind(this, ele, cb)); if(!checked) { // this is needed because google map's init changed to DrawMap's checkForInit(); checked = true; } } Map.prototype = { init: function init(ele, cb) { var styles = [{ 'featureType': 'administrative', 'elementType': 'labels.text.fill', 'stylers': [{ 'color': '#444444' }] }, { 'featureType': 'landscape', 'elementType': 'all', 'stylers': [{ 'color': '#f2f2f2' }] }, { 'featureType': 'poi', 'elementType': 'all', 'stylers': [{ 'visibility': 'off' }] }, { 'featureType': 'road', 'elementType': 'all', 'stylers': [{ 'saturation': -100 }, { 'lightness': 45 }] }, { 'featureType': 'road.highway', 'elementType': 'all', 'stylers': [{ 'visibility': 'simplified' }] }, { 'featureType': 'road.arterial', 'elementType': 'labels.icon', 'stylers': [{ 'visibility': 'off' }] }, { 'featureType': 'transit', 'elementType': 'all', 'stylers': [{ 'visibility': 'off' }] }, { 'featureType': 'water', 'elementType': 'all', 'stylers': [{ 'color': '#46bcec' }, { 'visibility': 'on' }] }], styledMap = new google.maps.StyledMapType(styles, { name: 'Meteora' }), minZoomLevel = 3, mapOptions = { minZoom: minZoomLevel, zoom: 6, center: new google.maps.LatLng(32.9774169, -96.722763), panControl: false, zoomControl: true, scaleControl: true, streetViewControl: false, mapTypeControlOptions: { mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style'] } }, map = new google.maps.Map(ele, mapOptions), strictBounds = new google.maps.LatLngBounds( new google.maps.LatLng(28.70, -127.50), new google.maps.LatLng(48.85, -55.90) ); map.mapTypes.set('map_style', styledMap); map.setMapTypeId('map_style'); if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { var loc = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); if (strictBounds.contains(loc)) { map.setCenter(loc); } }); } // Limit the zoom level var zoomInit = false; google.maps.event.addListener(map, 'zoom_changed', function() { if (!zoomInit) { zoomInit = true; map.setZoom(6); map.setCenter(new google.maps.LatLng(32.9774169, -96.722763)); } if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel); }); google.maps.event.trigger(map, 'resize'); map.fitBounds(strictBounds); this.m = map; this.markers = []; this._init = true; var self = this; setTimeout(function() { if (!!self._onload) { zoomInit = true; self._onload.call(self); self._onload = null; } if (typeof cb === 'function') setTimeout(function() { cb(self); }, 100); }, 100); }, focusPoint: function focusPoint(title, pt, radiusInMiles) { if (!this._init) { this._onload = function() { this.focusPoint(title, pt, radiusInMiles); }; return; } this.removeAll(); pt.pos = new google.maps.LatLng(pt.lat, pt.lon || pt.lng); var marker = new google.maps.Marker({ position: pt.pos, map: this.m, title: title }); this.markers.push(marker); if (!!radius) { var circle = new google.maps.Circle({ map: this.m, radius: radiusInMiles / metersPerMile, // metres fillColor: '#AA0000' }); circle.bindTo('center', marker, 'position'); this.markers.push(circle); } if (radiusInMiles >= 10) this.m.setZoom(10); else if (radiusInMiles >= 5) this.m.setZoom(11); else if (radiusInMiles >= 1) this.m.setZoom(12); else this.m.setZoom(14); this.m.setCenter(marker.getPosition()); }, setPoints: function setPoints(points) { if (!this._init) { this._onload = function() { this.setPoints(points); }; return; } this.removeAll(); for (var zip in points) { var pt = points[zip]; pt.pos = new google.maps.LatLng(pt.lat, pt.lon || pt.lng); this.addCircle(pt); } }, removeAll: function removeAll() { if (!isArray(this.markers)) return; for (var i = 0, m; i < this.markers.length; i++) { m = this.markers[i]; if (!!m) m.setMap(null); } this.markers = []; }, remove: function removePoints(pts) { if (!isArray(this.markers)) return; for (var i = 0, m; i < this.markers.length; i++) { m = this.markers[i]; if (!m) continue; for (var x = 0, pt; !!(pt = pts[x]); x++) { var mpt = m.center; if (mpt.lat() == pt.lat && mpt.lng() == pt.lng) { m.setMap(null); this.markers[i] = null; } } } }, addCircle: function addCircle(pt) { var color = !!pt.color ? pt.color : '#52D834', modifier = 1609.34, circle = new google.maps.Circle({ 'strokeColor': color, 'strokeOpacity': 1, 'strokeWeight': 1, 'fillColor': color, 'fillOpacity': 0.5, 'map': this.m, 'center': pt.pos, 'radius': !!pt.radius ? (pt.radius < modifier ? pt.radius * modifier : pt.radius) : 48600 }); if (!isArray(this.markers)) this.markers = []; this.markers.push(circle); }, addStar: function addStar(loc) { var marker = new google.maps.Marker({ map: this.m, position: loc, icon: { // Star path: 'M 0,-24 6,-7 24,-7 10,4 15,21 0,11 -15,21 -10,4 -24,-7 -6,-7 z', fillColor: '#ffff00', fillOpacity: 1, scale: 1 / 4, strokeColor: '#bd8d2c', strokeWeight: 1 } }); this.markers.push(marker); } }; Map.Init = function MapInit() { while (initCallbacks.length > 0) { var cb = initCallbacks.shift(); cb(); } }; Map.Geocoder = function Geocoder(cb) { this.cache = []; initCallbacks.push(this.init.bind(this, cb)); }; Map.Geocoder.prototype = { init: function() { var cache = this.cache, self = this; delete this.cache; this.g = new google.maps.Geocoder(); cache.forEach(function(q) { self.search(q.addr, q.cb); }); }, search: function(addr, cb) { if (this.cache != null) return this.cache.push({ addr: addr, cb: cb }); this.g.geocode({ address: addr }, function(results, status) { if (status !== google.maps.GeocoderStatus.OK) { cb(null, status); return console.error('geocoder error:', status); } var loc = results[0].geometry.location; cb({ lat: loc.lat(), lng: loc.lng() }); }); } }; return Map; })(); var WeatherMap = (function() { 'use strict'; function WeatherMap(ele, cb) { if (!(this instanceof WeatherMap)) { return new WeatherMap(ele, cb); } MeteoraMap.call(this, ele, cb); } WeatherMap.prototype = Object.create(MeteoraMap.prototype); WeatherMap.prototype.update = function update(wc, tf) { if (!tf) tf = '0'; var self = this; new HttpRequest('GET', '/api/v1/weatherMap/' + wc + '/' + tf, null, null, 'json', function(points) { self.setPoints(points); }); }; return WeatherMap; })();