gmaps4rails 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,7 +1,7 @@
1
1
  == Gmaps4rails
2
2
 
3
- Gmaps4rails is developped to simply create a map with model instances (say Users).
4
- It's based on Rails 3 Engines.
3
+ Gmaps4rails is developped to simply create a Google Map (Gmaps) with model instances (say Users).
4
+ It's based on Ruby on Rails 3 Engines.
5
5
  It works properly but I still have something to correct: making it work without resorting to the serve_static_assets to true in production.
6
6
 
7
7
  == Installation
@@ -10,87 +10,96 @@ It works properly but I still have something to correct: making it work without
10
10
 
11
11
  == Requirements
12
12
  - jQuery (used for ajax json)
13
- - <%= yield :head %> in your header
13
+ - <%= yield :head %> (in your header)
14
+ - <%= yield :scripts %> (in your footer)
14
15
  - config.serve_static_assets = true (in your production.rb)
15
16
 
16
17
  == Quickstart
17
- In your model
18
- acts_as_gmappable
19
-
20
- def gmaps4rails_address
21
- self.address #describe how to retrieve the address from your model
22
- end
18
+ Say you have a User model.
19
+ In your model, add:
23
20
 
24
- def gmaps4rails_picture
25
- self.picture #describe how to retrieve the picture from your model
26
- end
21
+ acts_as_gmappable
27
22
 
28
- def gmaps4rails_description
29
- self.name #could be html it's rendered when user clicks on the markers on the map
30
- end
23
+ def gmaps4rails_address
24
+ self.address #describe how to retrieve the address from your model
25
+ end
31
26
 
32
27
  Create a migration and add the following fields to your table:
33
- t.string "gmaps4rails_latitude"
34
- t.string "gmaps4rails_longitude"
35
- t.boolean "gmaps"
36
28
 
37
- In your view
38
- <%= render :partial => 'gmaps4rails/gmaps4rails', :locals => { :model => "User"} %>
29
+ add_column :users, :latitude, :float
30
+ add_column :users, :longitude, :float
31
+ add_column :users, :gmaps, :boolean
32
+
33
+ In your view:
39
34
 
35
+ <%= gmaps4rails_map("User") %>
40
36
 
41
37
  == Options
42
38
 
43
- === Setting Map Center
44
- <%= render :partial => 'gmaps4rails/gmaps4rails', :locals => { :model => "User", :ref_latitude => 0 , :ref_longitude => 0 } %>
39
+ === Add an info window
40
+ To add an info window (visible when you click a marker), add this method in your model:
41
+
42
+ def gmaps4rails_infowindow
43
+ # add here whatever html content you desire, it will be displayed when users clicks on the marker
44
+ end
45
+
46
+ === List of Options
47
+ You can customize the following:
48
+
49
+ processing: 'rails_model', //or 'json'
50
+ marker_picture : 'http://www.mbs.edu/i/gmap_marker_default.gif,
51
+ model_scope : null,
52
+ marker_width : 22,
53
+ marker_length : 32,
54
+ map_center_latitude : 0,
55
+ map_center_longitude : 0,
56
+ map_zoom : 1,
57
+ do_clustering: true,
58
+ clusterer_gridSize: 50,
59
+ clusterer_maxZoom: 10
60
+
61
+ === How to set your own options
62
+ Change the call in your view this way and add an additional hash containing the options you want:
63
+ <%= gmaps4rails_map("User", {"map_center_longitude" => "90", "do_clustering" => false}) %>
45
64
 
46
65
  === Scopes (displays a dropdown list enabling you to filter the map content)
47
- In your model:
48
- # Whatever scope you want
49
- scope :low_height, lambda {|height|
50
- where("users.height <= ?", height)
51
- }
52
- scope :high_height, lambda {|height|
53
- where("users.height >= ?", height)
54
- }
55
-
56
- # method to fill in the dropdown list: provide scope argument, variable to pass to it, and text you want to display
57
- def self.gmaps4rails_filters
58
- [
59
- {"filter" => "All", "options" => "", "display" => "All"},
60
- {"filter" => "low", "options" => "130", "display" => "People Under 130cm"},
61
- {"filter" => "low", "options" => "100", "display" => "People Under 100cm"},
62
- {"filter" => "high", "options" =>"190", "display" => "People Above 190cm"},
63
- {"filter" => "high", "options" =>"200", "display" => "People Above 200cm"}
64
- ]
65
- end
66
-
67
- # method to filter your requests given the parameters you provided above
68
- def self.gmaps4rails_filter(filter, options)
69
- case filter
70
- when 'low' then self.low_height(options)
71
- when 'high' then self.high_height(options)
72
- else self.all
73
- end
74
- end
66
+ Note that you can pass a model_scope in the options to filter the data you want to be displayed.
67
+ So as above, change the call in your view:
75
68
 
76
- In your view:
77
- <%=raw gmaps4rails_filters_display("User") %>
69
+ <%= gmaps4rails_map("User", {"model_scope" => "my_scope"}) %>
78
70
 
79
- === Styling your Marker
80
- In your model, add the following:
71
+ In this case, you must add in your model:
81
72
 
82
- def self.gmaps4rails_markers_pic
83
- { "picture" => "http://www.ci.burlington.vt.us/planning/images/google_maps_marker.png",
84
- "width" => 22,
85
- "length" => 32 }
73
+ def self.gmaps4rails_trusted_scopes
74
+ ["my_scope", "another_trusted_scope"]
86
75
  end
87
76
 
88
- == Todo
89
- Still much more customization to provide, some ideas:
90
- - direct integration of scopes to decide what you want to display precisely
91
- - distance filter
77
+ Why? because you shall never trust the params, so it's a way to keep control.
78
+
79
+ === Create from your own json
80
+ If you want to use your own json to initialize the map, create your json with the following attributes
81
+
82
+ @json = '[
83
+ {"description": "", "longitude": "", "latitude": "", "picture": "", "width": "", "height": ""},
84
+ {"longitude": "", "latitude": "" }
85
+ ]'
86
+
87
+ Only `latitude` and `longitude` are mandatory. But you can customize any single marker.
88
+
89
+ Then in your view:
90
+
91
+ <%= gmaps4rails_map(@json, { "processing" => 'json' }) %>
92
+
93
+ Or with options as well:
94
+
95
+ <%= gmaps4rails_map("json", @json, {"processing" => 'json', "map_center_longitude" => "90"}) %>
96
+
97
+
98
+ == Todo?
99
+
100
+ Maybe use Geokit to handle the geocoding stuff.
92
101
 
93
102
  Feel free ton contact me, you have your say. I hope I'll have time enough to improve it.
94
- == Copyright
95
103
 
96
- Copyright (c) 2010 apneadiving. See LICENSE for details.
104
+ == Copyright
105
+ Copyright (c) 2011 apneadiving. See LICENSE for details.
@@ -3,15 +3,16 @@ module Gmaps4rails
3
3
  unloadable
4
4
 
5
5
  def index
6
- if request.xhr?
7
- @model = params["model"]
8
- @filter = params["filter"]
9
- @options = params["options"]
10
- if @model.constantize.is_gmappable? == true
11
- @objects = @model.constantize.gmaps4rails_filter(@filter, @options)
12
- end
13
- end
14
- end
15
-
6
+ @model = params["model"]
7
+ @scope = params["scope"]
8
+ @model = @model.constantize
9
+
10
+ if @scope && !@scope.empty? && @model.gmaps4rails_trusted_scopes.include?(@scope)
11
+ @objects = eval("#{@model}.#{@scope}") # Cannot use send with lambda scope
12
+ # because the arguments have to be separated
13
+ else
14
+ @objects = @model.all
15
+ end
16
+ end
16
17
  end
17
18
  end
@@ -1,7 +1,11 @@
1
1
  <% content_for :head do %>
2
2
  <%= stylesheet_link_tag 'gmaps4rails' %>
3
- <script src="http://www.google.com/jsapi"></script>
3
+ <% end %>
4
+
5
+ <% content_for :scripts do %>
6
+ <script src="http://www.google.com/jsapi"></script>
4
7
  <script type="text/javascript" src='http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/src/markerclusterer.js'></script>
8
+ <%=javascript_include_tag 'gmaps4rails' %>
5
9
  <script type="text/javascript" charset="utf-8">
6
10
  var styles = [{
7
11
  url: 'http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/images/people35.png',
@@ -11,25 +15,14 @@ var styles = [{
11
15
  opt_textColor: '#ff00ff',
12
16
  opt_textSize: 10
13
17
  }];
14
- var gmaps4rails_ref_lat = <%= defined?(ref_latitude) ? ref_latitude : 'null' %>;
15
- var gmaps4rails_ref_long = <%= defined?(ref_longitude) ? ref_longitude : 'null' %>;
16
- var gmaps4rails_marker_picture = '<%= model.constantize.gmaps4rails_markers_pic["picture"] %>';
17
- var gmaps4rails_marker_width = <%= model.constantize.gmaps4rails_markers_pic["width"] %>;
18
- var gmaps4rails_marker_length = <%= model.constantize.gmaps4rails_markers_pic["length"] %>;
19
- var gmaps4rails_model = '<%= model %>';
20
- var gmaps4rails_map_center_lat = <%= model.constantize.gmaps4rails_map_options["latitude"] %>;
21
- var gmaps4rails_map_center_long = <%= model.constantize.gmaps4rails_map_options["longitude"] %>;
22
- var gmaps4rails_map_zoom = <%= model.constantize.gmaps4rails_map_options["zoom"] %>;
23
- var gmaps4rails_base_url = '<%= url_for gmaps_path %>';
18
+ <% options.each do |key, value| %>
19
+ Gmaps4Rails.<%= key %> = <%=raw value.is_a?(String) ? "'#{value}'" : value %>;
20
+ <% end %>
21
+ var builder = <%=raw options[""] == "json" ? builder : "'#{builder}'" %>;
22
+ Gmaps4Rails.initialize(builder);
24
23
  </script>
25
- <%=javascript_include_tag 'gmaps4rails' %>
26
24
  <% end %>
27
- <div id="map-container">
28
- <div id="gmaps4rails_map"></div>
29
- </div>
30
- <% if defined?(ref_longitude) && defined?(ref_latitude)%>
31
- <div id="inline-actions">
32
- <input id="gmaps4rails_user_distance" >
33
- <input type="submit" value="Filter" class="item" onclick="filter_distance()">
34
- </div>
35
- <% end %>
25
+
26
+ <div id="map-container">
27
+ <div id="gmaps4rails_map"></div>
28
+ </div>
@@ -1,27 +1,19 @@
1
- { "count": <%= @objects.size.to_s %>,
2
- "markers": [
3
- <% if @objects.first.respond_to?('gmaps4rails_info')
4
- @objects.each do |object|
5
- desc = object.gmaps4rails_info.nil? ? "No description provided" : CGI::escapeHTML(object.gmaps4rails_info)
6
- lat = object.gmaps4rails_latitude.nil? ? "" : object.gmaps4rails_latitude
7
- long = object.gmaps4rails_longitude.nil? ? "" : object.gmaps4rails_longitude
8
- %>
9
- {"description": "<%=raw desc %>", "longitude": <%= long %>, "latitude": <%= lat %>, "marker_object": "null"}
10
- <% end
11
-
12
- else
13
- @objects.each do |object|
14
- stat = object.status.nil? ? "" : CGI::escapeHTML(object.status)
15
- desc = object.picture.nil? ? "No description provided" : "<img width='40' heigth='40' src='" + object.gmaps4rails_picture + "'>" + stat
16
-
17
- lat = object.gmaps4rails_latitude.nil? ? "" : object.gmaps4rails_latitude
18
- long = object.gmaps4rails_longitude.nil? ? "" : object.gmaps4rails_longitude
19
-
20
- if (!(lat == "" || long == "")) %>
21
- {"description": "<%=raw desc %>", "longitude": <%= long %>, "latitude": <%= lat %>, "marker_object": "null"} <%= ',' unless object == @objects.last %>
22
- <%
23
- end
24
- end
25
- end
1
+ [
2
+ <%
3
+ @objects.each do |object|
4
+ # lat = object.gmaps4rails_latitude.nil? ? "" : object.gmaps4rails_latitude
5
+ # long = object.gmaps4rails_longitude.nil? ? "" : object.gmaps4rails_longitude
6
+ if (!(object.gmaps4rails_latitude == "" || object.gmaps4rails_longitude == ""))
26
7
  %>
27
- ]}
8
+ {"description": "<%=raw object.gmaps4rails_infowindow %>",
9
+ "longitude": "<%= object.gmaps4rails_longitude %>",
10
+ "latitude": "<%= object.gmaps4rails_latitude %>",
11
+ "picture": "<%= object.gmaps4rails_marker_picture["picture"] %>",
12
+ "width": "<%= object.gmaps4rails_marker_picture["width"] %>",
13
+ "height": "<%= object.gmaps4rails_marker_picture["height"] %>"
14
+ } <%= ',' unless object == @objects.last %>
15
+ <%
16
+ end
17
+ end
18
+ %>
19
+ ]
data/config/routes.rb CHANGED
@@ -1,13 +1,5 @@
1
- #Rails.application.routes.draw do |map|
2
1
  Rails.application.routes.draw do
3
2
 
4
- #mount_at = Gmaps4rails::Engine.config.mount_at
5
-
6
- #match mount_at => 'gmaps4rails/gmaps#index'
7
-
8
3
  resources :gmaps, :only => [ :index ], :controller => "gmaps4rails/gmaps"
9
- #,
10
- # :path_prefix => mount_at
11
- #:name_prefix => "gmaps4rails_"
12
4
 
13
5
  end
@@ -15,57 +15,60 @@ module Gmaps4rails
15
15
  module Config
16
16
  def acts_as_gmappable options = {}
17
17
  before_save :get_coordinates
18
-
19
- def self.is_gmappable?
20
- true
21
- end
22
-
23
- def self.gmaps4rails_markers_pic
24
- { "picture" =>'http://inmotionchiro.com/gmap_plugin/imgs/markers/marker.png',
25
- "width" => 22,
26
- "length" => 32 }
27
- end
28
-
29
- def gmaps4rails_map_options
30
- { "zoom" =>1, "latitude" => 0, "longitude" => 0}
31
- end
32
-
33
18
  include Gmaps4rails::ActsAsGmappable::Base::InstanceMethods
34
19
  end
35
20
  end
36
21
 
37
22
  module InstanceMethods
38
-
23
+
24
+ def gmaps4rails_infowindow
25
+ self.gmaps4rails_picture.blank? ? "" : "<img width='40' heigth='40' src='" + self.gmaps4rails_picture + "'>"
26
+ end
27
+
28
+ def gmaps4rails_picture
29
+ ""
30
+ end
31
+
32
+ def gmaps4rails_marker_picture
33
+ {
34
+ "picture" => "",
35
+ "width" => "",
36
+ "height" => ""
37
+ }
38
+ end
39
+
40
+ def self.gmaps4rails_trusted_scopes
41
+ []
42
+ end
43
+
39
44
  def get_coordinates
40
- if self.gmaps4rails_address.nil? || self.gmaps4rails_address.empty?
41
- self.gmaps = false
42
- else
43
- geocoder = "http://maps.googleapis.com/maps/api/geocode/json?address="
44
- output = "&sensor=false"
45
- #send request to the google api to get the lat/lng
46
- request = geocoder + self.gmaps4rails_address + output
47
- url = URI.escape(request)
48
- resp = Net::HTTP.get_response(URI.parse(url))
49
- #parse result if result received properly
50
- if resp.inspect.include?('HTTPOK 200 OK')
51
- #parse the json
52
- parse = Crack::JSON.parse(resp.body)
53
- #check if google went well
54
- if parse["status"] == "OK"
55
- #TODO maybe handle case when there are many results
56
- #TODO store the country name and maybe other details?
57
- latitude = parse["results"].first["geometry"]["location"]["lat"]
58
- longitude = parse["results"].first["geometry"]["location"]["lng"]
59
- self.gmaps4rails_latitude = latitude
60
- self.gmaps4rails_longitude = longitude
61
- #saves a boolean to remind the status
62
- self.gmaps = true
63
- end
64
- else
65
- self.gmaps = false
45
+ if self.gmaps4rails_address.nil? || self.gmaps4rails_address.empty?
46
+ self.gmaps = false
47
+ else
48
+ geocoder = "http://maps.googleapis.com/maps/api/geocode/json?address="
49
+ output = "&sensor=false"
50
+ #send request to the google api to get the lat/lng
51
+ request = geocoder + self.gmaps4rails_address + output
52
+ url = URI.escape(request)
53
+ resp = Net::HTTP.get_response(URI.parse(url))
54
+ #parse result if result received properly
55
+ if resp.inspect.include?('HTTPOK 200 OK')
56
+ #parse the json
57
+ parse = Crack::JSON.parse(resp.body)
58
+ #check if google went well
59
+ if parse["status"] == "OK"
60
+ #TODO maybe handle case when there are many results
61
+ #TODO store the country name and maybe other details?
62
+ self.gmaps4rails_latitude = parse["results"].first["geometry"]["location"]["lat"]
63
+ self.gmaps4rails_longitude = parse["results"].first["geometry"]["location"]["lng"]
64
+ #saves a boolean to remind the status
65
+ self.gmaps = true
66
66
  end
67
+ else
68
+ self.gmaps = false
67
69
  end
68
- return true
70
+ end
71
+ return true
69
72
  end
70
73
  end # InstanceMethods
71
74
  end
@@ -1,15 +1,7 @@
1
1
  module ApplicationHelper
2
-
3
- def gmaps4rails_filters_display(model)
4
- if model.constantize.respond_to?("gmaps4rails_filters")
5
- display = '<form name="gmaps4rails_form">'
6
- display += '<select name="gmaps4rails_list" onChange="gmaps4rails_resfreshmap()">'
7
- model.constantize.gmaps4rails_filters.each do |filter_hash|
8
- display += '<option value="' + filter_hash["filter"] + '+' + filter_hash["options"] +'">' + filter_hash["display"]
9
- end
10
- display += '</select></form>'
11
- end
12
- return display
2
+
3
+ def gmaps4rails_map(builder, options = {})
4
+ render :partial => 'gmaps4rails/gmaps4rails', :locals => { :builder => builder, :options => options }
13
5
  end
14
6
 
15
7
  end
data/lib/gmaps4rails.rb CHANGED
@@ -1,29 +1,14 @@
1
1
  if defined?(Rails) && Rails::VERSION::MAJOR == 3
2
2
  module Gmaps4rails
3
- # require 'rails/all'
4
3
  require "rails"
5
4
  require "action_controller"
6
- # require 'action_controller'
7
5
  require 'application_helper'
8
6
  require 'acts_as_gmappable/base'
9
7
 
10
8
  class Engine < Rails::Engine
11
- # config.autoload_paths += %W(#{config.root}/lib)
12
- #
13
- # initializer "static assets" do |app|
14
- # app.middleware.use ::ActionDispatch::Static, "#{root}/public%s"
15
- # end
16
- #
17
- # def self.activate
18
- # Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*.rb")) do |c|
19
- # Rails.env.production? ? require(c) : load(c)
20
- # end
21
- # end
22
- #
23
- # config.to_prepare &method(:activate).to_proc
24
- # initializer 'gmaps.helper' do |app|
25
- # ActionView::Base.send :include, Gmaps4rails::GmapsHelper
26
- # end
9
+ initializer "static assets" do |app|
10
+ app.middleware.use ::ActionDispatch::Static, "#{root}/public"
11
+ end
27
12
  end
28
13
  end
29
14
  end
Binary file
@@ -1,139 +1,122 @@
1
1
  google.load('maps', '3', { other_params: 'sensor=false' });
2
2
 
3
- google.setOnLoadCallback(initialize);
4
- var gmaps4rails_map = null;
5
- var gmaps4rails_data = null;
6
- var markerClusterer = null;
7
- var gmaps4rails_infowindow = null;
8
- var gmaps_circle = null ;
9
-
10
- //put markers on the map + launch the clusterer
11
- function setMarkers(locations) {
12
-
13
- //variable used for Marker Clusterer
14
- var markers = [];
3
+ var Gmaps4Rails = {
4
+ processing: 'rails_model',
5
+ map: null,
6
+ marker_picture : 'http://inmotionchiro.com/gmap_plugin/imgs/markers/marker.png',
7
+ marker_width : 22,
8
+ marker_length : 32,
9
+ map_center_latitude : 0,
10
+ map_center_longitude : 0,
11
+ map_zoom : 1,
12
+ base_url : '/gmaps',
13
+ rails_model : null,
14
+ model_scope : null,
15
+ ref_latitude : null,
16
+ ref_longitude : null,
17
+ info_window : null,
18
+ locations : null,
19
+ markerClusterer: null,
20
+ do_clustering: true,
21
+ clusterer_gridSize: 50,
22
+ clusterer_maxZoom: 10,
23
+ //Triggers the creation of the map.
24
+ //Two options:
25
+ // 1- processing == "rails_model" && builder = model_name
26
+ // 2- processing == "json" && builder = json in format: [{"description": , "longitude": , "latitude":, "picture": "", "width": "", "length": ""}]
27
+ initialize: function(builder) {
28
+ this.reset_map();
29
+ //infowindow closes when user clicks on the map
30
+ google.maps.event.addListener(this.map, 'click', function()
31
+ { if (this.info_window != null) {this.info_window.close();}
32
+ });
33
+ if (this.processing == "rails_model")
34
+ {
35
+ this.rails_model = builder;
36
+ this.create_from_model();
37
+ }
38
+ else if (this.processing == "json")
39
+ {
40
+ this.locations = builder;
41
+ this.setup_Markers();
42
+ }
43
+ },
15
44
 
16
- if (markerClusterer) {
17
- markerClusterer.clearMarkers();
18
- }
19
- // Add markers to the map
20
- for (var i = 0; i < locations.markers.length; ++i) {
21
- // Marker sizes are expressed as a Size of X,Y
22
- var image = new google.maps.MarkerImage(gmaps4rails_marker_picture,
23
- new google.maps.Size(gmaps4rails_marker_width, gmaps4rails_marker_length));
24
- var myLatLng = new google.maps.LatLng(locations.markers[i].latitude, locations.markers[i].longitude);
25
- var ThisMarker = new google.maps.Marker({position: myLatLng, map: gmaps4rails_map, icon: image}); //TODO Offer title customization title: "title"
26
- //save object for later use, basically, to get back the text to display when clicking it
27
- locations.markers[i].marker_object = ThisMarker;
28
- //save the marker again in a list for the clusterer
29
- markers.push(ThisMarker);
30
-
31
- //add click listener
32
- google.maps.event.addListener(locations.markers[i].marker_object, 'click', function() { if (gmaps4rails_infowindow!=null) {gmaps4rails_infowindow.close();}; getInfoWindow(this);});
45
+ //resets the map, removes all markers
46
+ reset_map: function(){
47
+ this.map = new google.maps.Map(document.getElementById('gmaps4rails_map'), {
48
+ zoom: this.map_zoom,
49
+ center: new google.maps.LatLng(this.map_center_latitude, this.map_center_longitude),
50
+ mapTypeId: google.maps.MapTypeId.ROADMAP
51
+ });
52
+ },
33
53
 
34
- }
35
-
36
- markerClusterer = new MarkerClusterer(gmaps4rails_map, markers, {
37
- maxZoom: 10,
38
- gridSize: 50,
39
- //styles: styles TODO: offer clusterer customization
40
- });
41
- }
54
+ //creates the necessary query to get the model + scope, and sends json to setup_Markers
55
+ create_from_model: function (filter_value) {
56
+ request = this.base_url + '?model=' + this.rails_model;
42
57
 
43
- //get infowindow content when listener calls it
44
- function getInfoWindow(which)
45
- {
46
- for ( var m = 0; m < gmaps4rails_data.markers.length; ++m )
47
- {
48
- var markerInfo = gmaps4rails_data.markers[m].marker_object;
49
- if ( markerInfo == which )
50
- {
51
- gmaps4rails_infowindow = new google.maps.InfoWindow({content: gmaps4rails_data.markers[m].description });
52
- gmaps4rails_infowindow.open( gmaps4rails_map, which );
53
- return;
54
- }
55
- }
56
- }
58
+ if(this.model_scope != null)
59
+ { request += '&scope=' + this.model_scope; }
57
60
 
58
- //initializes the map
59
- function create_map(filter_value) {
60
- request = gmaps4rails_base_url + '?model=' + gmaps4rails_model;
61
+ jQuery.getJSON(request,function(data){
62
+ Gmaps4Rails.locations = data;
63
+ Gmaps4Rails.setup_Markers();
64
+ }
65
+ );
66
+ },
61
67
 
62
- if (!(filter_value == null))
63
- {
64
- split_filter_value = filter_value.split('+');
65
- if (!(split_filter_value[0] == null))
66
- {
67
- request += '&filter=' + split_filter_value[0];
68
- }
69
- if (!(split_filter_value[1] == null))
70
- {
71
- request += '&options=' + split_filter_value[1];
72
- }
73
- }
74
- jQuery.getJSON(request,
75
- function(data){
76
- gmaps4rails_data = data;
77
- setMarkers(gmaps4rails_data);
78
- });
79
- }
80
-
81
- function initialize() {
82
- gmaps4rails_reset();
83
- //infowindow closes when user clicks on the map
84
- google.maps.event.addListener(gmaps4rails_map, 'click', function()
85
- { if (gmaps4rails_infowindow != null) {gmaps4rails_infowindow.close();}
86
- });
87
- create_map();
88
- }
89
-
90
- function gmaps4rails_resfreshmap() {
91
- gmaps4rails_reset();
92
- var index = document.gmaps4rails_form.gmaps4rails_list.selectedIndex;
93
- var filter_value = document.gmaps4rails_form.gmaps4rails_list.options[index].value;
94
- create_map(filter_value);
95
- }
96
-
97
- function gmaps4rails_reset(){
98
- gmaps4rails_map = new google.maps.Map(document.getElementById('gmaps4rails_map'), {
99
- zoom: gmaps4rails_map_zoom,
100
- center: new google.maps.LatLng(gmaps4rails_map_center_lat, gmaps4rails_map_center_long),
101
- mapTypeId: google.maps.MapTypeId.ROADMAP
102
- });
103
- }
104
-
105
- // max_distance in km
106
- function filter_distance() {
107
- var max_distance = parseInt(document.getElementById('gmaps4rails_user_distance').value, 10);
108
- if (!(max_distance>0 || max_distance<0))
109
- {
110
- alert('Please set the max distance');
111
- }
112
- else{
113
- if (gmaps_circle!=null) { gmaps_circle.setMap(null);}
114
- var myCenter = new google.maps.LatLng(gmaps4rails_ref_lat, gmaps4rails_ref_long);
115
- var filtered_markers = {"markers":[]};
116
-
68
+ //Creates Marker from the locations passed + markerClusterer
69
+ setup_Markers: function () {
70
+ //variable used for Marker Clusterer
71
+ var markers = [];
117
72
 
118
- for (var i = 0; i < gmaps4rails_data.markers.length; ++i) {
119
- if (get_distance(gmaps4rails_ref_long, gmaps4rails_data.markers[i].longitude, gmaps4rails_ref_lat, gmaps4rails_data.markers[i].latitude) < max_distance)
120
- { filtered_markers.markers.push(gmaps4rails_data.markers[i]);}
121
- setMarkers(filtered_markers);
73
+ //resests Clusterer if needed
74
+ if (this.markerClusterer) {
75
+ this.markerClusterer.clearMarkers();
122
76
  }
123
- //radius is in meters
124
- gmaps_circle = new google.maps.Circle({radius: max_distance*1000, center: myCenter, fillColor:"#00FF00", strokeColor: "#00EE00"});
125
- gmaps_circle.setMap(gmaps4rails_map);
77
+ // Add markers to the map
78
+ for (var i = 0; i < this.locations.length; ++i) {
79
+
80
+ //test if value passed or use default
81
+ var marker_picture = this.locations[i].picture != "" && typeof this.locations[i].picture !== "undefined" ? this.locations[i].picture : this.marker_picture;
82
+ var marker_width = this.locations[i].width != "" && typeof this.locations[i].width !== "undefined" ? this.locations[i].width : this.marker_width;
83
+ var marker_height = this.locations[i].height != "" && typeof this.locations[i].height !== "undefined" ? this.locations[i].height : this.marker_length;
84
+ // Marker sizes are expressed as a Size of X,Y
85
+ var image = new google.maps.MarkerImage(marker_picture,
86
+ new google.maps.Size(marker_width, marker_height)
87
+ );
88
+ var myLatLng = new google.maps.LatLng(this.locations[i].latitude, this.locations[i].longitude);
89
+ var ThisMarker = new google.maps.Marker({position: myLatLng, map: this.map, icon: image}); //TODO Offer title customization title: "title"
90
+ //save object for later use, basically, to get back the text to display when clicking it
91
+ this.locations[i].marker_object = ThisMarker;
92
+ //save the marker again in a list for the clusterer
93
+ markers.push(ThisMarker);
94
+ //add click listener
95
+ google.maps.event.addListener(Gmaps4Rails.locations[i].marker_object, 'click', function() { if (Gmaps4Rails.info_window!=null) {Gmaps4Rails.info_window.close();}; Gmaps4Rails.getInfoWindow(this);});
96
+ }
97
+ if (this.do_clustering == true)
98
+ {
99
+ this.markerClusterer = new MarkerClusterer(this.map, markers, {
100
+ maxZoom: this.clusterer_maxZoom,
101
+ gridSize: this.clusterer_gridSize,
102
+ //styles: styles TODO: offer clusterer customization
103
+ });
104
+ }
105
+ },
106
+
107
+ //get info_window content when listener calls it
108
+ getInfoWindow: function(which)
109
+ {
110
+ for ( var m = 0; m < this.locations.length; ++m )
111
+ {
112
+ var markerInfo = this.locations[m].marker_object;
113
+ if ( markerInfo == which && this.locations[m].description != "")
114
+ {
115
+ this.info_window = new google.maps.InfoWindow({content: this.locations[m].description });
116
+ this.info_window.open( this.map, which );
117
+ return;
118
+ }
119
+ }
126
120
  }
127
- }
128
-
129
- function get_distance(long1, long2, lat1, lat2) {
130
- var theta = long1 - long2;
131
- var dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
132
- dist = Math.acos(dist);
133
- dist = rad2deg(dist);
134
- var km = dist * 60 * 1.853;
135
- return km;
136
- }
137
-
138
- function deg2rad(value) { return value*Math.PI/180;}
139
- function rad2deg(value) { return value*180/Math.PI;}
121
+
122
+ };
@@ -0,0 +1,144 @@
1
+ google.load('maps', '3', { other_params: 'sensor=false' });
2
+
3
+ google.setOnLoadCallback(initialize);
4
+ var gmaps4rails_map = null;
5
+ var gmaps4rails_data = null;
6
+ var markerClusterer = null;
7
+ var gmaps4rails_infowindow = null;
8
+ var gmaps_circle = null ;
9
+
10
+ //put markers on the map + launch the clusterer
11
+ function setMarkers(locations) {
12
+
13
+ //variable used for Marker Clusterer
14
+ var markers = [];
15
+
16
+ if (markerClusterer) {
17
+ markerClusterer.clearMarkers();
18
+ }
19
+ // Add markers to the map
20
+ for (var i = 0; i < locations.markers.length; ++i) {
21
+ // Marker sizes are expressed as a Size of X,Y
22
+ var image = new google.maps.MarkerImage(gmaps4rails_marker_picture,
23
+ new google.maps.Size(gmaps4rails_marker_width, gmaps4rails_marker_length)
24
+ );
25
+ var myLatLng = new google.maps.LatLng(locations.markers[i].latitude, locations.markers[i].longitude);
26
+ var ThisMarker = new google.maps.Marker({position: myLatLng, map: gmaps4rails_map, icon: image}); //TODO Offer title customization title: "title"
27
+ //save object for later use, basically, to get back the text to display when clicking it
28
+ locations.markers[i].marker_object = ThisMarker;
29
+ //save the marker again in a list for the clusterer
30
+ markers.push(ThisMarker);
31
+
32
+ //add click listener
33
+ google.maps.event.addListener(locations.markers[i].marker_object, 'click', function() { if (gmaps4rails_infowindow!=null) {gmaps4rails_infowindow.close();}; getInfoWindow(this);});
34
+
35
+ }
36
+
37
+ markerClusterer = new MarkerClusterer(gmaps4rails_map, markers, {
38
+ maxZoom: 10,
39
+ gridSize: 50,
40
+ //styles: styles TODO: offer clusterer customization
41
+ });
42
+ }
43
+
44
+ //get infowindow content when listener calls it
45
+ function getInfoWindow(which)
46
+ {
47
+ for ( var m = 0; m < gmaps4rails_data.markers.length; ++m )
48
+ {
49
+ var markerInfo = gmaps4rails_data.markers[m].marker_object;
50
+ if ( markerInfo == which )
51
+ {
52
+ gmaps4rails_infowindow = new google.maps.InfoWindow({content: gmaps4rails_data.markers[m].description });
53
+ gmaps4rails_infowindow.open( gmaps4rails_map, which );
54
+ return;
55
+ }
56
+ }
57
+ }
58
+
59
+ //initializes the map
60
+ function create_map(filter_value) {
61
+ request = gmaps4rails_base_url + '?model=' + gmaps4rails_model;
62
+
63
+ // if(gmaps4rails_scope != null)
64
+ // request += '&scope=' + gmaps4rails_scope;
65
+
66
+ if (!(filter_value == null))
67
+ {
68
+ split_filter_value = filter_value.split('+');
69
+ if (!(split_filter_value[0] == null))
70
+ {
71
+ request += '&filter=' + split_filter_value[0];
72
+ }
73
+ if (!(split_filter_value[1] == null))
74
+ {
75
+ request += '&options=' + split_filter_value[1];
76
+ }
77
+ }
78
+ jQuery.getJSON(request,
79
+ function(data){
80
+ gmaps4rails_data = data;
81
+ setMarkers(gmaps4rails_data);
82
+ });
83
+ }
84
+
85
+ function initialize() {
86
+ gmaps4rails_reset();
87
+ //infowindow closes when user clicks on the map
88
+ google.maps.event.addListener(gmaps4rails_map, 'click', function()
89
+ { if (gmaps4rails_infowindow != null) {gmaps4rails_infowindow.close();}
90
+ });
91
+ create_map();
92
+ }
93
+
94
+ function gmaps4rails_resfreshmap() {
95
+ gmaps4rails_reset();
96
+ var index = document.gmaps4rails_form.gmaps4rails_list.selectedIndex;
97
+ var filter_value = document.gmaps4rails_form.gmaps4rails_list.options[index].value;
98
+ create_map(filter_value);
99
+ }
100
+
101
+ function gmaps4rails_reset(){
102
+ gmaps4rails_map = new google.maps.Map(document.getElementById('gmaps4rails_map'), {
103
+ zoom: gmaps4rails_map_zoom,
104
+ center: new google.maps.LatLng(gmaps4rails_map_center_lat, gmaps4rails_map_center_long),
105
+ mapTypeId: google.maps.MapTypeId.ROADMAP
106
+ });
107
+ }
108
+
109
+ // max_distance in km
110
+ function filter_distance() {
111
+ var max_distance = parseInt(document.getElementById('gmaps4rails_user_distance').value, 10);
112
+ if (!(max_distance>0 || max_distance<0))
113
+ {
114
+ alert('Please set the max distance');
115
+ }
116
+ else{
117
+ if (gmaps_circle!=null) { gmaps_circle.setMap(null);}
118
+ var myCenter = new google.maps.LatLng(gmaps4rails_ref_lat, gmaps4rails_ref_long);
119
+ var filtered_markers = {"markers":[]};
120
+
121
+
122
+ for (var i = 0; i < gmaps4rails_data.markers.length; ++i) {
123
+ if (get_distance(gmaps4rails_ref_long, gmaps4rails_data.markers[i].longitude, gmaps4rails_ref_lat, gmaps4rails_data.markers[i].latitude) < max_distance)
124
+ { filtered_markers.markers.push(gmaps4rails_data.markers[i]);}
125
+ setMarkers(filtered_markers);
126
+ }
127
+ //radius is in meters
128
+ gmaps_circle = new google.maps.Circle({radius: max_distance*1000, center: myCenter, fillColor:"#00FF00", strokeColor: "#00EE00"});
129
+ gmaps_circle.setMap(gmaps4rails_map);
130
+ }
131
+ }
132
+
133
+ function get_distance(long1, long2, lat1, lat2)
134
+ {
135
+ var theta = long1 - long2;
136
+ var dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
137
+ dist = Math.acos(dist);
138
+ dist = rad2deg(dist);
139
+ var km = dist * 60 * 1.853;
140
+ return km;
141
+ }
142
+
143
+ function deg2rad(value) { return value*Math.PI/180;}
144
+ function rad2deg(value) { return value*180/Math.PI;}
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gmaps4rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease: false
4
+ hash: 23
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Benjamin Roth
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-24 00:00:00 +01:00
18
+ date: 2011-02-13 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: "0"
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
- description: Enables easy display of items (taken from a model) on a Google Map. Uses Javascript API V3.
35
+ description: Enables easy display of items (taken from a Rails 3 model) on a Google Map. Uses Javascript API V3.
36
36
  email: apnea.diving.deep@gmail.com
37
37
  executables: []
38
38
 
@@ -57,6 +57,7 @@ files:
57
57
  - lib/tasks/install.rake
58
58
  - public/images/marker.png
59
59
  - public/javascripts/gmaps4rails.js
60
+ - public/javascripts/old.js
60
61
  - public/stylesheets/gmaps4rails.css
61
62
  - README.rdoc
62
63
  - test/test_helper.rb
@@ -66,8 +67,8 @@ homepage: http://github.com/apneadiving/Gmaps4rails
66
67
  licenses: []
67
68
 
68
69
  post_install_message:
69
- rdoc_options:
70
- - --charset=UTF-8
70
+ rdoc_options: []
71
+
71
72
  require_paths:
72
73
  - lib
73
74
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -91,10 +92,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
92
  requirements: []
92
93
 
93
94
  rubyforge_project:
94
- rubygems_version: 1.3.7
95
+ rubygems_version: 1.5.0
95
96
  signing_key:
96
97
  specification_version: 3
97
- summary: Enables easy display of items (taken from a model) on a Google Map. Uses Javascript API V3.
98
+ summary: Enables easy display of items (taken from a Rails 3 model) on a Google Map. Uses Javascript API V3.
98
99
  test_files:
99
100
  - test/test_helper.rb
100
101
  - test/unit/gmaps4rails_widget_test.rb