gmaps4rails 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,210 @@
1
+ #######################################################################################################
2
+ ############################################## Open Layers ##########################################
3
+ #######################################################################################################
4
+
5
+ #// http://wiki.openstreetmap.org/wiki/OpenLayers
6
+ #// http://openlayers.org/dev/examples
7
+ #//http://docs.openlayers.org/contents.html
8
+
9
+ class @Gmaps4RailsOpenlayers extends Gmaps4Rails
10
+
11
+ constructor: ->
12
+ super
13
+ @map_options = {}
14
+ @mergeWithDefault "map_options"
15
+ @markers_conf = {}
16
+ @mergeWithDefault "markers_conf"
17
+
18
+ @openMarkers = null
19
+ @markersLayer = null
20
+ @markersControl = null
21
+
22
+ #////////////////////////////////////////////////////
23
+ #/////////////// Basic Objects ////////////////////
24
+ #////////////////////////////////////////////////////
25
+
26
+ createPoint: (lat, lng)->
27
+
28
+ createLatLng: (lat, lng)->
29
+ return new OpenLayers.LonLat(lng, lat).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")) # transform from WGS 1984 to Spherical Mercator Projection
30
+
31
+ createAnchor: (offset)->
32
+ return null if offset == null
33
+ return new OpenLayers.Pixel(offset[0], offset[1])
34
+
35
+ createSize: (width, height)->
36
+ return new OpenLayers.Size(width, height)
37
+
38
+ createLatLngBounds: ->
39
+ return new OpenLayers.Bounds()
40
+
41
+ createMap: ->
42
+ #//todo add customization: kind of map and other map options
43
+ map = new OpenLayers.Map(@map_options.id)
44
+ map.addLayer(new OpenLayers.Layer.OSM())
45
+ map.setCenter(@createLatLng(@map_options.center_latitude, @map_options.center_longitude), #// Center of the map
46
+ @map_options.zoom) #// Zoom level
47
+ return map
48
+
49
+ #////////////////////////////////////////////////////
50
+ #////////////////////// Markers /////////////////////
51
+ #////////////////////////////////////////////////////
52
+ #//http://openlayers.org/dev/examples/marker-shadow.html
53
+ createMarker: (args) ->
54
+ style_mark = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default'])
55
+ style_mark.fillOpacity = 1
56
+
57
+ #//creating markers' dedicated layer
58
+ if (@markersLayer == null)
59
+ @markersLayer = new OpenLayers.Layer.Vector("Markers", null)
60
+ @map.addLayer(@markersLayer)
61
+ #//TODO move?
62
+ @markersLayer.events.register("featureselected", @markersLayer, @onFeatureSelect)
63
+ @markersLayer.events.register("featureunselected", @markersLayer, @onFeatureUnselect)
64
+ @markersControl = new OpenLayers.Control.SelectFeature(@markersLayer)
65
+ @map.addControl(@markersControl)
66
+ @markersControl.activate()
67
+ #//showing default pic if none available
68
+ if args.marker_picture == ""
69
+ #style_mark.graphicWidth = 24
70
+ style_mark.graphicHeight = 30
71
+ style_mark.externalGraphic = "http://openlayers.org/dev/img/marker-blue.png"
72
+ #//creating custom pic
73
+ else
74
+ style_mark.graphicWidth = args.marker_width
75
+ style_mark.graphicHeight = args.marker_height
76
+ style_mark.externalGraphic = args.marker_picture
77
+ #//adding anchor if any
78
+ if args.marker_anchor != null
79
+ style_mark.graphicXOffset = args.marker_anchor[0]
80
+ style_mark.graphicYOffset = args.marker_anchor[1]
81
+ #//adding shadow if any
82
+ if args.shadow_picture != ""
83
+ style_mark.backgroundGraphic = args.shadow_picture
84
+ style_mark.backgroundWidth = args.shadow_width
85
+ style_mark.backgroundHeight = args.shadow_height
86
+ #//adding shadow's anchor if any
87
+ if args.shadow_anchor != null
88
+ style_mark.backgroundXOffset = args.shadow_anchor[0]
89
+ style_mark.backgroundYOffset = args.shadow_anchor[1]
90
+
91
+ style_mark.graphicTitle = args.title
92
+ marker = new OpenLayers.Feature.Vector(
93
+ new OpenLayers.Geometry.Point(args.Lng, args.Lat),
94
+ null,
95
+ style_mark)
96
+ #//changing coordinates so that it actually appears on the map!
97
+ marker.geometry.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"))
98
+ #//adding layer to the map
99
+ @markersLayer.addFeatures([marker])
100
+
101
+ return marker
102
+
103
+ #//clear markers
104
+ clearMarkers: ->
105
+ @clearMarkersLayerIfExists()
106
+ @markersLayer = null
107
+ @boundsObject = new OpenLayers.Bounds()
108
+
109
+ clearMarkersLayerIfExists: ->
110
+ this.map.removeLayer(this.markersLayer) if @markersLayer != null and @map.getLayer(@markersLayer.id) != null
111
+
112
+ extendBoundsWithMarkers: ->
113
+ for marker in @markers
114
+ @boundsObject.extend(@createLatLng(marker.lat,marker.lng))
115
+
116
+ #////////////////////////////////////////////////////
117
+ #/////////////////// Clusterer //////////////////////
118
+ #////////////////////////////////////////////////////
119
+ #//too ugly to be considered valid :(
120
+
121
+ createClusterer: (markers_array)->
122
+ options =
123
+ pointRadius: "${radius}"
124
+ fillColor: "#ffcc66"
125
+ fillOpacity: 0.8
126
+ strokeColor: "#cc6633"
127
+ strokeWidth: "${width}"
128
+ strokeOpacity: 0.8
129
+ funcs =
130
+ context:
131
+ width: (feature) ->
132
+ return (feature.cluster) ? 2 : 1
133
+ radius: (feature) ->
134
+ pix = 2
135
+ pix = Math.min(feature.attributes.count, 7) + 2 if feature.cluster
136
+ return pix
137
+
138
+ style = new OpenLayers.Style options, funcs
139
+
140
+ strategy = new OpenLayers.Strategy.Cluster()
141
+
142
+ clusters = new OpenLayers.Layer.Vector "Clusters",
143
+ strategies: [strategy]
144
+ styleMap: new OpenLayers.StyleMap
145
+ "default": style
146
+ "select":
147
+ fillColor: "#8aeeef"
148
+ strokeColor: "#32a8a9"
149
+
150
+ @clearMarkersLayerIfExists()
151
+ @map.addLayer(clusters)
152
+ clusters.addFeatures(markers_array)
153
+ return clusters
154
+
155
+ clusterize: ->
156
+
157
+ if @markers_conf.do_clustering == true
158
+ #//first clear the existing clusterer if any
159
+ if @markerClusterer != null
160
+ @clearClusterer()
161
+ markers_array = new Array
162
+ for marker in markers
163
+ markers_array.push(marker.serviceObject)
164
+ @markerClusterer = @createClusterer markers_array
165
+
166
+ clearClusterer: ->
167
+ @map.removeLayer @markerClusterer
168
+
169
+ #////////////////////////////////////////////////////
170
+ #/////////////////// INFO WINDOW ////////////////////
171
+ #////////////////////////////////////////////////////
172
+
173
+ #// creates infowindows
174
+ createInfoWindow: -> (marker_container) ->
175
+ if marker_container.description?
176
+ marker_container.serviceObject.infoWindow = marker_container.description
177
+
178
+ onPopupClose: (evt) ->
179
+ #// 'this' is the popup.
180
+ @markersControl.unselect @feature
181
+
182
+ onFeatureSelect: (evt) ->
183
+ feature = evt.feature
184
+ popup = new OpenLayers.Popup.FramedCloud("featurePopup",
185
+ feature.geometry.getBounds().getCenterLonLat(),
186
+ new OpenLayers.Size(300,200),
187
+ feature.infoWindow,
188
+ null, true, @onPopupClose)
189
+ feature.popup = popup
190
+ popup.feature = feature
191
+ @map.addPopup popup
192
+
193
+ onFeatureUnselect: (evt) ->
194
+ feature = evt.feature
195
+ if feature.popup
196
+ #//popup.feature = null;
197
+ @map.removePopup feature.popup
198
+ feature.popup.destroy()
199
+ feature.popup = null
200
+
201
+ # #////////////////////////////////////////////////////
202
+ # #/////////////////// Other methods //////////////////
203
+ # #////////////////////////////////////////////////////
204
+
205
+ fitBounds: ->
206
+ @map.zoomToExtent(@boundsObject, true)
207
+
208
+ centerMapOnUser: ->
209
+ @map.setCenter @userLocation
210
+
@@ -6,12 +6,20 @@ module Gmaps4rails
6
6
  desc 'Creates a Gmaps4rails initializer and copies the assets to the public folder.'
7
7
 
8
8
  def copy_locale
9
+ if Rails::VERSION::MINOR >= 1
10
+ copy_file '../../../app/assets/javascripts/gmaps4rails/gmaps4rails.base.js.coffee', 'app/assets/javascripts/gmaps4rails/gmaps4rails.base.js.coffee'
11
+ copy_file '../../../app/assets/javascripts/gmaps4rails/gmaps4rails.googlemaps.js.coffee', 'app/assets/javascripts/gmaps4rails/gmaps4rails.googlemaps.js.coffee'
12
+ copy_file '../../../app/assets/javascripts/gmaps4rails/gmaps4rails.openlayers.js.coffee', 'app/assets/javascripts/gmaps4rails/gmaps4rails.openlayers.js.coffee'
13
+ copy_file '../../../app/assets/javascripts/gmaps4rails/gmaps4rails.bing.js.coffee', 'app/assets/javascripts/gmaps4rails/gmaps4rails.bing.js.coffee'
14
+ copy_file '../../../app/assets/javascripts/gmaps4rails/gmaps4rails.mapquest.js.coffee', 'app/assets/javascripts/gmaps4rails/gmaps4rails.mapquest.js.coffee'
15
+ else
9
16
  #I don't copy manifests, kind of useless
10
- copy_file '../../../public/javascripts/gmaps4rails/gmaps4rails.base.js', 'public/javascripts/gmaps4rails.base.js'
11
- copy_file '../../../public/javascripts/gmaps4rails/gmaps4rails.googlemaps.js', 'public/javascripts/gmaps4rails.googlemaps.js'
12
- copy_file '../../../public/javascripts/gmaps4rails/gmaps4rails.bing.js', 'public/javascripts/gmaps4rails.bing.js'
13
- copy_file '../../../public/javascripts/gmaps4rails/gmaps4rails.openlayers.js', 'public/javascripts/gmaps4rails.openlayers.js'
14
- copy_file '../../../public/javascripts/gmaps4rails/gmaps4rails.mapquest.js', 'public/javascripts/gmaps4rails.mapquest.js'
17
+ copy_file '../../../public/javascripts/gmaps4rails/gmaps4rails.base.js', 'public/javascripts/gmaps4rails.base.js'
18
+ copy_file '../../../public/javascripts/gmaps4rails/gmaps4rails.googlemaps.js', 'public/javascripts/gmaps4rails.googlemaps.js'
19
+ copy_file '../../../public/javascripts/gmaps4rails/gmaps4rails.bing.js', 'public/javascripts/gmaps4rails.bing.js'
20
+ copy_file '../../../public/javascripts/gmaps4rails/gmaps4rails.openlayers.js', 'public/javascripts/gmaps4rails.openlayers.js'
21
+ copy_file '../../../public/javascripts/gmaps4rails/gmaps4rails.mapquest.js', 'public/javascripts/gmaps4rails.mapquest.js'
22
+ end
15
23
  copy_file '../../../public/stylesheets/gmaps4rails.css', 'public/stylesheets/gmaps4rails.css'
16
24
  copy_file '../../../public/images/marker.png', 'public/images/marker.png'
17
25
  end
data/lib/gmaps4rails.rb CHANGED
@@ -13,9 +13,7 @@ if defined?(Rails) && Rails::VERSION::MAJOR == 3
13
13
  end
14
14
  initializer "add asset directories to pipeline" do |app|
15
15
  if Rails::VERSION::MINOR >= 1
16
- app.config.assets.paths << "#{root}/public/javascripts"
17
16
  app.config.assets.paths << "#{root}/public/stylesheets"
18
- app.config.assets.paths << "#{root}/public/images"
19
17
  else
20
18
  app.middleware.use ::ActionDispatch::Static, "#{root}/public"
21
19
  end
@@ -205,14 +205,17 @@ module Gmaps4rails
205
205
 
206
206
  #means we are creating a new map
207
207
  if edit_map_with_id == false
208
-
209
208
  result << "#{map_id} = new #{Gmaps4rails.get_constructor hash[:map_options] }" + ";"
210
209
  result << "function #{Gmaps4rails.js_function_name hash }() {"
210
+
211
211
  #extract map_options
212
212
  unless hash[:map_options].nil?
213
213
  hash[:map_options].each do |option_k, option_v|
214
- if option_k.to_sym == :bounds #particular case
214
+ case option_k.to_sym
215
+ when :bounds #particular case
215
216
  result << "#{map_id}.map_options.#{option_k} = #{option_v};"
217
+ when :class
218
+ when :container_class
216
219
  else
217
220
  result << "#{map_id}.map_options.#{option_k} = #{Gmaps4rails.filter option_v};"
218
221
  end
@@ -251,12 +254,14 @@ module Gmaps4rails
251
254
  result << "#{map_id}.create_#{category}();"
252
255
  end
253
256
  end
257
+ result << "#{map_id}.adjustMapToBounds();"
254
258
  result << "#{map_id}.callback();"
255
259
 
256
260
  if edit_map_with_id == false
257
261
  result << "};"
258
262
  if hash[:last_map].nil? || hash[:last_map] == true
259
- result << "window.onload = Gmaps.loadMaps;"
263
+ result << "window.onload = function() { Gmaps.loadMaps(); };"
264
+ #result << "window.onload = load_map;"
260
265
  end
261
266
  end
262
267
 
@@ -1,537 +1,461 @@
1
- var Gmaps = {};
2
-
3
- Gmaps.loadMaps = function(){
4
- //loop through all variable names.
5
- // there should only be maps inside so it trigger their load function
6
- for (var attrname in Gmaps) {
7
- if (attrname !== "loadMaps"){
8
- window["load_" + attrname]();
1
+ (function() {
2
+ var Gmaps;
3
+ Gmaps = {};
4
+ Gmaps.loadMaps = function() {
5
+ var key, value, _results;
6
+ _results = [];
7
+ for (key in Gmaps) {
8
+ value = Gmaps[key];
9
+ _results.push(key !== "loadMaps" ? window["load_" + key]() : void 0);
9
10
  }
10
- }
11
- }
12
-
13
- function Gmaps4Rails() {
14
-
15
- //map config
16
- this.map = null; // contains the map we're working on
17
- this.visibleInfoWindow = null; //contains the current opened infowindow
18
- this.userLocation = null; //contains user's location if geolocalization was performed and successful
19
-
20
- //empty slots
21
- this.geolocationFailure = function() { return false;} //triggered when geolocation fails. If customized, must be like= function(navigator_handles_geolocation){} where 'navigator_handles_geolocation' is a boolean
22
- this.callback = function() { return false;} //to let user set a custom callback function
23
- this.customClusterer = function() { return null;} //to let user set custom clusterer pictures
24
- this.infobox = function() { return null;} //to let user use custom infoboxes
25
-
26
- this.default_map_options = {
27
- id: 'map',
28
- draggable: true,
29
- detect_location: false, // should the browser attempt to use geolocation detection features of HTML5?
30
- center_on_user: false, // centers map on the location detected through the browser
31
- center_latitude: 0,
32
- center_longitude: 0,
33
- zoom: 7,
34
- maxZoom: null,
35
- minZoom: null,
36
- auto_adjust : true, // adjust the map to the markers if set to true
37
- auto_zoom: true, // zoom given by auto-adjust
38
- bounds: [] // adjust map to these limits. Should be [{"lat": , "lng": }]
11
+ return _results;
39
12
  };
40
-
41
- this.default_markers_conf = {
42
- // Marker config
43
- title: "",
44
- // MarkerImage config
45
- picture : "",
46
- width: 22,
47
- length: 32,
48
- draggable: false, // how to modify: <%= gmaps( "markers" => { "data" => @object.to_gmaps4rails, "options" => { "draggable" => true }}) %>
49
- //clustering config
50
- do_clustering: true, // do clustering if set to true
51
- randomize: false, // Google maps can't display two markers which have the same coordinates. This randomizer enables to prevent this situation from happening.
52
- max_random_distance: 100, // in meters. Each marker coordinate could be altered by this distance in a random direction
53
- list_container: null, // id of the ul that will host links to all markers
54
- offset: 0 //used when adding_markers to an existing map. Because new markers are concated with previous one, offset is here to prevent the existing from being re-created.
55
- };
56
-
57
- //Stored variables
58
- this.markers = []; // contains all markers. A marker contains the following: {"description": , "longitude": , "title":, "latitude":, "picture": "", "width": "", "length": "", "sidebar": "", "serviceObject": google_marker}
59
- this.boundsObject = null; // contains current bounds from markers, polylines etc...
60
- this.polygons = []; // contains raw data, array of arrays (first element could be a hash containing options)
61
- this.polylines = []; // contains raw data, array of arrays (first element could be a hash containing options)
62
- this.circles = []; // contains raw data, array of hash
63
- this.markerClusterer = null; // contains all marker clusterers
64
- this.markerImages = [];
65
-
66
- //initializes the map
67
- this.initialize = function() {
68
-
69
- this.map = this.createMap();
70
-
71
- if (this.map_options.detect_location === true || this.map_options.center_on_user === true) {
72
- this.findUserLocation(this);
73
- }
74
- //resets sidebar if needed
75
- this.resetSidebarContent();
76
- }
77
-
78
- // this.initialize();
79
-
80
- this.findUserLocation = function(map_object) {
81
- if(navigator.geolocation) {
82
- //try to retrieve user's position
83
- navigator.geolocation.getCurrentPosition(function(position) {
84
- //saves the position in the userLocation variable
85
- map_object.userLocation = map_object.createLatLng(position.coords.latitude, position.coords.longitude);
86
- //change map's center to focus on user's geoloc if asked
87
- if(map_object.map_options.center_on_user === true) {
88
- map_object.centerMapOnUser();
89
- }
90
- },
91
- function() {
92
- //failure, but the navigator handles geolocation
93
- map_object.geolocationFailure(true);
94
- });
13
+ window.Gmaps = Gmaps;
14
+ this.Gmaps4Rails = (function() {
15
+ function Gmaps4Rails() {
16
+ this.map = null;
17
+ this.visibleInfoWindow = null;
18
+ this.userLocation = null;
19
+ this.geolocationFailure = function() {
20
+ return false;
21
+ };
22
+ this.callback = function() {
23
+ return false;
24
+ };
25
+ this.customClusterer = function() {
26
+ return false;
27
+ };
28
+ this.infobox = function() {
29
+ return false;
30
+ };
31
+ this.default_map_options = {
32
+ id: 'map',
33
+ draggable: true,
34
+ detect_location: false,
35
+ center_on_user: false,
36
+ center_latitude: 0,
37
+ center_longitude: 0,
38
+ zoom: 7,
39
+ maxZoom: null,
40
+ minZoom: null,
41
+ auto_adjust: true,
42
+ auto_zoom: true,
43
+ bounds: []
44
+ };
45
+ this.default_markers_conf = {
46
+ title: "",
47
+ picture: "",
48
+ width: 22,
49
+ length: 32,
50
+ draggable: false,
51
+ do_clustering: true,
52
+ randomize: false,
53
+ max_random_distance: 100,
54
+ list_container: null,
55
+ offset: 0
56
+ };
57
+ this.markers = [];
58
+ this.boundsObject = null;
59
+ this.polygons = [];
60
+ this.polylines = [];
61
+ this.circles = [];
62
+ this.markerClusterer = null;
63
+ this.markerImages = [];
95
64
  }
96
- else {
97
- //failure but the navigator doesn't handle geolocation
98
- map_object.geolocationFailure(false);
99
- }
100
- }
101
-
102
- ////////////////////////////////////////////////////
103
- //////////////////// DIRECTIONS ////////////////////
104
- ////////////////////////////////////////////////////
105
-
106
- this.create_direction = function(){
107
- var directionsDisplay = new google.maps.DirectionsRenderer();
108
- var directionsService = new google.maps.DirectionsService();
109
-
110
- directionsDisplay.setMap(this.map);
111
- //display panel only if required
112
- if (this.direction_conf.display_panel) { directionsDisplay.setPanel(document.getElementById(this.direction_conf.panel_id)); }
113
- directionsDisplay.setOptions({
114
- suppressMarkers: false,
115
- suppressInfoWindows: false,
116
- suppressPolylines: false
117
- });
118
- var request = {
119
- origin: this.direction_conf.origin,
120
- destination: this.direction_conf.destination,
121
- waypoints: this.direction_conf.waypoints,
122
- optimizeWaypoints: this.direction_conf.optimizeWaypoints,
123
- unitSystem: google.maps.DirectionsUnitSystem[this.direction_conf.unitSystem],
124
- avoidHighways: this.direction_conf.avoidHighways,
125
- avoidTolls: this.direction_conf.avoidTolls,
126
- region: this.direction_conf.region,
127
- travelMode: google.maps.DirectionsTravelMode[this.direction_conf.travelMode],
128
- language: "en"
65
+ Gmaps4Rails.prototype.initialize = function() {
66
+ this.map = this.createMap();
67
+ if (this.map_options.detect_location === true || this.map_options.center_on_user === true) {
68
+ this.findUserLocation(this);
69
+ }
70
+ return this.resetSidebarContent();
129
71
  };
130
- directionsService.route(request, function(response, status) {
131
- if (status == google.maps.DirectionsStatus.OK) {
132
- directionsDisplay.setDirections(response);
72
+ Gmaps4Rails.prototype.findUserLocation = function(map_object) {
73
+ var positionFailure, positionSuccessful;
74
+ if (navigator.geolocation) {
75
+ positionSuccessful = function(position) {
76
+ map_object.userLocation = map_object.createLatLng(position.coords.latitude, position.coords.longitude);
77
+ if (map_object.map_options.center_on_user === true) {
78
+ return map_object.centerMapOnUser();
79
+ }
80
+ };
81
+ positionFailure = function() {
82
+ return map_object.geolocationFailure(true);
83
+ };
84
+ return navigator.geolocation.getCurrentPosition(positionSuccessful, positionFailure);
85
+ } else {
86
+ return map_object.geolocationFailure(false);
133
87
  }
134
- });
135
- }
136
-
137
- ////////////////////////////////////////////////////
138
- ///////////////////// CIRCLES //////////////////////
139
- ////////////////////////////////////////////////////
140
-
141
- //Loops through all circles
142
- //Loops through all circles and draws them
143
- this.create_circles = function() {
144
- for (var i = 0; i < this.circles.length; ++i) {
145
- //by convention, default style configuration could be integrated in the first element
146
- this.create_circle(this.circles[i]);
147
- }
148
- }
149
-
150
- this.create_circle = function(circle) {
151
- if ( i === 0 ) {
152
- if (this.exists(circle.strokeColor )) { this.circles_conf.strokeColor = circle.strokeColor; }
153
- if (this.exists(circle.strokeOpacity)) { this.circles_conf.strokeOpacity = circle.strokeOpacity; }
154
- if (this.exists(circle.strokeWeight )) { this.circles_conf.strokeWeight = circle.strokeWeight; }
155
- if (this.exists(circle.fillColor )) { this.circles_conf.fillColor = circle.fillColor; }
156
- if (this.exists(circle.fillOpacity )) { this.circles_conf.fillOpacity = circle.fillOpacity; }
157
- }
158
- if (this.exists(circle.lat) && this.exists(circle.lng)) {
159
- // always check if a config is given, if not, use defaults
160
- // NOTE: is there a cleaner way to do this? Maybe a hash merge of some sort?
161
- var newCircle = new google.maps.Circle({
162
- center: this.createLatLng(circle.lat, circle.lng),
163
- strokeColor: circle.strokeColor || this.circles_conf.strokeColor,
164
- strokeOpacity: circle.strokeOpacity || this.circles_conf.strokeOpacity,
165
- strokeWeight: circle.strokeWeight || this.circles_conf.strokeWeight,
166
- fillOpacity: circle.fillOpacity || this.circles_conf.fillOpacity,
167
- fillColor: circle.fillColor || this.circles_conf.fillColor,
168
- clickable: circle.clickable || this.circles_conf.clickable,
169
- zIndex: circle.zIndex || this.circles_conf.zIndex,
170
- radius: circle.radius
88
+ };
89
+ Gmaps4Rails.prototype.create_direction = function() {
90
+ var directionsDisplay, directionsService, request;
91
+ directionsDisplay = new google.maps.DirectionsRenderer();
92
+ directionsService = new google.maps.DirectionsService();
93
+ directionsDisplay.setMap(this.map);
94
+ if (this.direction_conf.display_panel) {
95
+ directionsDisplay.setPanel(document.getElementById(this.direction_conf.panel_id));
96
+ }
97
+ directionsDisplay.setOptions({
98
+ suppressMarkers: false,
99
+ suppressInfoWindows: false,
100
+ suppressPolylines: false
171
101
  });
172
- circle.serviceObject = newCircle;
173
- newCircle.setMap(this.map);
174
- }
175
- }
176
-
177
- // clear circles
178
- this.clear_circles = function() {
179
- for (var i = 0; i < this.circles.length; ++i) {
180
- this.clear_circle(this.circles[i]);
181
- }
182
- }
183
-
184
- this.clear_circle = function(circle) {
185
- circle.serviceObject.setMap(null);
186
- }
187
-
188
- this.hide_circles = function() {
189
- for (var i = 0; i < this.circles.length; ++i) {
190
- this.hide_circle(this.circles[i]);
191
- }
192
- }
193
-
194
- this.hide_circle = function(circle) {
195
- circle.serviceObject.setMap(null);
196
- }
197
-
198
- this.show_circles = function() {
199
- for (var i = 0; i < this.circles.length; ++i) {
200
- this.show_circle(this.circles[i]);
201
- }
202
- }
203
-
204
- this.show_circle = function(circle) {
205
- circle.serviceObject.setMap(this.map);
206
- }
207
-
208
- ////////////////////////////////////////////////////
209
- ///////////////////// POLYGONS /////////////////////
210
- ////////////////////////////////////////////////////
211
-
212
- //polygons is an array of arrays. It loops.
213
- this.create_polygons = function(){
214
- for (var i = 0; i < this.polygons.length; ++i) {
215
- this.create_polygon(i);
216
- }
217
- }
218
-
219
- //creates a single polygon, triggered by create_polygons
220
- this.create_polygon = function(i){
221
- var polygon_coordinates = [];
222
- var strokeColor;
223
- var strokeOpacity;
224
- var strokeWeight;
225
- var fillColor;
226
- var fillOpacity;
227
- //Polygon points are in an Array, that's why looping is necessary
228
- for (var j = 0; j < this.polygons[i].length; ++j) {
229
- var latlng = this.createLatLng(this.polygons[i][j].lat, this.polygons[i][j].lng);
230
- polygon_coordinates.push(latlng);
231
- //first element of an Array could contain specific configuration for this particular polygon. If no config given, use default
232
- if (j===0) {
233
- strokeColor = this.polygons[i][j].strokeColor || this.polygons_conf.strokeColor;
234
- strokeOpacity = this.polygons[i][j].strokeOpacity || this.polygons_conf.strokeOpacity;
235
- strokeWeight = this.polygons[i][j].strokeWeight || this.polygons_conf.strokeWeight;
236
- fillColor = this.polygons[i][j].fillColor || this.polygons_conf.fillColor;
237
- fillOpacity = this.polygons[i][j].fillOpacity || this.polygons_conf.fillOpacity;
102
+ request = {
103
+ origin: this.direction_conf.origin,
104
+ destination: this.direction_conf.destination,
105
+ waypoints: this.direction_conf.waypoints,
106
+ optimizeWaypoints: this.direction_conf.optimizeWaypoints,
107
+ unitSystem: google.maps.DirectionsUnitSystem[this.direction_conf.unitSystem],
108
+ avoidHighways: this.direction_conf.avoidHighways,
109
+ avoidTolls: this.direction_conf.avoidTolls,
110
+ region: this.direction_conf.region,
111
+ travelMode: google.maps.DirectionsTravelMode[this.direction_conf.travelMode],
112
+ language: "en"
113
+ };
114
+ return directionsService.route(request, function(response, status) {
115
+ if (status === google.maps.DirectionsStatus.OK) {
116
+ return directionsDisplay.setDirections(response);
117
+ }
118
+ });
119
+ };
120
+ Gmaps4Rails.prototype.create_circles = function() {
121
+ var circle, _i, _len, _ref, _results;
122
+ _ref = this.circles;
123
+ _results = [];
124
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
125
+ circle = _ref[_i];
126
+ _results.push(this.create_circle(circle));
238
127
  }
239
- }
240
-
241
- // Construct the polygon
242
- var new_poly = new google.maps.Polygon({
243
- paths: polygon_coordinates,
244
- strokeColor: strokeColor,
245
- strokeOpacity: strokeOpacity,
246
- strokeWeight: strokeWeight,
247
- fillColor: fillColor,
248
- fillOpacity: fillOpacity,
249
- clickable: false
250
- });
251
- //save polygon in list
252
- this.polygons[i].serviceObject = new_poly;
253
- new_poly.setMap(this.map);
254
- }
255
-
256
- ////////////////////////////////////////////////////
257
- /////////////////// POLYLINES //////////////////////
258
- ////////////////////////////////////////////////////
259
-
260
- //polylines is an array of arrays. It loops.
261
- this.create_polylines = function(){
262
- for (var i = 0; i < this.polylines.length; ++i) {
263
- this.create_polyline(i);
264
- }
265
- }
266
-
267
- //creates a single polyline, triggered by create_polylines
268
- this.create_polyline = function(i) {
269
- var polyline_coordinates = [];
270
- var strokeColor;
271
- var strokeOpacity;
272
- var strokeWeight;
273
-
274
- //2 cases here, either we have a coded array of LatLng or we have an Array of LatLng
275
- for (var j = 0; j < this.polylines[i].length; ++j) {
276
- //if we have a coded array
277
- if (this.exists(this.polylines[i][j].coded_array)) {
278
- var decoded_array = new google.maps.geometry.encoding.decodePath(this.polylines[i][j].coded_array);
279
- //loop through every point in the array
280
- for (var k = 0; k < decoded_array.length; ++k) {
281
- polyline_coordinates.push(decoded_array[k]);
282
- polyline_coordinates.push(decoded_array[k]);
128
+ return _results;
129
+ };
130
+ Gmaps4Rails.prototype.create_circle = function(circle) {
131
+ var newCircle;
132
+ if (circle === this.circles[0]) {
133
+ if (circle.strokeColor != null) {
134
+ this.circles_conf.strokeColor = circle.strokeColor;
283
135
  }
136
+ if (circle.strokeOpacity != null) {
137
+ this.circles_conf.strokeOpacity = circle.strokeOpacity;
138
+ }
139
+ if (circle.strokeWeight != null) {
140
+ this.circles_conf.strokeWeight = circle.strokeWeight;
141
+ }
142
+ if (circle.fillColor != null) {
143
+ this.circles_conf.fillColor = circle.fillColor;
144
+ }
145
+ if (circle.fillOpacity != null) {
146
+ this.circles_conf.fillOpacity = circle.fillOpacity;
147
+ }
148
+ }
149
+ if ((circle.lat != null) && (circle.lng != null)) {
150
+ newCircle = new google.maps.Circle({
151
+ center: this.createLatLng(circle.lat, circle.lng),
152
+ strokeColor: circle.strokeColor || this.circles_conf.strokeColor,
153
+ strokeOpacity: circle.strokeOpacity || this.circles_conf.strokeOpacity,
154
+ strokeWeight: circle.strokeWeight || this.circles_conf.strokeWeight,
155
+ fillOpacity: circle.fillOpacity || this.circles_conf.fillOpacity,
156
+ fillColor: circle.fillColor || this.circles_conf.fillColor,
157
+ clickable: circle.clickable || this.circles_conf.clickable,
158
+ zIndex: circle.zIndex || this.circles_conf.zIndex,
159
+ radius: circle.radius
160
+ });
161
+ circle.serviceObject = newCircle;
162
+ return newCircle.setMap(this.map);
163
+ }
164
+ };
165
+ Gmaps4Rails.prototype.clear_circles = function() {
166
+ var circle, _i, _len, _ref, _results;
167
+ _ref = this.circles;
168
+ _results = [];
169
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
170
+ circle = _ref[_i];
171
+ _results.push(this.clear_circle(circle));
284
172
  }
285
- //or we have an array of latlng
286
- else{
287
- //by convention, a single polyline could be customized in the first array or it uses default values
288
- if (j===0) {
289
- strokeColor = this.polylines[i][0].strokeColor || this.polylines_conf.strokeColor;
290
- strokeOpacity = this.polylines[i][0].strokeOpacity || this.polylines_conf.strokeOpacity;
291
- strokeWeight = this.polylines[i][0].strokeWeight || this.polylines_conf.strokeWeight;
173
+ return _results;
174
+ };
175
+ Gmaps4Rails.prototype.clear_circle = function(circle) {
176
+ return circle.serviceObject.setMap(null);
177
+ };
178
+ Gmaps4Rails.prototype.hide_circles = function() {
179
+ var circle, _i, _len, _ref, _results;
180
+ _ref = this.circles;
181
+ _results = [];
182
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
183
+ circle = _ref[_i];
184
+ _results.push(this.hide_circle(circle));
185
+ }
186
+ return _results;
187
+ };
188
+ Gmaps4Rails.prototype.hide_circle = function(circle) {
189
+ return circle.serviceObject.setMap(null);
190
+ };
191
+ Gmaps4Rails.prototype.show_circles = function() {
192
+ var circle, _i, _len, _ref, _results;
193
+ _ref = this.circles;
194
+ _results = [];
195
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
196
+ circle = _ref[_i];
197
+ _results.push(this.show_circle(this.circle));
198
+ }
199
+ return _results;
200
+ };
201
+ Gmaps4Rails.prototype.show_circle = function(circle) {
202
+ return circle.serviceObject.setMap(this.map);
203
+ };
204
+ Gmaps4Rails.prototype.create_polygons = function() {
205
+ var polygon, _i, _len, _ref, _results;
206
+ _ref = this.polygons;
207
+ _results = [];
208
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
209
+ polygon = _ref[_i];
210
+ _results.push(this.create_polygon(polygon));
211
+ }
212
+ return _results;
213
+ };
214
+ Gmaps4Rails.prototype.create_polygon = function(polygon) {
215
+ var fillColor, fillOpacity, latlng, new_poly, point, polygon_coordinates, strokeColor, strokeOpacity, strokeWeight, _i, _len;
216
+ polygon_coordinates = [];
217
+ for (_i = 0, _len = polygon.length; _i < _len; _i++) {
218
+ point = polygon[_i];
219
+ latlng = this.createLatLng(point.lat, point.lng);
220
+ polygon_coordinates.push(latlng);
221
+ if (point === polygon[0]) {
222
+ strokeColor = this.polygons[i][j].strokeColor || this.polygons_conf.strokeColor;
223
+ strokeOpacity = this.polygons[i][j].strokeOpacity || this.polygons_conf.strokeOpacity;
224
+ strokeWeight = this.polygons[i][j].strokeWeight || this.polygons_conf.strokeWeight;
225
+ fillColor = this.polygons[i][j].fillColor || this.polygons_conf.fillColor;
226
+ fillOpacity = this.polygons[i][j].fillOpacity || this.polygons_conf.fillOpacity;
292
227
  }
293
- //add latlng if positions provided
294
- if (this.exists(this.polylines[i][j].lat) && this.exists(this.polylines[i][j].lng)) {
295
- var latlng = this.createLatLng(this.polylines[i][j].lat, this.polylines[i][j].lng);
296
- polyline_coordinates.push(latlng);
228
+ }
229
+ new_poly = new google.maps.Polygon({
230
+ paths: polygon_coordinates,
231
+ strokeColor: strokeColor,
232
+ strokeOpacity: strokeOpacity,
233
+ strokeWeight: strokeWeight,
234
+ fillColor: fillColor,
235
+ fillOpacity: fillOpacity,
236
+ clickable: false
237
+ });
238
+ polygon.serviceObject = new_poly;
239
+ return new_poly.setMap(this.map);
240
+ };
241
+ Gmaps4Rails.prototype.create_polylines = function() {
242
+ var polyline, _i, _len, _ref, _results;
243
+ _ref = this.polylines;
244
+ _results = [];
245
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
246
+ polyline = _ref[_i];
247
+ _results.push(this.create_polyline(polyline));
248
+ }
249
+ return _results;
250
+ };
251
+ Gmaps4Rails.prototype.create_polyline = function(polyline) {
252
+ var decoded_array, element, latlng, new_poly, point, polyline_coordinates, strokeColor, strokeOpacity, strokeWeight, _i, _j, _len, _len2, _ref;
253
+ polyline_coordinates = [];
254
+ for (_i = 0, _len = polyline.length; _i < _len; _i++) {
255
+ element = polyline[_i];
256
+ if (element.coded_array != null) {
257
+ decoded_array = new google.maps.geometry.encoding.decodePath(element.coded_array);
258
+ _ref = decoded_array.length;
259
+ for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
260
+ point = _ref[_j];
261
+ polyline_coordinates.push(point);
262
+ polyline_coordinates.push(point);
263
+ }
264
+ } else {
265
+ if (element === polyline[0]) {
266
+ strokeColor = element.strokeColor || this.polylines_conf.strokeColor;
267
+ strokeOpacity = element.strokeOpacity || this.polylines_conf.strokeOpacity;
268
+ strokeWeight = element.strokeWeight || this.polylines_conf.strokeWeight;
269
+ }
270
+ if ((element.lat != null) && (element.lng != null)) {
271
+ latlng = this.createLatLng(element.lat, element.lng);
272
+ polyline_coordinates.push(latlng);
273
+ }
297
274
  }
298
275
  }
299
- }
300
- // Construct the polyline
301
- var new_poly = new google.maps.Polyline({
302
- path: polyline_coordinates,
303
- strokeColor: strokeColor,
304
- strokeOpacity: strokeOpacity,
305
- strokeWeight: strokeWeight,
306
- clickable: false
307
- });
308
- //save polyline
309
- this.polylines[i].serviceObject = new_poly;
310
- new_poly.setMap(this.map);
311
- }
312
-
313
- ////////////////////////////////////////////////////
314
- ///////////////////// MARKERS //////////////////////
315
- //////////////////tests coded///////////////////////
316
-
317
- //creates, clusterizes and adjusts map
318
- this.create_markers = function() {
319
- this.createServiceMarkersFromMarkers();
320
- this.clusterize();
321
- this.adjustMapToBounds();
322
- }
323
-
324
- //create google.maps Markers from data provided by user
325
- this.createServiceMarkersFromMarkers = function() {
326
- for (var i = this.markers_conf.offset; i < this.markers.length; ++i) {
327
- //extract options, test if value passed or use default
328
- var Lat = this.markers[i].lat;
329
- var Lng = this.markers[i].lng;
330
-
331
- //alter coordinates if randomize is true
332
- if ( this.markers_conf.randomize) {
333
- var LatLng = this.randomize(Lat, Lng);
334
- //retrieve coordinates from the æarray
335
- Lat = LatLng[0]; Lng = LatLng[1];
276
+ new_poly = new google.maps.Polyline({
277
+ path: polyline_coordinates,
278
+ strokeColor: strokeColor,
279
+ strokeOpacity: strokeOpacity,
280
+ strokeWeight: strokeWeight,
281
+ clickable: false
282
+ });
283
+ polyline.serviceObject = new_poly;
284
+ return new_poly.setMap(this.map);
285
+ };
286
+ Gmaps4Rails.prototype.create_markers = function() {
287
+ this.createServiceMarkersFromMarkers();
288
+ return this.clusterize();
289
+ };
290
+ Gmaps4Rails.prototype.createServiceMarkersFromMarkers = function() {
291
+ var Lat, LatLng, Lng, index, _ref, _ref2;
292
+ for (index = _ref = this.markers_conf.offset, _ref2 = this.markers.length - 1; _ref <= _ref2 ? index <= _ref2 : index >= _ref2; _ref <= _ref2 ? index++ : index--) {
293
+ Lat = this.markers[index].lat;
294
+ Lng = this.markers[index].lng;
295
+ if (this.markers_conf.randomize) {
296
+ LatLng = this.randomize(Lat, Lng);
297
+ Lat = LatLng[0];
298
+ Lng = LatLng[1];
336
299
  }
337
- //save object
338
- this.markers[i].serviceObject = this.createMarker({
339
- "marker_picture": this.exists(this.markers[i].picture) ? this.markers[i].picture : this.markers_conf.picture,
340
- "marker_width": this.exists(this.markers[i].width) ? this.markers[i].width : this.markers_conf.width,
341
- "marker_height": this.exists(this.markers[i].height) ? this.markers[i].height : this.markers_conf.length,
342
- "marker_title": this.exists(this.markers[i].title) ? this.markers[i].title : null,
343
- "marker_anchor": this.exists(this.markers[i].marker_anchor) ? this.markers[i].marker_anchor : null,
344
- "shadow_anchor": this.exists(this.markers[i].shadow_anchor) ? this.markers[i].shadow_anchor : null,
345
- "shadow_picture": this.exists(this.markers[i].shadow_picture) ? this.markers[i].shadow_picture : null,
346
- "shadow_width": this.exists(this.markers[i].shadow_width) ? this.markers[i].shadow_width : null,
347
- "shadow_height": this.exists(this.markers[i].shadow_height) ? this.markers[i].shadow_height : null,
348
- "marker_draggable": this.exists(this.markers[i].draggable) ? this.markers[i].draggable : this.markers_conf.draggable,
349
- "rich_marker": this.exists(this.markers[i].rich_marker) ? this.markers[i].rich_marker : null,
350
- "Lat": Lat,
351
- "Lng": Lng,
352
- "index": i
300
+ this.markers[index].serviceObject = this.createMarker({
301
+ "marker_picture": this.markers[index].picture ? this.markers[index].picture : this.markers_conf.picture,
302
+ "marker_width": this.markers[index].width ? this.markers[index].width : this.markers_conf.width,
303
+ "marker_height": this.markers[index].height ? this.markers[index].height : this.markers_conf.length,
304
+ "marker_title": this.markers[index].title ? this.markers[index].title : null,
305
+ "marker_anchor": this.markers[index].marker_anchor ? this.markers[index].marker_anchor : null,
306
+ "shadow_anchor": this.markers[index].shadow_anchor ? this.markers[index].shadow_anchor : null,
307
+ "shadow_picture": this.markers[index].shadow_picture ? this.markers[index].shadow_picture : null,
308
+ "shadow_width": this.markers[index].shadow_width ? this.markers[index].shadow_width : null,
309
+ "shadow_height": this.markers[index].shadow_height ? this.markers[index].shadow_height : null,
310
+ "marker_draggable": this.markers[index].draggable ? this.markers[index].draggable : this.markers_conf.draggable,
311
+ "rich_marker": this.markers[index].rich_marker ? this.markers[index].rich_marker : null,
312
+ "Lat": Lat,
313
+ "Lng": Lng,
314
+ "index": index
353
315
  });
354
- //add infowindowstuff if enabled
355
- this.createInfoWindow(this.markers[i]);
356
- //create sidebar if enabled
357
- this.createSidebar(this.markers[i]);
358
- }
359
- this.markers_conf.offset = this.markers.length;
360
- }
361
-
362
-
363
- // creates Image Anchor Position or return null if nothing passed
364
- this.createImageAnchorPosition = function(anchorLocation) {
365
- if (anchorLocation === null)
366
- { return null; }
367
- else
368
- { return this.createPoint(anchorLocation[0], anchorLocation[1]); }
369
- }
370
-
371
- // replace old markers with new markers on an existing map
372
- this.replaceMarkers = function(new_markers){
373
- this.clearMarkers();
374
- //reset previous markers
375
- this.markers = new Array;
376
- //reset current bounds
377
- this.boundsObject = this.createLatLngBounds();
378
- //reset sidebar content if exists
379
- this.resetSidebarContent();
380
- //add new markers
381
- this.addMarkers(new_markers);
382
- }
383
-
384
- //add new markers to on an existing map
385
- this.addMarkers = function(new_markers){
386
- //update the list of markers to take into account
387
- this.markers = this.markers.concat(new_markers);
388
- //put markers on the map
389
- this.create_markers();
390
- }
391
-
392
- ////////////////////////////////////////////////////
393
- ///////////////////// SIDEBAR //////////////////////
394
- ////////////////////////////////////////////////////
395
-
396
- //creates sidebar
397
- this.createSidebar = function(marker_container){
398
- if (this.markers_conf.list_container)
399
- {
400
- var ul = document.getElementById(this.markers_conf.list_container);
401
- var li = document.createElement('li');
402
- var aSel = document.createElement('a');
403
- aSel.href = 'javascript:void(0);';
404
- var html = this.exists(marker_container.sidebar) ? marker_container.sidebar : "Marker";
405
- aSel.innerHTML = html;
406
- aSel.onclick = this.sidebar_element_handler(marker_container.serviceObject, 'click');
407
- li.appendChild(aSel);
408
- ul.appendChild(li);
409
- }
410
- }
411
-
412
- //moves map to marker clicked + open infowindow
413
- this.sidebar_element_handler = function(marker, eventType) {
414
- return function() {
415
- this.map.panTo(marker.position);
416
- google.maps.event.trigger(marker, eventType);
316
+ this.createInfoWindow(this.markers[index]);
317
+ this.createSidebar(this.markers[index]);
318
+ }
319
+ return this.markers_conf.offset = this.markers.length;
417
320
  };
418
- }
419
-
420
- this.resetSidebarContent = function(){
421
- if (this.markers_conf.list_container !== null ){
422
- var ul = document.getElementById(this.markers_conf.list_container);
423
- ul.innerHTML = "";
424
- }
425
- }
426
-
427
- ////////////////////////////////////////////////////
428
- ////////////////// MISCELLANEOUS ///////////////////
429
- ////////////////////////////////////////////////////
430
-
431
- //to make the map fit the different LatLng points
432
- this.adjustMapToBounds = function() {
433
-
434
- //FIRST_STEP: retrieve all bounds
435
- //create the bounds object only if necessary
436
- if (this.map_options.auto_adjust || this.map_options.bounds !== null) {
321
+ Gmaps4Rails.prototype.createImageAnchorPosition = function(anchorLocation) {
322
+ if (anchorLocation === null) {
323
+ return null;
324
+ } else {
325
+ return this.createPoint(anchorLocation[0], anchorLocation[1]);
326
+ }
327
+ };
328
+ Gmaps4Rails.prototype.replaceMarkers = function(new_markers) {
329
+ this.clearMarkers();
330
+ this.markers = new Array;
437
331
  this.boundsObject = this.createLatLngBounds();
438
- }
439
-
440
- //if autodjust is true, must get bounds from markers polylines etc...
441
- if (this.map_options.auto_adjust) {
442
- //from markers
443
- this.extendBoundsWithMarkers();
444
- //from polylines:
445
- for (var i = 0; i < this.polylines.length; ++i) {
446
- var polyline_points = this.polylines[i].serviceObject.latLngs.getArray()[0].getArray();
447
- for (var j = 0; j < polyline_points.length; ++j) {
448
- this.boundsObject.extend(polyline_points[j]);
332
+ this.resetSidebarContent();
333
+ return this.addMarkers(new_markers);
334
+ };
335
+ Gmaps4Rails.prototype.addMarkers = function(new_markers) {
336
+ this.markers = this.markers.concat(new_markers);
337
+ this.create_markers();
338
+ return this.adjustMapToBounds();
339
+ };
340
+ Gmaps4Rails.prototype.createSidebar = function(marker_container) {
341
+ var aSel, currentMap, html, li, ul;
342
+ if (this.markers_conf.list_container) {
343
+ ul = document.getElementById(this.markers_conf.list_container);
344
+ li = document.createElement('li');
345
+ aSel = document.createElement('a');
346
+ aSel.href = 'javascript:void(0);';
347
+ html = marker_container.sidebar != null ? marker_container.sidebar : "Marker";
348
+ aSel.innerHTML = html;
349
+ currentMap = this;
350
+ aSel.onclick = this.sidebar_element_handler(currentMap, marker_container.serviceObject, 'click');
351
+ li.appendChild(aSel);
352
+ return ul.appendChild(li);
353
+ }
354
+ };
355
+ Gmaps4Rails.prototype.sidebar_element_handler = function(currentMap, marker, eventType) {
356
+ return function() {
357
+ currentMap.map.panTo(marker.position);
358
+ return google.maps.event.trigger(marker, eventType);
359
+ };
360
+ };
361
+ Gmaps4Rails.prototype.resetSidebarContent = function() {
362
+ var ul;
363
+ if (this.markers_conf.list_container !== null) {
364
+ ul = document.getElementById(this.markers_conf.list_container);
365
+ return ul.innerHTML = "";
366
+ }
367
+ };
368
+ Gmaps4Rails.prototype.adjustMapToBounds = function() {
369
+ var bound, circle, map_center, point, polygon, polygon_points, polyline, polyline_points, _i, _j, _k, _l, _len, _len2, _len3, _len4, _len5, _len6, _m, _n, _ref, _ref2, _ref3, _ref4;
370
+ if (this.map_options.auto_adjust || this.map_options.bounds !== null) {
371
+ this.boundsObject = this.createLatLngBounds();
372
+ }
373
+ if (this.map_options.auto_adjust) {
374
+ this.extendBoundsWithMarkers();
375
+ _ref = this.polylines;
376
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
377
+ polyline = _ref[_i];
378
+ polyline_points = polyline.serviceObject.latLngs.getArray()[0].getArray();
379
+ for (_j = 0, _len2 = polyline.length; _j < _len2; _j++) {
380
+ point = polyline[_j];
381
+ this.boundsObject.extend(point);
382
+ }
383
+ }
384
+ _ref2 = this.polygons;
385
+ for (_k = 0, _len3 = _ref2.length; _k < _len3; _k++) {
386
+ polygon = _ref2[_k];
387
+ polygon_points = polygon.serviceObject.latLngs.getArray()[0].getArray();
388
+ for (_l = 0, _len4 = polygon.length; _l < _len4; _l++) {
389
+ point = polygon[_l];
390
+ this.boundsObject.extend(point);
391
+ }
392
+ }
393
+ _ref3 = this.circles;
394
+ for (_m = 0, _len5 = _ref3.length; _m < _len5; _m++) {
395
+ circle = _ref3[_m];
396
+ this.boundsObject.extend(circle.serviceObject.getBounds().getNorthEast());
397
+ this.boundsObject.extend(circle.serviceObject.getBounds().getSouthWest());
449
398
  }
450
399
  }
451
- //from polygons:
452
- for (var i = 0; i < this.polygons.length; ++i) {
453
- var polygon_points = this.polygons[i].serviceObject.latLngs.getArray()[0].getArray();
454
- for (var j = 0; j < polygon_points.length; ++j) {
455
- this.boundsObject.extend(polygon_points[j]);
400
+ _ref4 = this.map_options.bounds;
401
+ for (_n = 0, _len6 = _ref4.length; _n < _len6; _n++) {
402
+ bound = _ref4[_n];
403
+ bound = this.createLatLng(bound.lat, bound.lng);
404
+ this.boundsObject.extend(bound);
405
+ }
406
+ if (this.map_options.auto_adjust || this.map_options.bounds.length > 0) {
407
+ if (!this.map_options.auto_zoom) {
408
+ map_center = this.boundsObject.getCenter();
409
+ this.map_options.center_latitude = map_center.lat();
410
+ this.map_options.center_longitude = map_center.lng();
411
+ return this.map.setCenter(map_center);
412
+ } else {
413
+ return this.fitBounds();
456
414
  }
457
415
  }
458
- //from circles
459
- for (var i = 0; i < this.circles.length; ++i) {
460
- this.boundsObject.extend(this.circles[i].serviceObject.getBounds().getNorthEast());
461
- this.boundsObject.extend(this.circles[i].serviceObject.getBounds().getSouthWest());
462
- }
463
- }
464
- //in every case, I've to take into account the bounds set up by the user
465
- for (var i = 0; i < this.map_options.bounds.length; ++i) {
466
- //create points from bounds provided
467
- //TODO:only works with google maps
468
- var bound = this.createLatLng(this.map_options.bounds[i].lat, this.map_options.bounds[i].lng);
469
- this.boundsObject.extend(bound);
470
- }
471
-
472
- //SECOND_STEP: ajust the map to the bounds
473
- if (this.map_options.auto_adjust || this.map_options.bounds.length > 0) {
474
-
475
- //if autozoom is false, take user info into account
476
- if(!this.map_options.auto_zoom) {
477
- var map_center = this.boundsObject.getCenter();
478
- this.map_options.center_latitude = map_center.lat();
479
- this.map_options.center_longitude = map_center.lng();
480
- this.map.setCenter(map_center);
416
+ };
417
+ Gmaps4Rails.prototype.create_kml = function() {
418
+ var kml, _i, _len, _ref, _results;
419
+ _ref = this.kml;
420
+ _results = [];
421
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
422
+ kml = _ref[_i];
423
+ _results.push(kml.serviceObject = this.createKmlLayer(kml));
481
424
  }
482
- else {
483
- this.fitBounds();
425
+ return _results;
426
+ };
427
+ Gmaps4Rails.prototype.exists = function(var_name) {
428
+ return var_name !== "" && typeof var_name !== "undefined";
429
+ };
430
+ Gmaps4Rails.prototype.randomize = function(Lat0, Lng0) {
431
+ var Lat, Lng, dx, dy;
432
+ dx = this.markers_conf.max_random_distance * this.random();
433
+ dy = this.markers_conf.max_random_distance * this.random();
434
+ Lat = parseFloat(Lat0) + (180 / Math.PI) * (dy / 6378137);
435
+ Lng = parseFloat(Lng0) + (90 / Math.PI) * (dx / 6378137) / Math.cos(Lat0);
436
+ return [Lat, Lng];
437
+ };
438
+ Gmaps4Rails.prototype.mergeObjectWithDefault = function(object1, object2) {
439
+ var copy_object1, key, value;
440
+ copy_object1 = object1;
441
+ for (key in object2) {
442
+ value = object2[key];
443
+ if (copy_object1[key] == null) {
444
+ copy_object1[key] = value;
445
+ }
484
446
  }
485
- }
486
- }
487
-
488
- ////////////////////////////////////////////////////
489
- ///////////////// KML //////////////////
490
- ////////////////////////////////////////////////////
491
-
492
- this.create_kml = function(){
493
- for (var i = 0; i < this.kml.length; ++i) {
494
- this.kml[i].serviceObject = this.createKmlLayer(this.kml[i]);
495
- }
496
- }
497
-
498
-
499
- ////////////////////////////////////////////////////
500
- ///////////////// Basic functions //////////////////
501
- ///////////////////tests coded//////////////////////
502
-
503
- //basic function to check existence of a variable
504
- this.exists = function(var_name) {
505
- return (var_name !== "" && typeof var_name !== "undefined");
506
- }
507
-
508
- //randomize
509
- this.randomize = function(Lat0, Lng0) {
510
- //distance in meters between 0 and max_random_distance (positive or negative)
511
- var dx = this.markers_conf.max_random_distance * this.random();
512
- var dy = this.markers_conf.max_random_distance * this.random();
513
- var Lat = parseFloat(Lat0) + (180/Math.PI)*(dy/6378137);
514
- var Lng = parseFloat(Lng0) + ( 90/Math.PI)*(dx/6378137)/Math.cos(Lat0);
515
- return [Lat, Lng];
516
- }
517
-
518
- this.mergeObjectWithDefault = function(object1, object2){
519
- var copy_object1 = object1;
520
- for (var attrname in object2) {
521
- if ( typeof(copy_object1[attrname]) === "undefined") {
522
- copy_object1[attrname] = object2[attrname];
523
- }
524
- }
525
- return copy_object1;
526
- }
527
-
528
- this.mergeWithDefault = function(objectName){
529
- var default_object = this["default_" + objectName];
530
- var object = this[objectName];
531
- this[objectName] = this.mergeObjectWithDefault(object, default_object);
532
- return true;
533
- }
534
- //gives a value between -1 and 1
535
- this.random = function() { return(Math.random() * 2 -1); }
536
-
537
- }
447
+ return copy_object1;
448
+ };
449
+ Gmaps4Rails.prototype.mergeWithDefault = function(objectName) {
450
+ var default_object, object;
451
+ default_object = this["default_" + objectName];
452
+ object = this[objectName];
453
+ this[objectName] = this.mergeObjectWithDefault(object, default_object);
454
+ return true;
455
+ };
456
+ Gmaps4Rails.prototype.random = function() {
457
+ return Math.random() * 2 - 1;
458
+ };
459
+ return Gmaps4Rails;
460
+ })();
461
+ }).call(this);