From 2b644e0b7456df436447b7d64def91b2203af9e2 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Wed, 9 Oct 2013 05:14:30 -0700 Subject: [PATCH] Adding gmaps --- Gemfile.lock | 2 + app/assets/stylesheets/spaces.css | 4 + app/assets/stylesheets/spaces.css.scss | 3 - app/controllers/spaces_controller.rb | 31 +- app/models/space.rb | 38 ++ app/views/spaces/_form.html.erb | 10 +- app/views/spaces/_marker.html.erb | 20 + app/views/spaces/_space.html.erb | 7 +- app/views/spaces/index.html.erb | 51 +- app/views/spaces/index.json.jbuilder | 2 +- app/views/spaces/show.html.erb | 5 +- app/views/spaces/show.json.jbuilder | 2 +- ...31009103919_add_lat_long_gmap_to_spaces.rb | 7 + ...20131009120246_add_city_state_to_spaces.rb | 6 + db/schema.rb | 7 +- .../gmaps4rails/gmaps4rails.base.js | 464 ++++++++++++++++++ .../gmaps4rails/gmaps4rails.bing.js | 226 +++++++++ .../gmaps4rails/gmaps4rails.googlemaps.js | 430 ++++++++++++++++ .../gmaps4rails/gmaps4rails.mapquest.js | 178 +++++++ .../gmaps4rails/gmaps4rails.openlayers.js | 265 ++++++++++ public/stylesheets/gmaps4rails.css | 24 + test/controllers/spaces_controller_test.rb | 4 +- test/fixtures/spaces.yml | 8 +- 23 files changed, 1758 insertions(+), 36 deletions(-) create mode 100644 app/assets/stylesheets/spaces.css delete mode 100644 app/assets/stylesheets/spaces.css.scss create mode 100644 app/views/spaces/_marker.html.erb create mode 100644 db/migrate/20131009103919_add_lat_long_gmap_to_spaces.rb create mode 100644 db/migrate/20131009120246_add_city_state_to_spaces.rb create mode 100644 public/javascripts/gmaps4rails/gmaps4rails.base.js create mode 100644 public/javascripts/gmaps4rails/gmaps4rails.bing.js create mode 100644 public/javascripts/gmaps4rails/gmaps4rails.googlemaps.js create mode 100644 public/javascripts/gmaps4rails/gmaps4rails.mapquest.js create mode 100644 public/javascripts/gmaps4rails/gmaps4rails.openlayers.js create mode 100644 public/stylesheets/gmaps4rails.css diff --git a/Gemfile.lock b/Gemfile.lock index 278b695..bd23ce6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,6 +37,7 @@ GEM coffee-script-source (1.6.3) erubis (2.7.0) execjs (2.0.2) + gmaps4rails (1.5.6) hike (1.2.3) i18n (0.6.5) jbuilder (1.5.2) @@ -111,6 +112,7 @@ PLATFORMS DEPENDENCIES coffee-rails (~> 4.0.0) + gmaps4rails jbuilder (~> 1.2) jquery-rails paper_trail (>= 3.0.0.beta1) diff --git a/app/assets/stylesheets/spaces.css b/app/assets/stylesheets/spaces.css new file mode 100644 index 0000000..0686b60 --- /dev/null +++ b/app/assets/stylesheets/spaces.css @@ -0,0 +1,4 @@ +.marker-container h3 { + display: inline-block; + margin-bottom: 0; +} \ No newline at end of file diff --git a/app/assets/stylesheets/spaces.css.scss b/app/assets/stylesheets/spaces.css.scss deleted file mode 100644 index 81eeb85..0000000 --- a/app/assets/stylesheets/spaces.css.scss +++ /dev/null @@ -1,3 +0,0 @@ -// Place all the styles related to the Spaces controller here. -// They will automatically be included in application.css. -// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/spaces_controller.rb b/app/controllers/spaces_controller.rb index 13dcc95..5559741 100644 --- a/app/controllers/spaces_controller.rb +++ b/app/controllers/spaces_controller.rb @@ -5,6 +5,35 @@ class SpacesController < ApplicationController # GET /spaces.json def index @spaces = Space.all + + # Index, downcase, and capitalize the categories + dot_index = 0 + @spaces.group_by{|s| s.category.downcase}.each do |group| + dot_color = Space.dot_colors[dot_index] + group.last.each do |space| + space.category = group.first.capitalize + space.dot_color = dot_color + end + dot_index += 1 + end + + # Don't bother with generating markers for JSON + respond_to do |format| + format.html { + + @json = @spaces.to_gmaps4rails do |space, marker| + marker.infowindow render_to_string(:partial => "/spaces/marker", :locals => { :space => space}) + marker.picture({ + :picture => "http://maps.google.com/intl/en_us/mapfiles/ms/micons/#{space.dot_color}-dot.png", + :width => 32, + :height => 32 + }) + marker.title space.name + marker.json({ :id => space.id }) + end + } + format.json #render json + end end # GET /spaces/1 @@ -69,6 +98,6 @@ class SpacesController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def space_params - params.require(:space).permit(:name, :type, :address, :hours, :phone, :email, :website, :description) + params.require(:space).permit(:name, :category, :address, :city, :state, :hours, :phone, :email, :website, :description) end end diff --git a/app/models/space.rb b/app/models/space.rb index 5a93e3d..efbfbba 100644 --- a/app/models/space.rb +++ b/app/models/space.rb @@ -1,3 +1,41 @@ class Space < ActiveRecord::Base has_paper_trail + acts_as_gmappable + + before_update :update_lat_lng + + attr_accessor :dot_color + + def self.categories + # Downcased and capitalized for great justice + Space.all.select(:category).map{|s| s.category.downcase.capitalize }.uniq + end + + def self.dot_colors + ["red","blue","green","yellow","purple","orange"] + end + + def website_with_protocol + /^http/.match(self.website) ? self.website : "http://#{self.website}" + end + + def full_address + "#{self.address}, #{self.city} #{self.state}" + end + + def gmaps4rails_address + #describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki + self.full_address + end + + def update_lat_lng + coords = self.geocode.first + Rails.logger.info coords.inspect + self.latitude = coords[:lat] + self.longitude = coords[:lng] + end + + def geocode + Gmaps4rails.geocode(self.full_address) + end end diff --git a/app/views/spaces/_form.html.erb b/app/views/spaces/_form.html.erb index 83277a2..c516cea 100644 --- a/app/views/spaces/_form.html.erb +++ b/app/views/spaces/_form.html.erb @@ -17,12 +17,20 @@
<%= f.label :category %>
- <%= f.text_field :category %> + <%= f.text_field :category %> (suggested: <%= Space.categories.join(", ") %>)
<%= f.label :address %>
<%= f.text_field :address %>
+
+ <%= f.label :city %>
+ <%= f.text_field :city %> +
+
+ <%= f.label :state %>
+ <%= f.text_field :state %> +
<%= f.label :hours %>
<%= f.text_field :hours %> diff --git a/app/views/spaces/_marker.html.erb b/app/views/spaces/_marker.html.erb new file mode 100644 index 0000000..3354aa5 --- /dev/null +++ b/app/views/spaces/_marker.html.erb @@ -0,0 +1,20 @@ +
+

<%= space.name %>

+ <%= space.category %> + +

+ <%= space.full_address %>
+ + Hours: <%= space.hours %>
+ + Phone: <%= space.phone %>
+ + Email: <%= space.email %>
+ + Website: <%= link_to space.website, space.website_with_protocol %>
+

+ +

+ <%= space.description %> +

+
\ No newline at end of file diff --git a/app/views/spaces/_space.html.erb b/app/views/spaces/_space.html.erb index aa0b2b3..a817a39 100644 --- a/app/views/spaces/_space.html.erb +++ b/app/views/spaces/_space.html.erb @@ -11,7 +11,12 @@

Address: - <%= space.address %> + <%= space.full_address %> +

+ +

+ Latitude/Longitude: + <%= space.latitude %>, <%= space.longitude %>

diff --git a/app/views/spaces/index.html.erb b/app/views/spaces/index.html.erb index fb342d7..0ebc935 100644 --- a/app/views/spaces/index.html.erb +++ b/app/views/spaces/index.html.erb @@ -1,10 +1,21 @@ -

Listing spaces

+<%= stylesheet_link_tag 'gmaps4rails' %> + +

Collaborative Spaces in Arizona

+

+ <%= link_to 'Add Space', new_space_path %> | + <%= link_to 'JSON Feed', spaces_path("json") %> +

+ +<%= gmaps4rails(@json) %> + +<%= yield :scripts %> + +
- @@ -18,24 +29,24 @@ - <% @spaces.each do |space| %> - - - - - - - - - - - - - + <% @spaces.group_by{|s| s.category}.each do |category, spaces| %> + + <% spaces.each do |space| %> + + + + + + + + + + + + + <% end %> <% end %>
NameCategory Address Hours Phone
<%= space.name %><%= space.category %><%= space.address %><%= space.hours %><%= space.phone %><%= space.email %><%= space.website %><%= space.description %><%= link_to 'Show', space %><%= link_to 'Edit', edit_space_path(space) %><%= link_to 'Destroy', space, method: :delete, data: { confirm: 'Are you sure?' } %>
+ + <%= category.titleize.pluralize %>
<%= space.name %><%= space.full_address %><%= space.hours %><%= space.phone %><%= space.email %><%= space.website %><%= space.description %><%= link_to 'Show', space %><%= link_to 'Edit', edit_space_path(space) %><%= link_to 'Destroy', space, method: :delete, data: { confirm: 'Are you sure?' } %>
- -
- -<%= link_to 'New Space', new_space_path %> diff --git a/app/views/spaces/index.json.jbuilder b/app/views/spaces/index.json.jbuilder index 54a21a1..e2cfe8f 100644 --- a/app/views/spaces/index.json.jbuilder +++ b/app/views/spaces/index.json.jbuilder @@ -1,4 +1,4 @@ json.array!(@spaces) do |space| - json.extract! space, :name, :type, :address, :hours, :phone, :email, :website, :description + json.extract! space, :name, :category, :address, :city, :state, :hours, :phone, :email, :website, :description, :dot_color json.url space_url(space, format: :json) end diff --git a/app/views/spaces/show.html.erb b/app/views/spaces/show.html.erb index ff60eb0..61e4d77 100644 --- a/app/views/spaces/show.html.erb +++ b/app/views/spaces/show.html.erb @@ -2,12 +2,11 @@ <%= render partial: "space", locals: {space: @space} %> -<%= link_to 'Edit', edit_space_path(@space) %> | -<%= link_to 'Back', spaces_path %> +<%= link_to 'Edit', edit_space_path(@space) %>

Revision history

-<% @space.versions.each do |v| %> +<% @space.versions.reverse!.each do |v| %>
<%= v.event.capitalize %> <%= v.created_at %> <%= v.whodunnit if v.whodunnit %>
<% if v.reify %> diff --git a/app/views/spaces/show.json.jbuilder b/app/views/spaces/show.json.jbuilder index e3eb9dc..deaee87 100644 --- a/app/views/spaces/show.json.jbuilder +++ b/app/views/spaces/show.json.jbuilder @@ -1 +1 @@ -json.extract! @space, :name, :type, :address, :hours, :phone, :email, :website, :description, :created_at, :updated_at +json.extract! @space, :name, :category, :address, :city, :state, :hours, :phone, :email, :website, :description, :created_at, :updated_at diff --git a/db/migrate/20131009103919_add_lat_long_gmap_to_spaces.rb b/db/migrate/20131009103919_add_lat_long_gmap_to_spaces.rb new file mode 100644 index 0000000..467f787 --- /dev/null +++ b/db/migrate/20131009103919_add_lat_long_gmap_to_spaces.rb @@ -0,0 +1,7 @@ +class AddLatLongGmapToSpaces < ActiveRecord::Migration + def change + add_column :spaces, :latitude, :float + add_column :spaces, :longitude, :float + add_column :spaces, :gmaps, :boolean + end +end diff --git a/db/migrate/20131009120246_add_city_state_to_spaces.rb b/db/migrate/20131009120246_add_city_state_to_spaces.rb new file mode 100644 index 0000000..fa33964 --- /dev/null +++ b/db/migrate/20131009120246_add_city_state_to_spaces.rb @@ -0,0 +1,6 @@ +class AddCityStateToSpaces < ActiveRecord::Migration + def change + add_column :spaces, :city, :string + add_column :spaces, :state, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index f50e863..808b7fd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20131009051102) do +ActiveRecord::Schema.define(version: 20131009120246) do create_table "spaces", force: true do |t| t.string "name" @@ -24,6 +24,11 @@ ActiveRecord::Schema.define(version: 20131009051102) do t.text "description" t.datetime "created_at" t.datetime "updated_at" + t.float "latitude" + t.float "longitude" + t.boolean "gmaps" + t.string "city" + t.string "state" end create_table "versions", force: true do |t| diff --git a/public/javascripts/gmaps4rails/gmaps4rails.base.js b/public/javascripts/gmaps4rails/gmaps4rails.base.js new file mode 100644 index 0000000..a1e17a4 --- /dev/null +++ b/public/javascripts/gmaps4rails/gmaps4rails.base.js @@ -0,0 +1,464 @@ +(function() { + var Gmaps; + + Gmaps = {}; + + Gmaps.triggerOldOnload = function() { + if (typeof Gmaps.oldOnload === 'function') return Gmaps.oldOnload(); + }; + + Gmaps.loadMaps = function() { + var key, load_function_name, searchLoadIncluded, value, _results; + _results = []; + for (key in Gmaps) { + value = Gmaps[key]; + searchLoadIncluded = key.search(/load/); + if (searchLoadIncluded === -1) { + load_function_name = "load_" + key; + _results.push(Gmaps[load_function_name]()); + } else { + _results.push(void 0); + } + } + return _results; + }; + + window.Gmaps = Gmaps; + + this.Gmaps4Rails = (function() { + + function Gmaps4Rails() { + this.map = null; + this.serviceObject = null; + this.visibleInfoWindow = null; + this.userLocation = null; + this.geolocationFailure = function() { + return false; + }; + this.callback = function() { + return false; + }; + this.customClusterer = function() { + return false; + }; + this.infobox = function() { + return false; + }; + this.jsTemplate = false; + this.default_map_options = { + id: 'map', + draggable: true, + detect_location: false, + center_on_user: false, + center_latitude: 0, + center_longitude: 0, + zoom: 7, + maxZoom: null, + minZoom: null, + auto_adjust: true, + auto_zoom: true, + bounds: [], + raw: {} + }; + this.default_markers_conf = { + title: "", + picture: "", + width: 22, + length: 32, + draggable: false, + do_clustering: false, + randomize: false, + max_random_distance: 100, + list_container: null, + offset: 0, + raw: {} + }; + this.markers = []; + this.boundsObject = null; + this.polygons = []; + this.polylines = []; + this.circles = []; + this.markerClusterer = null; + this.markerImages = []; + this.polylines_conf = { + strokeColor: "#FF0000", + strokeOpacity: 1, + strokeWeight: 2, + clickable: false, + zIndex: null + }; + } + + Gmaps4Rails.prototype.initialize = function() { + this.serviceObject = this.createMap(); + this.map = this.serviceObject; + if (this.map_options.detect_location === true || this.map_options.center_on_user === true) { + this.findUserLocation(this); + } + return this.resetSidebarContent(); + }; + + Gmaps4Rails.prototype.findUserLocation = function(map_object) { + var positionFailure, positionSuccessful; + if (navigator.geolocation) { + positionSuccessful = function(position) { + map_object.userLocation = map_object.createLatLng(position.coords.latitude, position.coords.longitude); + if (map_object.map_options.center_on_user === true) { + return map_object.centerMapOnUser(); + } + }; + positionFailure = function() { + return map_object.geolocationFailure(true); + }; + return navigator.geolocation.getCurrentPosition(positionSuccessful, positionFailure); + } else { + return map_object.geolocationFailure(false); + } + }; + + Gmaps4Rails.prototype.create_direction = function() { + var directionsDisplay, directionsService, request; + directionsDisplay = new google.maps.DirectionsRenderer(); + directionsService = new google.maps.DirectionsService(); + directionsDisplay.setMap(this.serviceObject); + if (this.direction_conf.display_panel) { + directionsDisplay.setPanel(document.getElementById(this.direction_conf.panel_id)); + } + directionsDisplay.setOptions({ + suppressMarkers: false, + suppressInfoWindows: false, + suppressPolylines: false + }); + request = { + origin: this.direction_conf.origin, + destination: this.direction_conf.destination, + waypoints: this.direction_conf.waypoints, + optimizeWaypoints: this.direction_conf.optimizeWaypoints, + unitSystem: google.maps.DirectionsUnitSystem[this.direction_conf.unitSystem], + avoidHighways: this.direction_conf.avoidHighways, + avoidTolls: this.direction_conf.avoidTolls, + region: this.direction_conf.region, + travelMode: google.maps.DirectionsTravelMode[this.direction_conf.travelMode], + language: "en" + }; + return directionsService.route(request, function(response, status) { + if (status === google.maps.DirectionsStatus.OK) { + return directionsDisplay.setDirections(response); + } + }); + }; + + Gmaps4Rails.prototype.create_circles = function() { + var circle, _i, _len, _ref, _results; + _ref = this.circles; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + circle = _ref[_i]; + _results.push(this.create_circle(circle)); + } + return _results; + }; + + Gmaps4Rails.prototype.create_circle = function(circle) { + var newCircle; + if (circle === this.circles[0]) { + if (circle.strokeColor != null) { + this.circles_conf.strokeColor = circle.strokeColor; + } + if (circle.strokeOpacity != null) { + this.circles_conf.strokeOpacity = circle.strokeOpacity; + } + if (circle.strokeWeight != null) { + this.circles_conf.strokeWeight = circle.strokeWeight; + } + if (circle.fillColor != null) { + this.circles_conf.fillColor = circle.fillColor; + } + if (circle.fillOpacity != null) { + this.circles_conf.fillOpacity = circle.fillOpacity; + } + } + if ((circle.lat != null) && (circle.lng != null)) { + newCircle = new google.maps.Circle({ + center: this.createLatLng(circle.lat, circle.lng), + strokeColor: circle.strokeColor || this.circles_conf.strokeColor, + strokeOpacity: circle.strokeOpacity || this.circles_conf.strokeOpacity, + strokeWeight: circle.strokeWeight || this.circles_conf.strokeWeight, + fillOpacity: circle.fillOpacity || this.circles_conf.fillOpacity, + fillColor: circle.fillColor || this.circles_conf.fillColor, + clickable: circle.clickable || this.circles_conf.clickable, + zIndex: circle.zIndex || this.circles_conf.zIndex, + radius: circle.radius + }); + circle.serviceObject = newCircle; + return newCircle.setMap(this.serviceObject); + } + }; + + Gmaps4Rails.prototype.clear_circles = function() { + var circle, _i, _len, _ref, _results; + _ref = this.circles; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + circle = _ref[_i]; + _results.push(this.clear_circle(circle)); + } + return _results; + }; + + Gmaps4Rails.prototype.clear_circle = function(circle) { + return circle.serviceObject.setMap(null); + }; + + Gmaps4Rails.prototype.hide_circles = function() { + var circle, _i, _len, _ref, _results; + _ref = this.circles; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + circle = _ref[_i]; + _results.push(this.hide_circle(circle)); + } + return _results; + }; + + Gmaps4Rails.prototype.hide_circle = function(circle) { + return circle.serviceObject.setMap(null); + }; + + Gmaps4Rails.prototype.show_circles = function() { + var circle, _i, _len, _ref, _results; + _ref = this.circles; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + circle = _ref[_i]; + _results.push(this.show_circle(this.circle)); + } + return _results; + }; + + Gmaps4Rails.prototype.show_circle = function(circle) { + return circle.serviceObject.setMap(this.serviceObject); + }; + + Gmaps4Rails.prototype.create_polygons = function() { + var polygon, _i, _len, _ref, _results; + _ref = this.polygons; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + polygon = _ref[_i]; + _results.push(this.create_polygon(polygon)); + } + return _results; + }; + + Gmaps4Rails.prototype.create_polygon = function(polygon) { + var clickable, fillColor, fillOpacity, latlng, new_poly, point, polygon_coordinates, strokeColor, strokeOpacity, strokeWeight, _i, _len; + polygon_coordinates = []; + for (_i = 0, _len = polygon.length; _i < _len; _i++) { + point = polygon[_i]; + latlng = this.createLatLng(point.lat, point.lng); + polygon_coordinates.push(latlng); + if (point === polygon[0]) { + strokeColor = point.strokeColor || this.polygons_conf.strokeColor; + strokeOpacity = point.strokeOpacity || this.polygons_conf.strokeOpacity; + strokeWeight = point.strokeWeight || this.polygons_conf.strokeWeight; + fillColor = point.fillColor || this.polygons_conf.fillColor; + fillOpacity = point.fillOpacity || this.polygons_conf.fillOpacity; + clickable = point.clickable || this.polygons_conf.clickable; + } + } + new_poly = new google.maps.Polygon({ + paths: polygon_coordinates, + strokeColor: strokeColor, + strokeOpacity: strokeOpacity, + strokeWeight: strokeWeight, + fillColor: fillColor, + fillOpacity: fillOpacity, + clickable: clickable, + map: this.serviceObject + }); + return polygon.serviceObject = new_poly; + }; + + Gmaps4Rails.prototype.create_markers = function() { + this.createServiceMarkersFromMarkers(); + return this.clusterize(); + }; + + Gmaps4Rails.prototype.createServiceMarkersFromMarkers = function() { + var Lat, LatLng, Lng, index, marker, _len, _ref; + _ref = this.markers; + for (index = 0, _len = _ref.length; index < _len; index++) { + marker = _ref[index]; + if (!(this.markers[index].serviceObject != null)) { + Lat = this.markers[index].lat; + Lng = this.markers[index].lng; + if (this.markers_conf.randomize) { + LatLng = this.randomize(Lat, Lng); + Lat = LatLng[0]; + Lng = LatLng[1]; + } + this.markers[index].serviceObject = this.createMarker({ + "marker_picture": this.markers[index].picture ? this.markers[index].picture : this.markers_conf.picture, + "marker_width": this.markers[index].width ? this.markers[index].width : this.markers_conf.width, + "marker_height": this.markers[index].height ? this.markers[index].height : this.markers_conf.length, + "marker_title": this.markers[index].title ? this.markers[index].title : null, + "marker_anchor": this.markers[index].marker_anchor ? this.markers[index].marker_anchor : null, + "shadow_anchor": this.markers[index].shadow_anchor ? this.markers[index].shadow_anchor : null, + "shadow_picture": this.markers[index].shadow_picture ? this.markers[index].shadow_picture : null, + "shadow_width": this.markers[index].shadow_width ? this.markers[index].shadow_width : null, + "shadow_height": this.markers[index].shadow_height ? this.markers[index].shadow_height : null, + "marker_draggable": this.markers[index].draggable ? this.markers[index].draggable : this.markers_conf.draggable, + "rich_marker": this.markers[index].rich_marker ? this.markers[index].rich_marker : null, + "zindex": this.markers[index].zindex ? this.markers[index].zindex : null, + "Lat": Lat, + "Lng": Lng, + "index": index + }); + this.createInfoWindow(this.markers[index]); + this.createSidebar(this.markers[index]); + } + } + return this.markers_conf.offset = this.markers.length; + }; + + Gmaps4Rails.prototype.createImageAnchorPosition = function(anchorLocation) { + if (anchorLocation === null) { + return null; + } else { + return this.createPoint(anchorLocation[0], anchorLocation[1]); + } + }; + + Gmaps4Rails.prototype.replaceMarkers = function(new_markers) { + this.clearMarkers(); + this.markers = new Array; + this.boundsObject = this.createLatLngBounds(); + this.resetSidebarContent(); + this.markers_conf.offset = 0; + return this.addMarkers(new_markers); + }; + + Gmaps4Rails.prototype.addMarkers = function(new_markers) { + this.markers = this.markers.concat(new_markers); + this.create_markers(); + return this.adjustMapToBounds(); + }; + + Gmaps4Rails.prototype.createSidebar = function(marker_container) { + var aSel, currentMap, html, li, ul; + if (this.markers_conf.list_container) { + ul = document.getElementById(this.markers_conf.list_container); + li = document.createElement('li'); + aSel = document.createElement('a'); + aSel.href = 'javascript:void(0);'; + html = marker_container.sidebar != null ? marker_container.sidebar : "Marker"; + aSel.innerHTML = html; + currentMap = this; + aSel.onclick = this.sidebar_element_handler(currentMap, marker_container.serviceObject, 'click'); + li.appendChild(aSel); + return ul.appendChild(li); + } + }; + + Gmaps4Rails.prototype.sidebar_element_handler = function(currentMap, marker, eventType) { + return function() { + currentMap.map.panTo(marker.position); + return google.maps.event.trigger(marker, eventType); + }; + }; + + Gmaps4Rails.prototype.resetSidebarContent = function() { + var ul; + if (this.markers_conf.list_container !== null) { + ul = document.getElementById(this.markers_conf.list_container); + return ul.innerHTML = ""; + } + }; + + Gmaps4Rails.prototype.adjustMapToBounds = function() { + if (this.map_options.auto_adjust || this.map_options.bounds !== null) { + this.boundsObject = this.createLatLngBounds(); + if (this.map_options.auto_adjust) { + this.extendBoundsWithMarkers(); + this.updateBoundsWithPolylines(); + this.updateBoundsWithPolygons(); + this.updateBoundsWithCircles(); + } + this.extendMapBounds(); + return this.adaptMapToBounds(); + } + }; + + Gmaps4Rails.prototype.replacePolylines = function(new_polylines) { + this.destroy_polylines(); + this.polylines = new_polylines; + this.create_polylines(); + return this.adjustMapToBounds(); + }; + + Gmaps4Rails.prototype.destroy_polylines = function() { + var polyline, _i, _len, _ref; + _ref = this.polylines; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + polyline = _ref[_i]; + polyline.serviceObject.setMap(null); + } + return this.polylines = []; + }; + + Gmaps4Rails.prototype.create_polylines = function() { + var polyline, _i, _len, _ref, _results; + _ref = this.polylines; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + polyline = _ref[_i]; + _results.push(this.create_polyline(polyline)); + } + return _results; + }; + + Gmaps4Rails.prototype.exists = function(var_name) { + return var_name !== "" && typeof var_name !== "undefined"; + }; + + Gmaps4Rails.prototype.randomize = function(Lat0, Lng0) { + var Lat, Lng, dx, dy; + dx = this.markers_conf.max_random_distance * this.random(); + dy = this.markers_conf.max_random_distance * this.random(); + Lat = parseFloat(Lat0) + (180 / Math.PI) * (dy / 6378137); + Lng = parseFloat(Lng0) + (90 / Math.PI) * (dx / 6378137) / Math.cos(Lat0); + return [Lat, Lng]; + }; + + Gmaps4Rails.prototype.mergeObjectWithDefault = function(object1, object2) { + var copy_object1, key, value; + copy_object1 = {}; + for (key in object1) { + value = object1[key]; + copy_object1[key] = value; + } + for (key in object2) { + value = object2[key]; + if (copy_object1[key] == null) copy_object1[key] = value; + } + return copy_object1; + }; + + Gmaps4Rails.prototype.mergeWithDefault = function(objectName) { + var default_object, object; + default_object = this["default_" + objectName]; + object = this[objectName]; + this[objectName] = this.mergeObjectWithDefault(object, default_object); + return true; + }; + + Gmaps4Rails.prototype.random = function() { + return Math.random() * 2 - 1; + }; + + return Gmaps4Rails; + + })(); + +}).call(this); diff --git a/public/javascripts/gmaps4rails/gmaps4rails.bing.js b/public/javascripts/gmaps4rails/gmaps4rails.bing.js new file mode 100644 index 0000000..10df8a9 --- /dev/null +++ b/public/javascripts/gmaps4rails/gmaps4rails.bing.js @@ -0,0 +1,226 @@ +(function() { + var __hasProp = Object.prototype.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }; + + this.Gmaps4RailsBing = (function(_super) { + + __extends(Gmaps4RailsBing, _super); + + function Gmaps4RailsBing() { + Gmaps4RailsBing.__super__.constructor.apply(this, arguments); + this.map_options = { + type: "road" + }; + this.markers_conf = { + infobox: "description" + }; + this.mergeWithDefault("map_options"); + this.mergeWithDefault("markers_conf"); + } + + Gmaps4RailsBing.prototype.getMapType = function() { + switch (this.map_options.type) { + case "road": + return Microsoft.Maps.MapTypeId.road; + case "aerial": + return Microsoft.Maps.MapTypeId.aerial; + case "auto": + return Microsoft.Maps.MapTypeId.auto; + case "birdseye": + return Microsoft.Maps.MapTypeId.birdseye; + case "collinsBart": + return Microsoft.Maps.MapTypeId.collinsBart; + case "mercator": + return Microsoft.Maps.MapTypeId.mercator; + case "ordnanceSurvey": + return Microsoft.Maps.MapTypeId.ordnanceSurvey; + default: + return Microsoft.Maps.MapTypeId.auto; + } + }; + + Gmaps4RailsBing.prototype.createPoint = function(lat, lng) { + return new Microsoft.Maps.Point(lat, lng); + }; + + Gmaps4RailsBing.prototype.createLatLng = function(lat, lng) { + return new Microsoft.Maps.Location(lat, lng); + }; + + Gmaps4RailsBing.prototype.createLatLngBounds = function() {}; + + Gmaps4RailsBing.prototype.createMap = function() { + return new Microsoft.Maps.Map(document.getElementById(this.map_options.id), { + credentials: this.map_options.provider_key, + mapTypeId: this.getMapType(), + center: this.createLatLng(this.map_options.center_latitude, this.map_options.center_longitude), + zoom: this.map_options.zoom + }); + }; + + Gmaps4RailsBing.prototype.createSize = function(width, height) { + return new google.maps.Size(width, height); + }; + + Gmaps4RailsBing.prototype.createMarker = function(args) { + var marker, markerLatLng; + markerLatLng = this.createLatLng(args.Lat, args.Lng); + marker; + if (args.marker_picture === "") { + marker = new Microsoft.Maps.Pushpin(this.createLatLng(args.Lat, args.Lng), { + draggable: args.marker_draggable, + anchor: this.createImageAnchorPosition(args.Lat, args.Lng), + text: args.marker_title + }); + } else { + marker = new Microsoft.Maps.Pushpin(this.createLatLng(args.Lat, args.Lng), { + draggable: args.marker_draggable, + anchor: this.createImageAnchorPosition(args.Lat, args.Lng), + icon: args.marker_picture, + height: args.marker_height, + text: args.marker_title, + width: args.marker_width + }); + } + this.addToMap(marker); + return marker; + }; + + Gmaps4RailsBing.prototype.clearMarkers = function() { + var marker, _i, _len, _ref, _results; + _ref = this.markers; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + marker = _ref[_i]; + _results.push(this.clearMarker(marker)); + } + return _results; + }; + + Gmaps4RailsBing.prototype.clearMarker = function(marker) { + return this.removeFromMap(marker.serviceObject); + }; + + Gmaps4RailsBing.prototype.showMarkers = function() { + var marker, _i, _len, _ref, _results; + _ref = this.markers; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + marker = _ref[_i]; + _results.push(this.showMarker(marker)); + } + return _results; + }; + + Gmaps4RailsBing.prototype.showMarker = function(marker) { + return marker.serviceObject.setOptions({ + visible: true + }); + }; + + Gmaps4RailsBing.prototype.hideMarkers = function() { + var marker, _i, _len, _ref, _results; + _ref = this.markers; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + marker = _ref[_i]; + _results.push(this.hideMarker(marker)); + } + return _results; + }; + + Gmaps4RailsBing.prototype.hideMarker = function(marker) { + return marker.serviceObject.setOptions({ + visible: false + }); + }; + + Gmaps4RailsBing.prototype.extendBoundsWithMarkers = function() { + var locationsArray, marker, _i, _len, _ref; + locationsArray = []; + _ref = this.markers; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + marker = _ref[_i]; + locationsArray.push(marker.serviceObject.getLocation()); + } + return this.boundsObject = Microsoft.Maps.LocationRect.fromLocations(locationsArray); + }; + + Gmaps4RailsBing.prototype.createClusterer = function(markers_array) {}; + + Gmaps4RailsBing.prototype.clearClusterer = function() {}; + + Gmaps4RailsBing.prototype.clusterize = function() {}; + + Gmaps4RailsBing.prototype.createInfoWindow = function(marker_container) { + var currentMap; + if (marker_container.description != null) { + if (this.markers_conf.infobox === "description") { + marker_container.info_window = new Microsoft.Maps.Infobox(marker_container.serviceObject.getLocation(), { + description: marker_container.description, + visible: false, + showCloseButton: true + }); + } else { + marker_container.info_window = new Microsoft.Maps.Infobox(marker_container.serviceObject.getLocation(), { + htmlContent: marker_container.description, + visible: false + }); + } + currentMap = this; + Microsoft.Maps.Events.addHandler(marker_container.serviceObject, 'click', this.openInfoWindow(currentMap, marker_container.info_window)); + return this.addToMap(marker_container.info_window); + } + }; + + Gmaps4RailsBing.prototype.openInfoWindow = function(currentMap, infoWindow) { + return function() { + if (currentMap.visibleInfoWindow) { + currentMap.visibleInfoWindow.setOptions({ + visible: false + }); + } + infoWindow.setOptions({ + visible: true + }); + return currentMap.visibleInfoWindow = infoWindow; + }; + }; + + Gmaps4RailsBing.prototype.fitBounds = function() { + return this.serviceObject.setView({ + bounds: this.boundsObject + }); + }; + + Gmaps4RailsBing.prototype.addToMap = function(object) { + return this.serviceObject.entities.push(object); + }; + + Gmaps4RailsBing.prototype.removeFromMap = function(object) { + return this.serviceObject.entities.remove(object); + }; + + Gmaps4RailsBing.prototype.centerMapOnUser = function() { + return this.serviceObject.setView({ + center: this.userLocation + }); + }; + + Gmaps4RailsBing.prototype.updateBoundsWithPolylines = function() {}; + + Gmaps4RailsBing.prototype.updateBoundsWithPolygons = function() {}; + + Gmaps4RailsBing.prototype.updateBoundsWithCircles = function() {}; + + Gmaps4RailsBing.prototype.extendMapBounds = function() {}; + + Gmaps4RailsBing.prototype.adaptMapToBounds = function() { + return this.fitBounds(); + }; + + return Gmaps4RailsBing; + + })(Gmaps4Rails); + +}).call(this); diff --git a/public/javascripts/gmaps4rails/gmaps4rails.googlemaps.js b/public/javascripts/gmaps4rails/gmaps4rails.googlemaps.js new file mode 100644 index 0000000..a7d848a --- /dev/null +++ b/public/javascripts/gmaps4rails/gmaps4rails.googlemaps.js @@ -0,0 +1,430 @@ +(function() { + var __hasProp = Object.prototype.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }; + + this.Gmaps4RailsGoogle = (function(_super) { + + __extends(Gmaps4RailsGoogle, _super); + + function Gmaps4RailsGoogle() { + Gmaps4RailsGoogle.__super__.constructor.apply(this, arguments); + this.map_options = { + disableDefaultUI: false, + disableDoubleClickZoom: false, + type: "ROADMAP" + }; + this.markers_conf = { + clusterer_gridSize: 50, + clusterer_maxZoom: 5, + custom_cluster_pictures: null, + custom_infowindow_class: null + }; + this.mergeWithDefault("map_options"); + this.mergeWithDefault("markers_conf"); + this.kml_options = { + clickable: true, + preserveViewport: false, + suppressInfoWindows: false + }; + this.polygons_conf = { + strokeColor: "#FFAA00", + strokeOpacity: 0.8, + strokeWeight: 2, + fillColor: "#000000", + fillOpacity: 0.35, + clickable: false + }; + this.circles_conf = { + fillColor: "#00AAFF", + fillOpacity: 0.35, + strokeColor: "#FFAA00", + strokeOpacity: 0.8, + strokeWeight: 2, + clickable: false, + zIndex: null + }; + this.direction_conf = { + panel_id: null, + display_panel: false, + origin: null, + destination: null, + waypoints: [], + optimizeWaypoints: false, + unitSystem: "METRIC", + avoidHighways: false, + avoidTolls: false, + region: null, + travelMode: "DRIVING" + }; + } + + Gmaps4RailsGoogle.prototype.createPoint = function(lat, lng) { + return new google.maps.Point(lat, lng); + }; + + Gmaps4RailsGoogle.prototype.createLatLng = function(lat, lng) { + return new google.maps.LatLng(lat, lng); + }; + + Gmaps4RailsGoogle.prototype.createLatLngBounds = function() { + return new google.maps.LatLngBounds(); + }; + + Gmaps4RailsGoogle.prototype.createMap = function() { + var defaultOptions, mergedOptions; + defaultOptions = { + maxZoom: this.map_options.maxZoom, + minZoom: this.map_options.minZoom, + zoom: this.map_options.zoom, + center: this.createLatLng(this.map_options.center_latitude, this.map_options.center_longitude), + mapTypeId: google.maps.MapTypeId[this.map_options.type], + mapTypeControl: this.map_options.mapTypeControl, + disableDefaultUI: this.map_options.disableDefaultUI, + disableDoubleClickZoom: this.map_options.disableDoubleClickZoom, + draggable: this.map_options.draggable + }; + mergedOptions = this.mergeObjectWithDefault(this.map_options.raw, defaultOptions); + return new google.maps.Map(document.getElementById(this.map_options.id), mergedOptions); + }; + + Gmaps4RailsGoogle.prototype.createMarkerImage = function(markerPicture, markerSize, origin, anchor, scaledSize) { + return new google.maps.MarkerImage(markerPicture, markerSize, origin, anchor, scaledSize); + }; + + Gmaps4RailsGoogle.prototype.createSize = function(width, height) { + return new google.maps.Size(width, height); + }; + + Gmaps4RailsGoogle.prototype.createMarker = function(args) { + var defaultOptions, imageAnchorPosition, markerImage, markerLatLng, mergedOptions, shadowAnchorPosition, shadowImage; + markerLatLng = this.createLatLng(args.Lat, args.Lng); + if (args.marker_picture === "" && args.rich_marker === null) { + defaultOptions = { + position: markerLatLng, + map: this.serviceObject, + title: args.marker_title, + draggable: args.marker_draggable, + zIndex: args.zindex + }; + mergedOptions = this.mergeObjectWithDefault(this.markers_conf.raw, defaultOptions); + return new google.maps.Marker(mergedOptions); + } + if (args.rich_marker !== null) { + return new RichMarker({ + position: markerLatLng, + map: this.serviceObject, + draggable: args.marker_draggable, + content: args.rich_marker, + flat: args.marker_anchor === null ? false : args.marker_anchor[1], + anchor: args.marker_anchor === null ? 0 : args.marker_anchor[0], + zIndex: args.zindex + }); + } + imageAnchorPosition = this.createImageAnchorPosition(args.marker_anchor); + shadowAnchorPosition = this.createImageAnchorPosition(args.shadow_anchor); + markerImage = this.createOrRetrieveImage(args.marker_picture, args.marker_width, args.marker_height, imageAnchorPosition); + shadowImage = this.createOrRetrieveImage(args.shadow_picture, args.shadow_width, args.shadow_height, shadowAnchorPosition); + defaultOptions = { + position: markerLatLng, + map: this.serviceObject, + icon: markerImage, + title: args.marker_title, + draggable: args.marker_draggable, + shadow: shadowImage, + zIndex: args.zindex + }; + mergedOptions = this.mergeObjectWithDefault(this.markers_conf.raw, defaultOptions); + return new google.maps.Marker(mergedOptions); + }; + + Gmaps4RailsGoogle.prototype.includeMarkerImage = function(arr, obj) { + var index, object, _len; + for (index = 0, _len = arr.length; index < _len; index++) { + object = arr[index]; + if (object.url === obj) return index; + } + return false; + }; + + Gmaps4RailsGoogle.prototype.createOrRetrieveImage = function(currentMarkerPicture, markerWidth, markerHeight, imageAnchorPosition) { + var markerImage, test_image_index; + if (currentMarkerPicture === "" || currentMarkerPicture === null) { + return null; + } + test_image_index = this.includeMarkerImage(this.markerImages, currentMarkerPicture); + switch (test_image_index) { + case false: + markerImage = this.createMarkerImage(currentMarkerPicture, this.createSize(markerWidth, markerHeight), null, imageAnchorPosition, null); + this.markerImages.push(markerImage); + return markerImage; + break; + default: + if (typeof test_image_index === 'number') { + return this.markerImages[test_image_index]; + } + return false; + } + }; + + Gmaps4RailsGoogle.prototype.clearMarkers = function() { + var marker, _i, _len, _ref, _results; + _ref = this.markers; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + marker = _ref[_i]; + _results.push(this.clearMarker(marker)); + } + return _results; + }; + + Gmaps4RailsGoogle.prototype.showMarkers = function() { + var marker, _i, _len, _ref, _results; + _ref = this.markers; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + marker = _ref[_i]; + _results.push(this.showMarker(marker)); + } + return _results; + }; + + Gmaps4RailsGoogle.prototype.hideMarkers = function() { + var marker, _i, _len, _ref, _results; + _ref = this.markers; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + marker = _ref[_i]; + _results.push(this.hideMarker(marker)); + } + return _results; + }; + + Gmaps4RailsGoogle.prototype.clearMarker = function(marker) { + return marker.serviceObject.setMap(null); + }; + + Gmaps4RailsGoogle.prototype.showMarker = function(marker) { + return marker.serviceObject.setVisible(true); + }; + + Gmaps4RailsGoogle.prototype.hideMarker = function(marker) { + return marker.serviceObject.setVisible(false); + }; + + Gmaps4RailsGoogle.prototype.extendBoundsWithMarkers = function() { + var marker, _i, _len, _ref, _results; + _ref = this.markers; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + marker = _ref[_i]; + _results.push(this.boundsObject.extend(marker.serviceObject.position)); + } + return _results; + }; + + Gmaps4RailsGoogle.prototype.createClusterer = function(markers_array) { + return new MarkerClusterer(this.serviceObject, markers_array, { + maxZoom: this.markers_conf.clusterer_maxZoom, + gridSize: this.markers_conf.clusterer_gridSize, + styles: this.customClusterer() + }); + }; + + Gmaps4RailsGoogle.prototype.clearClusterer = function() { + return this.markerClusterer.clearMarkers(); + }; + + Gmaps4RailsGoogle.prototype.clusterize = function() { + var marker, markers_array, _i, _len, _ref; + if (this.markers_conf.do_clustering === true) { + if (this.markerClusterer !== null) this.clearClusterer(); + markers_array = new Array; + _ref = this.markers; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + marker = _ref[_i]; + markers_array.push(marker.serviceObject); + } + return this.markerClusterer = this.createClusterer(markers_array); + } + }; + + Gmaps4RailsGoogle.prototype.createInfoWindow = function(marker_container) { + var boxText, currentMap; + if (typeof this.jsTemplate === "function" || (marker_container.description != null)) { + if (typeof this.jsTemplate === "function") { + marker_container.description = this.jsTemplate(marker_container); + } + if (this.markers_conf.custom_infowindow_class !== null) { + boxText = document.createElement("div"); + boxText.setAttribute("class", this.markers_conf.custom_infowindow_class); + boxText.innerHTML = marker_container.description; + marker_container.infowindow = new InfoBox(this.infobox(boxText)); + currentMap = this; + return google.maps.event.addListener(marker_container.serviceObject, 'click', this.openInfoWindow(currentMap, marker_container.infowindow, marker_container.serviceObject)); + } else { + marker_container.infowindow = new google.maps.InfoWindow({ + content: marker_container.description + }); + currentMap = this; + return google.maps.event.addListener(marker_container.serviceObject, 'click', this.openInfoWindow(currentMap, marker_container.infowindow, marker_container.serviceObject)); + } + } + }; + + Gmaps4RailsGoogle.prototype.openInfoWindow = function(currentMap, infoWindow, marker) { + return function() { + if (currentMap.visibleInfoWindow !== null) { + currentMap.visibleInfoWindow.close(); + } + infoWindow.open(currentMap.serviceObject, marker); + return currentMap.visibleInfoWindow = infoWindow; + }; + }; + + Gmaps4RailsGoogle.prototype.createKmlLayer = function(kml) { + var kml_options; + kml_options = kml.options || {}; + kml_options = this.mergeObjectWithDefault(kml_options, this.kml_options); + kml = new google.maps.KmlLayer(kml.url, kml_options); + kml.setMap(this.serviceObject); + return kml; + }; + + Gmaps4RailsGoogle.prototype.create_polyline = function(polyline) { + var clickable, decoded_array, element, latlng, new_poly, point, polyline_coordinates, strokeColor, strokeOpacity, strokeWeight, zIndex, _i, _j, _len, _len2; + polyline_coordinates = []; + for (_i = 0, _len = polyline.length; _i < _len; _i++) { + element = polyline[_i]; + if (element.coded_array != null) { + decoded_array = new google.maps.geometry.encoding.decodePath(element.coded_array); + for (_j = 0, _len2 = decoded_array.length; _j < _len2; _j++) { + point = decoded_array[_j]; + polyline_coordinates.push(point); + } + } else { + if (element === polyline[0]) { + strokeColor = element.strokeColor || this.polylines_conf.strokeColor; + strokeOpacity = element.strokeOpacity || this.polylines_conf.strokeOpacity; + strokeWeight = element.strokeWeight || this.polylines_conf.strokeWeight; + clickable = element.clickable || this.polylines_conf.clickable; + zIndex = element.zIndex || this.polylines_conf.zIndex; + } + if ((element.lat != null) && (element.lng != null)) { + latlng = this.createLatLng(element.lat, element.lng); + polyline_coordinates.push(latlng); + } + } + } + new_poly = new google.maps.Polyline({ + path: polyline_coordinates, + strokeColor: strokeColor, + strokeOpacity: strokeOpacity, + strokeWeight: strokeWeight, + clickable: clickable, + zIndex: zIndex + }); + polyline.serviceObject = new_poly; + return new_poly.setMap(this.serviceObject); + }; + + Gmaps4RailsGoogle.prototype.updateBoundsWithPolylines = function() { + var point, polyline, polyline_points, _i, _len, _ref, _results; + _ref = this.polylines; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + polyline = _ref[_i]; + polyline_points = polyline.serviceObject.latLngs.getArray()[0].getArray(); + _results.push((function() { + var _j, _len2, _results2; + _results2 = []; + for (_j = 0, _len2 = polyline_points.length; _j < _len2; _j++) { + point = polyline_points[_j]; + _results2.push(this.boundsObject.extend(point)); + } + return _results2; + }).call(this)); + } + return _results; + }; + + Gmaps4RailsGoogle.prototype.create_kml = function() { + var kml, _i, _len, _ref, _results; + _ref = this.kml; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + kml = _ref[_i]; + _results.push(kml.serviceObject = this.createKmlLayer(kml)); + } + return _results; + }; + + Gmaps4RailsGoogle.prototype.fitBounds = function() { + if (!this.boundsObject.isEmpty()) { + return this.serviceObject.fitBounds(this.boundsObject); + } + }; + + Gmaps4RailsGoogle.prototype.centerMapOnUser = function() { + return this.serviceObject.setCenter(this.userLocation); + }; + + Gmaps4RailsGoogle.prototype.updateBoundsWithPolygons = function() { + var point, polygon, polygon_points, _i, _len, _ref, _results; + _ref = this.polygons; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + polygon = _ref[_i]; + polygon_points = polygon.serviceObject.latLngs.getArray()[0].getArray(); + _results.push((function() { + var _j, _len2, _results2; + _results2 = []; + for (_j = 0, _len2 = polygon_points.length; _j < _len2; _j++) { + point = polygon_points[_j]; + _results2.push(this.boundsObject.extend(point)); + } + return _results2; + }).call(this)); + } + return _results; + }; + + Gmaps4RailsGoogle.prototype.updateBoundsWithCircles = function() { + var circle, _i, _len, _ref, _results; + _ref = this.circles; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + circle = _ref[_i]; + this.boundsObject.extend(circle.serviceObject.getBounds().getNorthEast()); + _results.push(this.boundsObject.extend(circle.serviceObject.getBounds().getSouthWest())); + } + return _results; + }; + + Gmaps4RailsGoogle.prototype.extendMapBounds = function() { + var bound, _i, _len, _ref, _results; + _ref = this.map_options.bounds; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + bound = _ref[_i]; + _results.push(this.boundsObject.extend(this.createLatLng(bound.lat, bound.lng))); + } + return _results; + }; + + Gmaps4RailsGoogle.prototype.adaptMapToBounds = function() { + var map_center; + if (!this.map_options.auto_zoom) { + map_center = this.boundsObject.getCenter(); + this.map_options.center_latitude = map_center.lat(); + this.map_options.center_longitude = map_center.lng(); + return this.serviceObject.setCenter(map_center); + } else { + return this.fitBounds(); + } + }; + + return Gmaps4RailsGoogle; + + })(Gmaps4Rails); + +}).call(this); diff --git a/public/javascripts/gmaps4rails/gmaps4rails.mapquest.js b/public/javascripts/gmaps4rails/gmaps4rails.mapquest.js new file mode 100644 index 0000000..a7d9d9c --- /dev/null +++ b/public/javascripts/gmaps4rails/gmaps4rails.mapquest.js @@ -0,0 +1,178 @@ +(function() { + var __hasProp = Object.prototype.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }; + + this.Gmaps4RailsMapquest = (function(_super) { + + __extends(Gmaps4RailsMapquest, _super); + + function Gmaps4RailsMapquest() { + Gmaps4RailsMapquest.__super__.constructor.apply(this, arguments); + this.map_options = { + type: "map" + }; + this.markers_conf = {}; + this.mergeWithDefault("markers_conf"); + this.mergeWithDefault("map_options"); + } + + Gmaps4RailsMapquest.prototype.createPoint = function(lat, lng) { + return new MQA.Poi({ + lat: lat, + lng: lng + }); + }; + + Gmaps4RailsMapquest.prototype.createLatLng = function(lat, lng) { + return { + lat: lat, + lng: lng + }; + }; + + Gmaps4RailsMapquest.prototype.createLatLngBounds = function() {}; + + Gmaps4RailsMapquest.prototype.createMap = function() { + var map; + map = new MQA.TileMap(document.getElementById(this.map_options.id), this.map_options.zoom, { + lat: this.map_options.center_latitude, + lng: this.map_options.center_longitude + }, this.map_options.type); + MQA.withModule('zoomcontrol3', (function() { + return map.addControl(new MQA.LargeZoomControl3(), new MQA.MapCornerPlacement(MQA.MapCorner.TOP_LEFT)); + })); + return map; + }; + + Gmaps4RailsMapquest.prototype.createMarkerImage = function(markerPicture, markerSize, origin, anchor, scaledSize) {}; + + Gmaps4RailsMapquest.prototype.createMarker = function(args) { + var icon, marker; + marker = new MQA.Poi({ + lat: args.Lat, + lng: args.Lng + }); + if (args.marker_picture !== "") { + icon = new MQA.Icon(args.marker_picture, args.marker_height, args.marker_width); + marker.setIcon(icon); + if (args.marker_anchor !== null) { + marker.setBias({ + x: args.marker_anchor[0], + y: args.marker_anchor[1] + }); + } + } + if (args.shadow_picture !== "") { + icon = new MQA.Icon(args.shadow_picture, args.shadow_height, args.shadow_width); + marker.setShadow(icon); + if (args.shadow_anchor !== null) { + marker.setShadowOffset({ + x: args.shadow_anchor[0], + y: args.shadow_anchor[1] + }); + } + } + this.addToMap(marker); + return marker; + }; + + Gmaps4RailsMapquest.prototype.clearMarkers = function() { + var marker, _i, _len, _results; + _results = []; + for (_i = 0, _len = markers.length; _i < _len; _i++) { + marker = markers[_i]; + _results.push(this.clearMarker(marker)); + } + return _results; + }; + + Gmaps4RailsMapquest.prototype.showMarkers = function() { + var marker, _i, _len, _results; + _results = []; + for (_i = 0, _len = markers.length; _i < _len; _i++) { + marker = markers[_i]; + _results.push(this.showMarker(marker)); + } + return _results; + }; + + Gmaps4RailsMapquest.prototype.hideMarkers = function() { + var marker, _i, _len, _results; + _results = []; + for (_i = 0, _len = markers.length; _i < _len; _i++) { + marker = markers[_i]; + _results.push(this.hideMarker(marker)); + } + return _results; + }; + + Gmaps4RailsMapquest.prototype.clearMarker = function(marker) { + return this.removeFromMap(marker.serviceObject); + }; + + Gmaps4RailsMapquest.prototype.showMarker = function(marker) {}; + + Gmaps4RailsMapquest.prototype.hideMarker = function(marker) {}; + + Gmaps4RailsMapquest.prototype.extendBoundsWithMarkers = function() { + var marker, _i, _len, _ref, _results; + if (this.markers.length >= 2) { + this.boundsObject = new MQA.RectLL(this.markers[0].serviceObject.latLng, this.markers[1].serviceObject.latLng); + _ref = this.markers; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + marker = _ref[_i]; + _results.push(this.boundsObject.extend(marker.serviceObject.latLng)); + } + return _results; + } + }; + + Gmaps4RailsMapquest.prototype.createClusterer = function(markers_array) {}; + + Gmaps4RailsMapquest.prototype.clearClusterer = function() {}; + + Gmaps4RailsMapquest.prototype.clusterize = function() {}; + + Gmaps4RailsMapquest.prototype.createInfoWindow = function(marker_container) { + return marker_container.serviceObject.setInfoTitleHTML(marker_container.description); + }; + + Gmaps4RailsMapquest.prototype.fitBounds = function() { + if (this.markers.length >= 2) { + this.serviceObject.zoomToRect(this.boundsObject); + } + if (this.markers.length === 1) { + return this.serviceObject.setCenter(this.markers[0].serviceObject.latLng); + } + }; + + Gmaps4RailsMapquest.prototype.centerMapOnUser = function() { + return this.serviceObject.setCenter(this.userLocation); + }; + + Gmaps4RailsMapquest.prototype.addToMap = function(object) { + return this.serviceObject.addShape(object); + }; + + Gmaps4RailsMapquest.prototype.removeFromMap = function(object) { + return this.serviceObject.removeShape(object); + }; + + Gmaps4RailsMapquest.prototype.updateBoundsWithPolylines = function() {}; + + Gmaps4RailsMapquest.prototype.updateBoundsWithPolygons = function() {}; + + Gmaps4RailsMapquest.prototype.updateBoundsWithCircles = function() {}; + + Gmaps4RailsMapquest.prototype.extendMapBounds = function() {}; + + Gmaps4RailsMapquest.prototype.adaptMapToBounds = function() { + return this.fitBounds(); + }; + + return Gmaps4RailsMapquest; + + })(Gmaps4Rails); + +}).call(this); diff --git a/public/javascripts/gmaps4rails/gmaps4rails.openlayers.js b/public/javascripts/gmaps4rails/gmaps4rails.openlayers.js new file mode 100644 index 0000000..0620536 --- /dev/null +++ b/public/javascripts/gmaps4rails/gmaps4rails.openlayers.js @@ -0,0 +1,265 @@ +(function() { + var __hasProp = Object.prototype.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }; + + this.Gmaps4RailsOpenlayers = (function(_super) { + + __extends(Gmaps4RailsOpenlayers, _super); + + function Gmaps4RailsOpenlayers() { + Gmaps4RailsOpenlayers.__super__.constructor.apply(this, arguments); + this.map_options = {}; + this.mergeWithDefault("map_options"); + this.markers_conf = {}; + this.mergeWithDefault("markers_conf"); + this.openMarkers = null; + this.markersLayer = null; + this.markersControl = null; + this.polylinesLayer = null; + } + + Gmaps4RailsOpenlayers.prototype.createPoint = function(lat, lng) {}; + + Gmaps4RailsOpenlayers.prototype.createLatLng = function(lat, lng) { + return new OpenLayers.LonLat(lng, lat).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); + }; + + Gmaps4RailsOpenlayers.prototype.createAnchor = function(offset) { + if (offset === null) return null; + return new OpenLayers.Pixel(offset[0], offset[1]); + }; + + Gmaps4RailsOpenlayers.prototype.createSize = function(width, height) { + return new OpenLayers.Size(width, height); + }; + + Gmaps4RailsOpenlayers.prototype.createLatLngBounds = function() { + return new OpenLayers.Bounds(); + }; + + Gmaps4RailsOpenlayers.prototype.createMap = function() { + var map; + map = new OpenLayers.Map(this.map_options.id); + map.addLayer(new OpenLayers.Layer.OSM()); + map.setCenter(this.createLatLng(this.map_options.center_latitude, this.map_options.center_longitude), this.map_options.zoom); + return map; + }; + + Gmaps4RailsOpenlayers.prototype.createMarker = function(args) { + var marker, style_mark; + style_mark = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']); + style_mark.fillOpacity = 1; + if (this.markersLayer === null) { + this.markersLayer = new OpenLayers.Layer.Vector("Markers", null); + this.serviceObject.addLayer(this.markersLayer); + this.markersLayer.events.register("featureselected", this.markersLayer, this.onFeatureSelect); + this.markersLayer.events.register("featureunselected", this.markersLayer, this.onFeatureUnselect); + this.markersControl = new OpenLayers.Control.SelectFeature(this.markersLayer); + this.serviceObject.addControl(this.markersControl); + this.markersControl.activate(); + } + if (args.marker_picture === "") { + style_mark.graphicHeight = 30; + style_mark.externalGraphic = "http://openlayers.org/dev/img/marker-blue.png"; + } else { + style_mark.graphicWidth = args.marker_width; + style_mark.graphicHeight = args.marker_height; + style_mark.externalGraphic = args.marker_picture; + if (args.marker_anchor !== null) { + style_mark.graphicXOffset = args.marker_anchor[0]; + style_mark.graphicYOffset = args.marker_anchor[1]; + } + if (args.shadow_picture !== "") { + style_mark.backgroundGraphic = args.shadow_picture; + style_mark.backgroundWidth = args.shadow_width; + style_mark.backgroundHeight = args.shadow_height; + if (args.shadow_anchor !== null) { + style_mark.backgroundXOffset = args.shadow_anchor[0]; + style_mark.backgroundYOffset = args.shadow_anchor[1]; + } + } + } + style_mark.graphicTitle = args.title; + marker = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(args.Lng, args.Lat), null, style_mark); + marker.geometry.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); + this.markersLayer.addFeatures([marker]); + return marker; + }; + + Gmaps4RailsOpenlayers.prototype.clearMarkers = function() { + this.clearMarkersLayerIfExists(); + this.markersLayer = null; + return this.boundsObject = new OpenLayers.Bounds(); + }; + + Gmaps4RailsOpenlayers.prototype.clearMarkersLayerIfExists = function() { + if (this.markersLayer !== null && this.serviceObject.getLayer(this.markersLayer.id) !== null) { + return this.serviceObject.removeLayer(this.markersLayer); + } + }; + + Gmaps4RailsOpenlayers.prototype.extendBoundsWithMarkers = function() { + var marker, _i, _len, _ref, _results; + console.log("here"); + _ref = this.markers; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + marker = _ref[_i]; + _results.push(this.boundsObject.extend(this.createLatLng(marker.lat, marker.lng))); + } + return _results; + }; + + Gmaps4RailsOpenlayers.prototype.createClusterer = function(markers_array) { + var clusters, funcs, options, strategy, style; + options = { + pointRadius: "${radius}", + fillColor: "#ffcc66", + fillOpacity: 0.8, + strokeColor: "#cc6633", + strokeWidth: "${width}", + strokeOpacity: 0.8 + }; + funcs = { + context: { + width: function(feature) { + var _ref; + return (_ref = feature.cluster) != null ? _ref : { + 2: 1 + }; + }, + radius: function(feature) { + var pix; + pix = 2; + if (feature.cluster) pix = Math.min(feature.attributes.count, 7) + 2; + return pix; + } + } + }; + style = new OpenLayers.Style(options, funcs); + strategy = new OpenLayers.Strategy.Cluster(); + clusters = new OpenLayers.Layer.Vector("Clusters", { + strategies: [strategy], + styleMap: new OpenLayers.StyleMap({ + "default": style, + "select": { + fillColor: "#8aeeef", + strokeColor: "#32a8a9" + } + }) + }); + this.clearMarkersLayerIfExists(); + this.serviceObject.addLayer(clusters); + clusters.addFeatures(markers_array); + return clusters; + }; + + Gmaps4RailsOpenlayers.prototype.clusterize = function() { + var marker, markers_array, _i, _len, _ref; + if (this.markers_conf.do_clustering === true) { + if (this.markerClusterer !== null) this.clearClusterer(); + markers_array = new Array; + _ref = this.markers; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + marker = _ref[_i]; + markers_array.push(marker.serviceObject); + } + return this.markerClusterer = this.createClusterer(markers_array); + } + }; + + Gmaps4RailsOpenlayers.prototype.clearClusterer = function() { + return this.serviceObject.removeLayer(this.markerClusterer); + }; + + Gmaps4RailsOpenlayers.prototype.createInfoWindow = function(marker_container) { + if (marker_container.description != null) { + return marker_container.serviceObject.infoWindow = marker_container.description; + } + }; + + Gmaps4RailsOpenlayers.prototype.onPopupClose = function(evt) { + return this.markersControl.unselect(this.feature); + }; + + Gmaps4RailsOpenlayers.prototype.onFeatureSelect = function(evt) { + var feature, popup; + feature = evt.feature; + popup = new OpenLayers.Popup.FramedCloud("featurePopup", feature.geometry.getBounds().getCenterLonLat(), new OpenLayers.Size(300, 200), feature.infoWindow, null, true, this.onPopupClose); + feature.popup = popup; + popup.feature = feature; + return this.map.addPopup(popup); + }; + + Gmaps4RailsOpenlayers.prototype.onFeatureUnselect = function(evt) { + var feature; + feature = evt.feature; + if (feature.popup) { + this.map.removePopup(feature.popup); + feature.popup.destroy(); + return feature.popup = null; + } + }; + + Gmaps4RailsOpenlayers.prototype.create_polyline = function(polyline) { + var clickable, element, latlng, line_points, line_style, polyline_coordinates, strokeColor, strokeOpacity, strokeWeight, zIndex, _i, _len; + if (this.polylinesLayer === null) { + this.polylinesLayer = new OpenLayers.Layer.Vector("Polylines", null); + this.serviceObject.addLayer(this.polylinesLayer); + this.polylinesLayer.events.register("featureselected", this.polylinesLayer, this.onFeatureSelect); + this.polylinesLayer.events.register("featureunselected", this.polylinesLayer, this.onFeatureUnselect); + this.polylinesControl = new OpenLayers.Control.DrawFeature(this.polylinesLayer, OpenLayers.Handler.Path); + this.serviceObject.addControl(this.polylinesControl); + } + polyline_coordinates = []; + for (_i = 0, _len = polyline.length; _i < _len; _i++) { + element = polyline[_i]; + if (element === polyline[0]) { + strokeColor = element.strokeColor || this.polylines_conf.strokeColor; + strokeOpacity = element.strokeOpacity || this.polylines_conf.strokeOpacity; + strokeWeight = element.strokeWeight || this.polylines_conf.strokeWeight; + clickable = element.clickable || this.polylines_conf.clickable; + zIndex = element.zIndex || this.polylines_conf.zIndex; + } + if ((element.lat != null) && (element.lng != null)) { + latlng = new OpenLayers.Geometry.Point(element.lng, element.lat); + polyline_coordinates.push(latlng); + } + } + line_points = new OpenLayers.Geometry.LineString(polyline_coordinates); + line_style = { + strokeColor: strokeColor, + strokeOpacity: strokeOpacity, + strokeWidth: strokeWeight + }; + polyline = new OpenLayers.Feature.Vector(line_points, null, line_style); + polyline.geometry.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); + this.polylinesLayer.addFeatures([polyline]); + return polyline; + }; + + Gmaps4RailsOpenlayers.prototype.updateBoundsWithPolylines = function() {}; + + Gmaps4RailsOpenlayers.prototype.updateBoundsWithPolygons = function() {}; + + Gmaps4RailsOpenlayers.prototype.updateBoundsWithCircles = function() {}; + + Gmaps4RailsOpenlayers.prototype.fitBounds = function() { + return this.serviceObject.zoomToExtent(this.boundsObject, true); + }; + + Gmaps4RailsOpenlayers.prototype.centerMapOnUser = function() { + return this.serviceObject.setCenter(this.userLocation); + }; + + Gmaps4RailsOpenlayers.prototype.extendMapBounds = function() {}; + + Gmaps4RailsOpenlayers.prototype.adaptMapToBounds = function() { + return this.fitBounds(); + }; + + return Gmaps4RailsOpenlayers; + + })(Gmaps4Rails); + +}).call(this); diff --git a/public/stylesheets/gmaps4rails.css b/public/stylesheets/gmaps4rails.css new file mode 100644 index 0000000..7413fb9 --- /dev/null +++ b/public/stylesheets/gmaps4rails.css @@ -0,0 +1,24 @@ +.map_container { + padding: 6px; + border-width: 1px; + border-style: solid; + border-color: #ccc #ccc #999 #ccc; + -webkit-box-shadow: rgba(64, 64, 64, 0.5) 0 2px 5px; + -moz-box-shadow: rgba(64, 64, 64, 0.5) 0 2px 5px; + box-shadow: rgba(64, 64, 64, 0.1) 0 2px 5px; + width: 95%; +} + +.gmaps4rails_map { + width: 100%; + height: 500px; +} + +.bing_map { + position: absolute; + top: 20; + left: 10; + width: 400px; + height: 400px; + border:#555555 2px solid; +} \ No newline at end of file diff --git a/test/controllers/spaces_controller_test.rb b/test/controllers/spaces_controller_test.rb index cfe3d84..2dd5fce 100644 --- a/test/controllers/spaces_controller_test.rb +++ b/test/controllers/spaces_controller_test.rb @@ -18,7 +18,7 @@ class SpacesControllerTest < ActionController::TestCase test "should create space" do assert_difference('Space.count') do - post :create, space: { address: @space.address, description: @space.description, email: @space.email, hours: @space.hours, name: @space.name, phone: @space.phone, type: @space.type, website: @space.website } + post :create, space: { address: @space.address, city: @space.city, state: @space.state, description: @space.description, email: @space.email, hours: @space.hours, name: @space.name, phone: @space.phone, category: @space.category, website: @space.website } end assert_redirected_to space_path(assigns(:space)) @@ -35,7 +35,7 @@ class SpacesControllerTest < ActionController::TestCase end test "should update space" do - patch :update, id: @space, space: { address: @space.address, description: @space.description, email: @space.email, hours: @space.hours, name: @space.name, phone: @space.phone, type: @space.type, website: @space.website } + patch :update, id: @space, space: { address: @space.address, city: @space.city, state: @space.state, description: @space.description, email: @space.email, hours: @space.hours, name: @space.name, phone: @space.phone, category: @space.category, website: @space.website } assert_redirected_to space_path(assigns(:space)) end diff --git a/test/fixtures/spaces.yml b/test/fixtures/spaces.yml index e2525aa..248fdc2 100644 --- a/test/fixtures/spaces.yml +++ b/test/fixtures/spaces.yml @@ -2,8 +2,10 @@ one: name: MyString - type: + category: MyString address: MyString + city: MyString + state: MyString hours: MyString phone: MyString email: MyString @@ -12,8 +14,10 @@ one: two: name: MyString - type: + category: MyString address: MyString + city: MyString + state: MyString hours: MyString phone: MyString email: MyString