vendor/assets/javascripts/_map.js in active_frontend-12.2.0 vs vendor/assets/javascripts/_map.js in active_frontend-12.3.0

- old
+ new

@@ -11,23 +11,21 @@ } }(this, function() { -if (!(typeof window.google === 'object' && window.google.maps)) { - throw 'Google Maps API is required. Please register the following JavaScript library http://maps.google.com/maps/api/js?sensor=true.' -} - var extend_object = function(obj, new_obj) { var name; if (obj === new_obj) { return obj; } for (name in new_obj) { - obj[name] = new_obj[name]; + if (new_obj[name] !== undefined) { + obj[name] = new_obj[name]; + } } return obj; }; @@ -110,13 +108,11 @@ } return coords; }; - var getElementsByClassName = function (class_name, context) { - var element, _class = class_name.replace('.', ''); if ('jQuery' in this && context) { element = $("." + _class, context)[0]; @@ -158,10 +154,19 @@ "use strict"; var doc = document; var GMaps = function(options) { + + if (!(typeof window.google === 'object' && window.google.maps)) { + if (typeof window.console === 'object' && window.console.error) { + console.error('Google Maps API is required. Please register the following JavaScript library https://maps.googleapis.com/maps/api/js.'); + } + + return function() {}; + } + if (!this) return new GMaps(options); options.zoom = options.zoom || 15; options.mapType = options.mapType || 'roadmap'; @@ -212,17 +217,15 @@ streetViewControl: streetViewControl, overviewMapControl: overviewMapControl }; if (typeof(options.el) === 'string' || typeof(options.div) === 'string') { - - if (identifier.indexOf("#") > -1) { - this.el = getElementById(identifier, options.context); - } else { - this.el = getElementsByClassName.apply(this, [identifier, options.context]); - } - + if (identifier.indexOf("#") > -1) { + this.el = getElementById(identifier, options.context); + } else { + this.el = getElementsByClassName.apply(this, [identifier, options.context]); + } } else { this.el = identifier; } if (typeof(this.el) === 'undefined' || this.el === null) { @@ -1364,11 +1367,11 @@ self.routes.push(result.routes[r]); } } if (options.callback) { - options.callback(self.routes); + options.callback(self.routes, result, status); } } else { if (options.error) { options.error(result, status); @@ -1376,11 +1379,11 @@ } }); }; GMaps.prototype.removeRoutes = function() { - this.routes = []; + this.routes.length = 0; }; GMaps.prototype.getElevations = function(options) { options = extend_object({ locations: [], @@ -1424,24 +1427,56 @@ } }; GMaps.prototype.cleanRoute = GMaps.prototype.removePolylines; +GMaps.prototype.renderRoute = function(options, renderOptions) { + var self = this, + panel = ((typeof renderOptions.panel === 'string') ? document.getElementById(renderOptions.panel.replace('#', '')) : renderOptions.panel), + display; + + renderOptions.panel = panel; + renderOptions = extend_object({ + map: this.map + }, renderOptions); + display = new google.maps.DirectionsRenderer(renderOptions); + + this.getRoutes({ + origin: options.origin, + destination: options.destination, + travelMode: options.travelMode, + waypoints: options.waypoints, + unitSystem: options.unitSystem, + error: options.error, + avoidHighways: options.avoidHighways, + avoidTolls: options.avoidTolls, + optimizeWaypoints: options.optimizeWaypoints, + callback: function(routes, response, status) { + if (status === google.maps.DirectionsStatus.OK) { + display.setDirections(response); + } + } + }); +}; + GMaps.prototype.drawRoute = function(options) { var self = this; this.getRoutes({ origin: options.origin, destination: options.destination, travelMode: options.travelMode, waypoints: options.waypoints, unitSystem: options.unitSystem, error: options.error, - callback: function(e) { - if (e.length > 0) { + avoidHighways: options.avoidHighways, + avoidTolls: options.avoidTolls, + optimizeWaypoints: options.optimizeWaypoints, + callback: function(routes) { + if (routes.length > 0) { var polyline_options = { - path: e[e.length - 1].overview_path, + path: routes[routes.length - 1].overview_path, strokeColor: options.strokeColor, strokeOpacity: options.strokeOpacity, strokeWeight: options.strokeWeight }; @@ -1450,11 +1485,11 @@ } self.drawPolyline(polyline_options); if (options.callback) { - options.callback(e[e.length - 1]); + options.callback(routes[routes.length - 1]); } } } }); }; @@ -1850,11 +1885,11 @@ if (opacity) { opacity = parseFloat(opacity); opacity = Math.min(1, Math.max(opacity, 0)); if (opacity === 0) { - return '0x10121900'; + return '0x00000000'; } opacity = (opacity * 255).toString(16); if (opacity.length === 1) { opacity += opacity; } @@ -1996,10 +2031,14 @@ GMaps.prototype.off = function(event_name) { GMaps.off(event_name, this); }; +GMaps.prototype.once = function(event_name, handler) { + return GMaps.once(event_name, this, handler); +}; + GMaps.custom_events = ['marker_added', 'marker_removed', 'polyline_added', 'polyline_removed', 'polygon_added', 'polygon_removed', 'geolocated', 'geolocation_failed']; GMaps.on = function(event_name, object, handler) { if (GMaps.custom_events.indexOf(event_name) == -1) { if(object instanceof GMaps) object = object.map; @@ -2026,10 +2065,17 @@ else { object.registered_events[event_name] = []; } }; +GMaps.once = function(event_name, object, handler) { + if (GMaps.custom_events.indexOf(event_name) == -1) { + if(object instanceof GMaps) object = object.map; + return google.maps.event.addListenerOnce(object, event_name, handler); + } +}; + GMaps.fire = function(event_name, object, scope) { if (GMaps.custom_events.indexOf(event_name) == -1) { google.maps.event.trigger(object, event_name, Array.prototype.slice.apply(arguments).slice(2)); } else { @@ -2086,94 +2132,96 @@ this.geocoder.geocode(options, function(results, status) { callback(results, status); }); }; -//========================== -// Polygon containsLatLng -// https://github.com/tparkin/Google-Maps-Point-in-Polygon -// Poygon getBounds extension - google-maps-extensions -// http://code.google.com/p/google-maps-extensions/source/browse/google.maps.Polygon.getBounds.js -if (!google.maps.Polygon.prototype.getBounds) { - google.maps.Polygon.prototype.getBounds = function(latLng) { - var bounds = new google.maps.LatLngBounds(); - var paths = this.getPaths(); - var path; +if (typeof window.google === 'object' && window.google.maps) { + //========================== + // Polygon containsLatLng + // https://github.com/tparkin/Google-Maps-Point-in-Polygon + // Poygon getBounds extension - google-maps-extensions + // http://code.google.com/p/google-maps-extensions/source/browse/google.maps.Polygon.getBounds.js + if (!google.maps.Polygon.prototype.getBounds) { + google.maps.Polygon.prototype.getBounds = function(latLng) { + var bounds = new google.maps.LatLngBounds(); + var paths = this.getPaths(); + var path; - for (var p = 0; p < paths.getLength(); p++) { - path = paths.getAt(p); - for (var i = 0; i < path.getLength(); i++) { - bounds.extend(path.getAt(i)); + for (var p = 0; p < paths.getLength(); p++) { + path = paths.getAt(p); + for (var i = 0; i < path.getLength(); i++) { + bounds.extend(path.getAt(i)); + } } - } - return bounds; - }; -} + return bounds; + }; + } -if (!google.maps.Polygon.prototype.containsLatLng) { - // Polygon containsLatLng - method to determine if a latLng is within a polygon - google.maps.Polygon.prototype.containsLatLng = function(latLng) { - // Exclude points outside of bounds as there is no way they are in the poly - var bounds = this.getBounds(); + if (!google.maps.Polygon.prototype.containsLatLng) { + // Polygon containsLatLng - method to determine if a latLng is within a polygon + google.maps.Polygon.prototype.containsLatLng = function(latLng) { + // Exclude points outside of bounds as there is no way they are in the poly + var bounds = this.getBounds(); - if (bounds !== null && !bounds.contains(latLng)) { - return false; - } + if (bounds !== null && !bounds.contains(latLng)) { + return false; + } - // Raycast point in polygon method - var inPoly = false; + // Raycast point in polygon method + var inPoly = false; - var numPaths = this.getPaths().getLength(); - for (var p = 0; p < numPaths; p++) { - var path = this.getPaths().getAt(p); - var numPoints = path.getLength(); - var j = numPoints - 1; + var numPaths = this.getPaths().getLength(); + for (var p = 0; p < numPaths; p++) { + var path = this.getPaths().getAt(p); + var numPoints = path.getLength(); + var j = numPoints - 1; - for (var i = 0; i < numPoints; i++) { - var vertex1 = path.getAt(i); - var vertex2 = path.getAt(j); + for (var i = 0; i < numPoints; i++) { + var vertex1 = path.getAt(i); + var vertex2 = path.getAt(j); - if (vertex1.lng() < latLng.lng() && vertex2.lng() >= latLng.lng() || vertex2.lng() < latLng.lng() && vertex1.lng() >= latLng.lng()) { - if (vertex1.lat() + (latLng.lng() - vertex1.lng()) / (vertex2.lng() - vertex1.lng()) * (vertex2.lat() - vertex1.lat()) < latLng.lat()) { - inPoly = !inPoly; + if (vertex1.lng() < latLng.lng() && vertex2.lng() >= latLng.lng() || vertex2.lng() < latLng.lng() && vertex1.lng() >= latLng.lng()) { + if (vertex1.lat() + (latLng.lng() - vertex1.lng()) / (vertex2.lng() - vertex1.lng()) * (vertex2.lat() - vertex1.lat()) < latLng.lat()) { + inPoly = !inPoly; + } } + + j = i; } + } - j = i; + return inPoly; + }; + } + + if (!google.maps.Circle.prototype.containsLatLng) { + google.maps.Circle.prototype.containsLatLng = function(latLng) { + if (google.maps.geometry) { + return google.maps.geometry.spherical.computeDistanceBetween(this.getCenter(), latLng) <= this.getRadius(); } - } + else { + return true; + } + }; + } - return inPoly; + google.maps.LatLngBounds.prototype.containsLatLng = function(latLng) { + return this.contains(latLng); }; -} -if (!google.maps.Circle.prototype.containsLatLng) { - google.maps.Circle.prototype.containsLatLng = function(latLng) { - if (google.maps.geometry) { - return google.maps.geometry.spherical.computeDistanceBetween(this.getCenter(), latLng) <= this.getRadius(); - } - else { - return true; - } + google.maps.Marker.prototype.setFences = function(fences) { + this.fences = fences; }; -} -google.maps.LatLngBounds.prototype.containsLatLng = function(latLng) { - return this.contains(latLng); -}; + google.maps.Marker.prototype.addFence = function(fence) { + this.fences.push(fence); + }; -google.maps.Marker.prototype.setFences = function(fences) { - this.fences = fences; -}; - -google.maps.Marker.prototype.addFence = function(fence) { - this.fences.push(fence); -}; - -google.maps.Marker.prototype.getId = function() { - return this['__gm_id']; -}; + google.maps.Marker.prototype.getId = function() { + return this['__gm_id']; + }; +} //========================== // Array indexOf // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf if (!Array.prototype.indexOf) { \ No newline at end of file