app/assets/javascripts/gmapsjs/gmapsjs.js in gmapsjs-0.4.16 vs app/assets/javascripts/gmapsjs/gmapsjs.js in gmapsjs-0.4.21

- old
+ new

@@ -1,29 +1,28 @@ +"use strict"; (function(root, factory) { if(typeof exports === 'object') { module.exports = factory(); } else if(typeof define === 'function' && define.amd) { - define('GMaps', [], factory); + define(['jquery', 'googlemaps!'], factory); } + else { + root.GMaps = factory(); + } - root.GMaps = factory(); }(this, function() { /*! - * GMaps.js v0.4.16 + * GMaps.js v0.4.21 * http://hpneo.github.com/gmaps/ * - * Copyright 2014, Gustavo Leon + * Copyright 2015, Gustavo Leon * Released under the MIT License. */ -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; @@ -58,11 +57,11 @@ array_length = array.length, i; if (Array.prototype.map && array.map === Array.prototype.map) { array_return = Array.prototype.map.call(array, function(item) { - callback_params = original_callback_params; + var callback_params = original_callback_params.slice(0); callback_params.splice(0, 0, item); return callback.apply(this, callback_params); }); } @@ -103,11 +102,11 @@ var arrayToLatLng = function(coords, useGeoJSON) { var i; for (i = 0; i < coords.length; i++) { if (!(coords[i] instanceof google.maps.LatLng)) { - if (coords[i].length > 0 && typeof(coords[i][0]) == "object") { + if (coords[i].length > 0 && typeof(coords[i][0]) === "object") { coords[i] = arrayToLatLng(coords[i], useGeoJSON); } else { coords[i] = coordsToLatLngs(coords[i], useGeoJSON); } @@ -115,16 +114,30 @@ } return coords; }; +var getElementsByClassName = function (class_name, context) { + + var element, + _class = class_name.replace('.', ''); + + if ('jQuery' in this && context) { + element = $("." + _class, context)[0]; + } else { + element = document.getElementsByClassName(_class)[0]; + } + return element; + +}; + var getElementById = function(id, context) { var element, id = id.replace('#', ''); - if ('jQuery' in this && context) { - element = $("#" + id, context)[0]; + if ('jQuery' in window && context) { + element = $('#' + id, context)[0]; } else { element = document.getElementById(id); }; return element; @@ -145,39 +158,55 @@ }; var GMaps = (function(global) { "use strict"; + 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() {}; + } + var doc = document; var GMaps = function(options) { if (!this) return new GMaps(options); options.zoom = options.zoom || 15; options.mapType = options.mapType || 'roadmap'; + var valueOrDefault = function(value, defaultValue) { + return value === undefined ? defaultValue : value; + }; + var self = this, i, - events_that_hide_context_menu = ['bounds_changed', 'center_changed', 'click', 'dblclick', 'drag', 'dragend', 'dragstart', 'idle', 'maptypeid_changed', 'projection_changed', 'resize', 'tilesloaded', 'zoom_changed'], + events_that_hide_context_menu = [ + 'bounds_changed', 'center_changed', 'click', 'dblclick', 'drag', + 'dragend', 'dragstart', 'idle', 'maptypeid_changed', 'projection_changed', + 'resize', 'tilesloaded', 'zoom_changed' + ], events_that_doesnt_hide_context_menu = ['mousemove', 'mouseout', 'mouseover'], options_to_be_deleted = ['el', 'lat', 'lng', 'mapType', 'width', 'height', 'markerClusterer', 'enableNewStyle'], - container_id = options.el || options.div, + identifier = options.el || options.div, markerClustererFunction = options.markerClusterer, mapType = google.maps.MapTypeId[options.mapType.toUpperCase()], map_center = new google.maps.LatLng(options.lat, options.lng), - zoomControl = options.zoomControl || true, + zoomControl = valueOrDefault(options.zoomControl, true), zoomControlOpt = options.zoomControlOpt || { style: 'DEFAULT', position: 'TOP_LEFT' }, zoomControlStyle = zoomControlOpt.style || 'DEFAULT', zoomControlPosition = zoomControlOpt.position || 'TOP_LEFT', - panControl = options.panControl || true, - mapTypeControl = options.mapTypeControl || true, - scaleControl = options.scaleControl || true, - streetViewControl = options.streetViewControl || true, - overviewMapControl = overviewMapControl || true, + panControl = valueOrDefault(options.panControl, true), + mapTypeControl = valueOrDefault(options.mapTypeControl, true), + scaleControl = valueOrDefault(options.scaleControl, true), + streetViewControl = valueOrDefault(options.streetViewControl, true), + overviewMapControl = valueOrDefault(overviewMapControl, true), map_options = {}, map_base_options = { zoom: this.zoom, center: map_center, mapTypeId: mapType @@ -193,16 +222,22 @@ scaleControl: scaleControl, streetViewControl: streetViewControl, overviewMapControl: overviewMapControl }; - if (typeof(options.el) === 'string' || typeof(options.div) === 'string') { - this.el = getElementById(container_id, options.context); - } else { - this.el = container_id; - } + 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]); + } + + } else { + this.el = identifier; + } + if (typeof(this.el) === 'undefined' || this.el === null) { throw 'No element defined.'; } window.context_menu = window.context_menu || {}; @@ -263,11 +298,11 @@ } if (!getElementById('gmaps_context_menu')) return; var context_menu_element = getElementById('gmaps_context_menu'); - + context_menu_element.innerHTML = html; var context_menu_items = context_menu_element.getElementsByTagName('a'), context_menu_items_count = context_menu_items.length, i; @@ -291,32 +326,38 @@ top = position[1] + e.pixel.y- 15; context_menu_element.style.left = left + "px"; context_menu_element.style.top = top + "px"; - context_menu_element.style.display = 'block'; + // context_menu_element.style.display = 'block'; }; this.buildContextMenu = function(control, e) { if (control === 'marker') { e.pixel = {}; var overlay = new google.maps.OverlayView(); overlay.setMap(self.map); - + overlay.draw = function() { var projection = overlay.getProjection(), position = e.marker.getPosition(); - + e.pixel = projection.fromLatLngToContainerPixel(position); buildContextMenuHTML(control, e); }; } else { buildContextMenuHTML(control, e); } + + var context_menu_element = getElementById('gmaps_context_menu'); + + setTimeout(function() { + context_menu_element.style.display = 'block'; + }, 0); }; this.setContextMenu = function(options) { window.context_menu[self.el.id][options.control] = {}; @@ -341,13 +382,15 @@ ul.style.background = 'white'; ul.style.listStyle = 'none'; ul.style.padding = '8px'; ul.style.boxShadow = '2px 2px 6px #ccc'; - doc.body.appendChild(ul); + if (!getElementById('gmaps_context_menu')) { + doc.body.appendChild(ul); + } - var context_menu_element = getElementById('gmaps_context_menu') + var context_menu_element = getElementById('gmaps_context_menu'); google.maps.event.addDomListener(context_menu_element, 'mouseout', function(ev) { if (!ev.relatedTarget || !this.contains(ev.relatedTarget)) { window.setTimeout(function(){ context_menu_element.style.display = 'none'; @@ -422,14 +465,15 @@ this.fitLatLngBounds(latLngs); }; this.fitLatLngBounds = function(latLngs) { - var total = latLngs.length; - var bounds = new google.maps.LatLngBounds(); + var total = latLngs.length, + bounds = new google.maps.LatLngBounds(), + i; - for(var i=0; i < total; i++) { + for(i = 0; i < total; i++) { bounds.extend(latLngs[i]); } this.map.fitBounds(bounds); }; @@ -467,11 +511,11 @@ if (typeof(this.map[method]) == 'function' && !this[method]) { native_methods.push(method); } } - for (i=0; i < native_methods.length; i++) { + for (i = 0; i < native_methods.length; i++) { (function(gmaps, scope, method_name) { gmaps[method_name] = function(){ return scope[method_name].apply(scope, arguments); }; })(this, this.map, native_methods[i]); @@ -483,11 +527,11 @@ GMaps.prototype.createControl = function(options) { var control = document.createElement('div'); control.style.cursor = 'pointer'; - + if (options.disableDefaultStyles !== true) { control.style.fontFamily = 'Roboto, Arial, sans-serif'; control.style.fontSize = '11px'; control.style.boxShadow = 'rgba(0, 0, 0, 0.298039) 0px 1px 4px -1px'; } @@ -498,10 +542,14 @@ if (options.id) { control.id = options.id; } + if (options.title) { + control.title = options.title; + } + if (options.classes) { control.className = options.classes; } if (options.content) { @@ -530,31 +578,35 @@ return control; }; GMaps.prototype.addControl = function(options) { var control = this.createControl(options); + this.controls.push(control); this.map.controls[control.position].push(control); return control; }; GMaps.prototype.removeControl = function(control) { - var position = null; + var position = null, + i; - for (var i = 0; i < this.controls.length; i++) { + for (i = 0; i < this.controls.length; i++) { if (this.controls[i] == control) { position = this.controls[i].position; this.controls.splice(i, 1); } } if (position) { for (i = 0; i < this.map.controls.length; i++) { - var controlsForPosition = this.map.controls[control.position] + var controlsForPosition = this.map.controls[control.position]; + if (controlsForPosition.getAt(i) == control) { controlsForPosition.removeAt(i); + break; } } } @@ -620,11 +672,11 @@ if (options[name]) { google.maps.event.addListener(object, name, function(me){ if(!me.pixel){ me.pixel = map.getProjection().fromLatLngToPoint(me.latLng) } - + options[name].apply(this, [me]); }); } })(this.map, marker, marker_events_with_mouse[ev]); } @@ -740,11 +792,11 @@ this.markerClusterer.removeMarker(marker); } GMaps.fire('marker_removed', marker, this); } - + this.markers = new_markers; } else { for (var i = 0; i < collection.length; i++) { var index = this.markers.indexOf(collection[i]); @@ -794,11 +846,11 @@ overlay.el = el; if (!options.layer) { options.layer = 'overlayLayer'; } - + var panes = this.getPanes(), overlayLayer = panes[options.layer], stop_overlay_events = ['contextmenu', 'DOMMouseScroll', 'dblclick', 'mousedown']; overlayLayer.appendChild(el); @@ -914,11 +966,11 @@ if (points.length) { if (points[0][0] === undefined) { path = points; } else { - for (var i=0, latlng; latlng=points[i]; i++) { + for (var i = 0, latlng; latlng = points[i]; i++) { path.push(new google.maps.LatLng(latlng[0], latlng[1])); } } } @@ -1395,17 +1447,23 @@ waypoints: options.waypoints, unitSystem: options.unitSystem, error: options.error, callback: function(e) { if (e.length > 0) { - self.drawPolyline({ + var polyline_options = { path: e[e.length - 1].overview_path, strokeColor: options.strokeColor, strokeOpacity: options.strokeOpacity, strokeWeight: options.strokeWeight - }); - + }; + + if (options.hasOwnProperty("icons")) { + polyline_options.icons = options.icons; + } + + self.drawPolyline(polyline_options); + if (options.callback) { options.callback(e[e.length - 1]); } } } @@ -1430,11 +1488,11 @@ //step callback if (e.length > 0 && options.step) { var route = e[e.length - 1]; if (route.legs.length > 0) { var steps = route.legs[0].steps; - for (var i=0, step; step=steps[i]; i++) { + for (var i = 0, step; step = steps[i]; i++) { step.step_number = i; options.step(step, (route.legs[0].steps.length - 1)); } } } @@ -1447,21 +1505,21 @@ }); } else if (options.route) { if (options.route.legs.length > 0) { var steps = options.route.legs[0].steps; - for (var i=0, step; step=steps[i]; i++) { + for (var i = 0, step; step = steps[i]; i++) { step.step_number = i; options.step(step); } } } }; GMaps.prototype.drawSteppedRoute = function(options) { var self = this; - + if (options.origin && options.destination) { this.getRoutes({ origin: options.origin, destination: options.destination, travelMode: options.travelMode, @@ -1476,18 +1534,24 @@ //step callback if (e.length > 0 && options.step) { var route = e[e.length - 1]; if (route.legs.length > 0) { var steps = route.legs[0].steps; - for (var i=0, step; step=steps[i]; i++) { + for (var i = 0, step; step = steps[i]; i++) { step.step_number = i; - self.drawPolyline({ + var polyline_options = { path: step.path, strokeColor: options.strokeColor, strokeOpacity: options.strokeOpacity, strokeWeight: options.strokeWeight - }); + }; + + if (options.hasOwnProperty("icons")) { + polyline_options.icons = options.icons; + } + + self.drawPolyline(polyline_options); options.step(step, (route.legs[0].steps.length - 1)); } } } @@ -1499,18 +1563,24 @@ }); } else if (options.route) { if (options.route.legs.length > 0) { var steps = options.route.legs[0].steps; - for (var i=0, step; step=steps[i]; i++) { + for (var i = 0, step; step = steps[i]; i++) { step.step_number = i; - self.drawPolyline({ + var polyline_options = { path: step.path, strokeColor: options.strokeColor, strokeOpacity: options.strokeOpacity, strokeWeight: options.strokeWeight - }); + }; + + if (options.hasOwnProperty("icons")) { + polyline_options.icons = options.icons; + } + + self.drawPolyline(polyline_options); options.step(step); } } } }; @@ -1524,16 +1594,22 @@ this.route = options.route; this.step_count = 0; this.steps = this.route.legs[0].steps; this.steps_length = this.steps.length; - this.polyline = this.map.drawPolyline({ + var polyline_options = { path: new google.maps.MVCArray(), strokeColor: options.strokeColor, strokeOpacity: options.strokeOpacity, strokeWeight: options.strokeWeight - }).getPath(); + }; + + if (options.hasOwnProperty("icons")) { + polyline_options.icons = options.icons; + } + + this.polyline = this.map.drawPolyline(polyline_options).getPath(); }; GMaps.Route.prototype.getRoute = function(options) { var self = this; @@ -1602,22 +1678,22 @@ static_map_options['lat'] = this.getCenter().lat(); static_map_options['lng'] = this.getCenter().lng(); if (this.markers.length > 0) { static_map_options['markers'] = []; - + for (var i = 0; i < this.markers.length; i++) { static_map_options['markers'].push({ lat: this.markers[i].getPosition().lat(), lng: this.markers[i].getPosition().lng() }); } } if (this.polylines.length > 0) { var polyline = this.polylines[0]; - + static_map_options['polyline'] = {}; static_map_options['polyline']['path'] = google.maps.geometry.encoding.encodePath(polyline.getPath()); static_map_options['polyline']['strokeColor'] = polyline.strokeColor static_map_options['polyline']['strokeOpacity'] = polyline.strokeOpacity static_map_options['polyline']['strokeWeight'] = polyline.strokeWeight @@ -1627,21 +1703,21 @@ }; GMaps.staticMapURL = function(options){ var parameters = [], data, - static_root = 'http://maps.googleapis.com/maps/api/staticmap'; + static_root = (location.protocol === 'file:' ? 'http:' : location.protocol ) + '//maps.googleapis.com/maps/api/staticmap'; if (options.url) { static_root = options.url; delete options.url; } static_root += '?'; var markers = options.markers; - + delete options.markers; if (!markers && options.marker) { markers = [options.marker]; delete options.marker; @@ -1701,11 +1777,11 @@ /** Markers **/ if (markers) { var marker, loc; - for (var i=0; data=markers[i]; i++) { + for (var i = 0; data = markers[i]; i++) { marker = []; if (data.size && data.size !== 'normal') { marker.push('size:' + data.size); delete data.size; @@ -1935,11 +2011,11 @@ 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; + if(object instanceof GMaps) object = object.map; return google.maps.event.addListener(object, event_name, handler); } else { var registered_event = { handler : handler, @@ -1953,11 +2029,11 @@ } }; GMaps.off = function(event_name, object) { if (GMaps.custom_events.indexOf(event_name) == -1) { - if(object instanceof GMaps) object = object.map; + if(object instanceof GMaps) object = object.map; google.maps.event.clearListeners(object, event_name); } else { object.registered_events[event_name] = []; } @@ -2015,102 +2091,104 @@ } delete options.lat; delete options.lng; delete options.callback; - + 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.getId = function() { + return this['__gm_id']; + }; +} -google.maps.Marker.prototype.addFence = function(fence) { - this.fences.push(fence); -}; - -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) { Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) { @@ -2142,8 +2220,8 @@ } } return -1; } } - + return GMaps; -})); \ No newline at end of file +}));