gmaps4rails 0.9.1 → 0.10.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,263 @@
1
+ //Map settings
2
+ Gmaps4Rails.map_options.disableDefaultUI = false;
3
+ Gmaps4Rails.map_options.disableDoubleClickZoom = false;
4
+ Gmaps4Rails.map_options.type = "ROADMAP"; // HYBRID, ROADMAP, SATELLITE, TERRAIN
5
+
6
+ //markers + info styling
7
+ Gmaps4Rails.markers_conf.clusterer_gridSize = 50;
8
+ Gmaps4Rails.markers_conf.clusterer_maxZoom = 5;
9
+ Gmaps4Rails.markers_conf.custom_cluster_pictures = null;
10
+ Gmaps4Rails.markers_conf.custom_infowindow_class = null;
11
+
12
+ //Polygon Styling
13
+ Gmaps4Rails.polygons_conf = { // default style for polygons
14
+ strokeColor: "#FFAA00",
15
+ strokeOpacity: 0.8,
16
+ strokeWeight: 2,
17
+ fillColor: "#000000",
18
+ fillOpacity: 0.35
19
+ };
20
+
21
+ //Polyline Styling
22
+ Gmaps4Rails.polylines_conf = { //default style for polylines
23
+ strokeColor: "#FF0000",
24
+ strokeOpacity: 1,
25
+ strokeWeight: 2
26
+ };
27
+
28
+ //Circle Styling
29
+ Gmaps4Rails.circles_conf = { //default style for circles
30
+ fillColor: "#00AAFF",
31
+ fillOpacity: 0.35,
32
+ strokeColor: "#FFAA00",
33
+ strokeOpacity: 0.8,
34
+ strokeWeight: 2,
35
+ clickable: false,
36
+ zIndex: null
37
+ };
38
+
39
+ //Direction Settings
40
+ Gmaps4Rails.direction_conf = {
41
+ panel_id: null,
42
+ display_panel: false,
43
+ origin: null,
44
+ destination: null,
45
+ waypoints: [], //[{location: "toulouse,fr", stopover: true}, {location: "Clermont-Ferrand, fr", stopover: true}]
46
+ optimizeWaypoints: false,
47
+ unitSystem: "METRIC", //IMPERIAL
48
+ avoidHighways: false,
49
+ avoidTolls: false,
50
+ region: null,
51
+ travelMode: "DRIVING" //WALKING, BICYCLING
52
+ };
53
+
54
+ ////////////////////////////////////////////////////
55
+ /////////////// Basic Objects //////////////
56
+ ////////////////////////////////////////////////////
57
+
58
+ Gmaps4Rails.createPoint = function(lat, lng){
59
+ return new google.maps.Point(lat, lng);
60
+ };
61
+
62
+ Gmaps4Rails.createLatLng = function(lat, lng){
63
+ return new google.maps.LatLng(lat, lng);
64
+ };
65
+
66
+ Gmaps4Rails.createLatLngBounds = function(){
67
+ return new google.maps.LatLngBounds();
68
+ };
69
+
70
+ Gmaps4Rails.createMap = function(){
71
+ return new google.maps.Map(document.getElementById(Gmaps4Rails.map_options.id), {
72
+ maxZoom: Gmaps4Rails.map_options.maxZoom,
73
+ minZoom: Gmaps4Rails.map_options.minZoom,
74
+ zoom: Gmaps4Rails.map_options.zoom,
75
+ center: Gmaps4Rails.createLatLng(this.map_options.center_latitude, this.map_options.center_longitude),
76
+ mapTypeId: google.maps.MapTypeId[this.map_options.type],
77
+ mapTypeControl: Gmaps4Rails.map_options.mapTypeControl,
78
+ disableDefaultUI: Gmaps4Rails.map_options.disableDefaultUI,
79
+ disableDoubleClickZoom: Gmaps4Rails.map_options.disableDoubleClickZoom,
80
+ draggable: Gmaps4Rails.map_options.draggable
81
+ });
82
+ };
83
+
84
+ Gmaps4Rails.createMarkerImage = function(markerPicture, markerSize, origin, anchor, scaledSize) {
85
+ return new google.maps.MarkerImage(markerPicture, markerSize, origin, anchor, scaledSize);
86
+ };
87
+
88
+
89
+ Gmaps4Rails.createSize = function(width, height){
90
+ return new google.maps.Size(width, height);
91
+ };
92
+
93
+ ////////////////////////////////////////////////////
94
+ ////////////////////// Markers /////////////////////
95
+ ////////////////////////////////////////////////////
96
+
97
+ Gmaps4Rails.createMarker = function(args){
98
+ var markerLatLng = Gmaps4Rails.createLatLng(args.Lat, args.Lng);
99
+
100
+ // Marker sizes are expressed as a Size of X,Y
101
+ if (args.marker_picture === "" ) {
102
+ return new google.maps.Marker({position: markerLatLng, map: Gmaps4Rails.map, title: args.marker_title, draggable: args.marker_draggable});
103
+ } else {
104
+ // calculate MarkerImage anchor location
105
+ var imageAnchorPosition = this.createImageAnchorPosition(args.marker_anchor);
106
+ var shadowAnchorPosition = this.createImageAnchorPosition(args.shadow_anchor);
107
+
108
+ //create or retrieve existing MarkerImages
109
+ var markerImage = this.createOrRetrieveImage(args.marker_picture, args.marker_width, args.marker_height, imageAnchorPosition);
110
+ var shadowImage = this.createOrRetrieveImage(args.shadow_picture, args.shadow_width, args.shadow_height, shadowAnchorPosition);
111
+
112
+ return new google.maps.Marker({position: markerLatLng, map: this.map, icon: markerImage, title: args.marker_title, draggable: args.marker_draggable, shadow: shadowImage});
113
+ }
114
+ };
115
+
116
+ //checks if obj is included in arr Array and returns the position or false
117
+ Gmaps4Rails.includeMarkerImage = function(arr, obj) {
118
+ for(var i=0; i<arr.length; i++) {
119
+ if (arr[i].url == obj) {return i;}
120
+ }
121
+ return false;
122
+ };
123
+
124
+ // checks if MarkerImage exists before creating a new one
125
+ // returns a MarkerImage or false if ever something wrong is passed as argument
126
+ Gmaps4Rails.createOrRetrieveImage = function(currentMarkerPicture, markerWidth, markerHeight, imageAnchorPosition){
127
+ if (currentMarkerPicture === "" || currentMarkerPicture === null )
128
+ { return null;}
129
+
130
+ var test_image_index = this.includeMarkerImage(this.markerImages, currentMarkerPicture);
131
+ switch (test_image_index)
132
+ {
133
+ case false:
134
+ var markerImage = Gmaps4Rails.createMarkerImage(currentMarkerPicture, Gmaps4Rails.createSize(markerWidth, markerHeight), null, imageAnchorPosition, null );
135
+ this.markerImages.push(markerImage);
136
+ return markerImage;
137
+ break;
138
+ default:
139
+ if (typeof test_image_index == 'number') { return this.markerImages[test_image_index]; }
140
+ else { return false; }
141
+ break;
142
+ }
143
+ };
144
+
145
+ // clear markers
146
+ Gmaps4Rails.clearMarkers = function() {
147
+ for (var i = 0; i < this.markers.length; ++i) {
148
+ this.clearMarker(this.markers[i]);
149
+ }
150
+ };
151
+
152
+ //show and hide markers
153
+ Gmaps4Rails.showMarkers = function() {
154
+ for (var i = 0; i < this.markers.length; ++i) {
155
+ this.showMarker(this.markers[i]);
156
+ }
157
+ };
158
+
159
+ Gmaps4Rails.hideMarkers = function() {
160
+ for (var i = 0; i < this.markers.length; ++i) {
161
+ this.hideMarker(this.markers[i]);
162
+ }
163
+ };
164
+
165
+ Gmaps4Rails.clearMarker = function(marker) {
166
+ marker.serviceObject.setMap(null);
167
+ };
168
+
169
+ Gmaps4Rails.showMarker = function(marker) {
170
+ marker.serviceObject.setVisible(true);
171
+ };
172
+
173
+ Gmaps4Rails.hideMarker = function(marker) {
174
+ marker.serviceObject.setVisible(false);
175
+ };
176
+
177
+ Gmaps4Rails.extendBoundsWithMarkers = function(){
178
+ for (var i = 0; i < Gmaps4Rails.markers.length; ++i) {
179
+ Gmaps4Rails.boundsObject.extend(Gmaps4Rails.markers[i].serviceObject.position);
180
+ }
181
+ };
182
+
183
+ ////////////////////////////////////////////////////
184
+ /////////////////// Clusterer //////////////////////
185
+ ////////////////////////////////////////////////////
186
+
187
+ Gmaps4Rails.createClusterer = function(markers_array){
188
+ return new MarkerClusterer( Gmaps4Rails.map,
189
+ markers_array,
190
+ { maxZoom: Gmaps4Rails.markers_conf.clusterer_maxZoom, gridSize: Gmaps4Rails.markers_conf.clusterer_gridSize, styles: Gmaps4Rails.customClusterer() }
191
+ );
192
+ };
193
+
194
+ Gmaps4Rails.clearClusterer = function() {
195
+ this.markerClusterer.clearMarkers();
196
+ };
197
+
198
+ //creates clusters
199
+ Gmaps4Rails.clusterize = function()
200
+ {
201
+ if (this.markers_conf.do_clustering === true)
202
+ {
203
+ //first clear the existing clusterer if any
204
+ if (this.markerClusterer !== null) {
205
+ this.clearClusterer();
206
+ }
207
+
208
+ var markers_array = new Array;
209
+ for (var i = 0; i < this.markers.length; ++i) {
210
+ markers_array.push(this.markers[i].serviceObject);
211
+ }
212
+
213
+ this.markerClusterer = Gmaps4Rails.createClusterer(markers_array);
214
+ }
215
+ };
216
+
217
+ ////////////////////////////////////////////////////
218
+ /////////////////// INFO WINDOW ////////////////////
219
+ ////////////////////////////////////////////////////
220
+
221
+ // creates infowindows
222
+ Gmaps4Rails.createInfoWindow = function(marker_container){
223
+ var info_window;
224
+ if (this.markers_conf.custom_infowindow_class === null && Gmaps4Rails.exists(marker_container.description)) {
225
+ //create the infowindow
226
+ info_window = new google.maps.InfoWindow({content: marker_container.description });
227
+ //add the listener associated
228
+ google.maps.event.addListener(marker_container.serviceObject, 'click', this.openInfoWindow(info_window, marker_container.serviceObject));
229
+ }
230
+ else { //creating custom infowindow
231
+ if (this.exists(marker_container.description)) {
232
+ var boxText = document.createElement("div");
233
+ boxText.setAttribute("class", this.markers_conf.custom_infowindow_class); //to customize
234
+ boxText.innerHTML = marker_container.description;
235
+ info_window = new InfoBox(Gmaps4Rails.infobox(boxText));
236
+ google.maps.event.addListener(marker_container.serviceObject, 'click', this.openInfoWindow(info_window, marker_container.serviceObject));
237
+ }
238
+ }
239
+ };
240
+
241
+ Gmaps4Rails.openInfoWindow = function(infoWindow, marker) {
242
+ return function() {
243
+ // Close the latest selected marker before opening the current one.
244
+ if (Gmaps4Rails.visibleInfoWindow) {
245
+ Gmaps4Rails.visibleInfoWindow.close();
246
+ }
247
+
248
+ infoWindow.open(Gmaps4Rails.map, marker);
249
+ Gmaps4Rails.visibleInfoWindow = infoWindow;
250
+ };
251
+ };
252
+
253
+ ////////////////////////////////////////////////////
254
+ /////////////////// Other methods //////////////////
255
+ ////////////////////////////////////////////////////
256
+
257
+ Gmaps4Rails.fitBounds = function(){
258
+ this.map.fitBounds(this.boundsObject);
259
+ };
260
+
261
+ Gmaps4Rails.centerMapOnUser = function(){
262
+ Gmaps4Rails.map.setCenter(Gmaps4Rails.userLocation);
263
+ };
@@ -1,727 +1,496 @@
1
1
  var Gmaps4Rails = {
2
- //map config
3
- map: null, // contains the map we're working on
4
- visibleInfoWindow: null, //contains the current opened infowindow
2
+
3
+ //map config
4
+ map: null, // contains the map we're working on
5
+ visibleInfoWindow: null, //contains the current opened infowindow
5
6
  userLocation: null, //contains user's location if geolocalization was performed and successful
6
7
 
7
8
  //empty slots
8
- geolocationFailure: function() { return false;}, //triggered when geolocation fails. If customized, must be like: function(navigator_handles_geolocation){} where 'navigator_handles_geolocation' is a boolean
9
+ geolocationFailure: function() { return false;}, //triggered when geolocation fails. If customized, must be like: function(navigator_handles_geolocation){} where 'navigator_handles_geolocation' is a boolean
9
10
  callback: function() { return false;}, //to let user set a custom callback function
10
11
  customClusterer: function() { return null;}, //to let user set custom clusterer pictures
11
12
  infobox: function() { return null;}, //to let user use custom infoboxes
12
13
 
13
- //Map settings
14
- map_options: {
15
- id: 'gmaps4rails_map',
16
- disableDefaultUI: false,
17
- disableDoubleClickZoom: false,
18
- draggable: true,
19
- type: "ROADMAP", // HYBRID, ROADMAP, SATELLITE, TERRAIN
20
- detect_location: false, // should the browser attempt to use geolocation detection features of HTML5?
21
- center_on_user: false, // centers map on the location detected through the browser
22
- center_latitude: 0,
23
- center_longitude: 0,
24
- zoom: 1,
25
- maxZoom: null,
26
- minZoom: null,
27
- auto_adjust : false, // adjust the map to the markers if set to true
28
- auto_zoom: true, // zoom given by auto-adjust
29
- bounds: [] // adjust map to these limits. Should be [{"lat": , "lng": }]
30
- },
31
-
32
- //markers + info styling
33
- markers_conf: {
34
- // Marker config
35
- title: "",
36
- // MarkerImage config
37
- picture : "",
38
- width: 22,
39
- length: 32,
14
+
15
+
16
+ map_options: {
17
+ id: 'gmaps4rails_map',
18
+ draggable: true,
19
+ detect_location: false, // should the browser attempt to use geolocation detection features of HTML5?
20
+ center_on_user: false, // centers map on the location detected through the browser
21
+ center_latitude: 0,
22
+ center_longitude: 0,
23
+ zoom: 1,
24
+ maxZoom: null,
25
+ minZoom: null,
26
+ auto_adjust : true, // adjust the map to the markers if set to true
27
+ auto_zoom: true, // zoom given by auto-adjust
28
+ bounds: [] // adjust map to these limits. Should be [{"lat": , "lng": }]
29
+ },
30
+
31
+ markers_conf: {
32
+ // Marker config
33
+ title: "",
34
+ // MarkerImage config
35
+ picture : "",
36
+ width: 22,
37
+ length: 32,
40
38
  draggable: false, // how to modify: <%= gmaps( "markers" => { "data" => @object.to_gmaps4rails, "options" => { "draggable" => true }}) %>
41
- //clustering config
42
- do_clustering: true, // do clustering if set to true
43
- clusterer_gridSize: 50, // the more the quicker but the less precise
44
- clusterer_maxZoom: 5, // removes clusterer at this zoom level
45
- randomize: false, // Google maps can't display two markers which have the same coordinates. This randomizer enables to prevent this situation from happening.
46
- max_random_distance: 100, // in meters. Each marker coordinate could be altered by this distance in a random direction
47
- list_container: null, // id of the ul that will host links to all markers
48
- custom_cluster_pictures: null,
49
- custom_infowindow_class: null,
50
- 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.
51
- },
52
-
53
- //Stored variables
54
- markers: [], // contains all markers. A marker contains the following: {"description": , "longitude": , "title":, "latitude":, "picture": "", "width": "", "length": "", "sidebar": "", "serviceObject": google_marker}
55
- bounds_object: null, // contains current bounds from markers, polylines etc...
56
- polygons: [], // contains raw data, array of arrays (first element could be a hash containing options)
57
- polylines: [], // contains raw data, array of arrays (first element could be a hash containing options)
58
- circles: [], // contains raw data, array of hash
59
- markerClusterer: null, // contains all marker clusterers
39
+ //clustering config
40
+ do_clustering: false, // do clustering if set to true
41
+ randomize: false, // Google maps can't display two markers which have the same coordinates. This randomizer enables to prevent this situation from happening.
42
+ max_random_distance: 100, // in meters. Each marker coordinate could be altered by this distance in a random direction
43
+ list_container: null, // id of the ul that will host links to all markers
44
+ 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.
45
+ },
46
+
47
+ //Stored variables
48
+ markers: [], // contains all markers. A marker contains the following: {"description": , "longitude": , "title":, "latitude":, "picture": "", "width": "", "length": "", "sidebar": "", "serviceObject": google_marker}
49
+ boundsObject: null, // contains current bounds from markers, polylines etc...
50
+ polygons: [], // contains raw data, array of arrays (first element could be a hash containing options)
51
+ polylines: [], // contains raw data, array of arrays (first element could be a hash containing options)
52
+ circles: [], // contains raw data, array of hash
53
+ markerClusterer: null, // contains all marker clusterers
60
54
  markerImages: [],
55
+
56
+ //initializes the map
57
+ initialize: function() {
58
+
59
+ this.map = Gmaps4Rails.createMap();
60
+
61
+ if (this.map_options.detect_location === true || this.map_options.center_on_user === true) {
62
+ this.findUserLocation();
63
+ }
64
+ //resets sidebar if needed
65
+ this.resetSidebarContent();
66
+ },
67
+
68
+ findUserLocation: function() {
69
+ if(navigator.geolocation) {
70
+ //try to retrieve user's position
71
+ navigator.geolocation.getCurrentPosition(function(position) {
72
+ //saves the position in the userLocation variable
73
+ Gmaps4Rails.userLocation = Gmaps4Rails.createLatLng(position.coords.latitude, position.coords.longitude);
74
+ //change map's center to focus on user's geoloc if asked
75
+ if(Gmaps4Rails.map_options.center_on_user === true) {
76
+ Gmaps4Rails.centerMapOnUser();
77
+ }
78
+ },
79
+ function() {
80
+ //failure, but the navigator handles geolocation
81
+ this.geolocationFailure(true);
82
+ });
83
+ }
84
+ else {
85
+ //failure but the navigator doesn't handle geolocation
86
+ this.geolocationFailure(false);
87
+ }
88
+ },
89
+
90
+ ////////////////////////////////////////////////////
91
+ //////////////////// DIRECTIONS ////////////////////
92
+ ////////////////////////////////////////////////////
93
+
94
+ create_direction: function(){
95
+ var directionsDisplay = new google.maps.DirectionsRenderer();
96
+ var directionsService = new google.maps.DirectionsService();
97
+
98
+ directionsDisplay.setMap(this.map);
99
+ //display panel only if required
100
+ if (this.direction_conf.display_panel) { directionsDisplay.setPanel(document.getElementById(this.direction_conf.panel_id)); }
101
+ directionsDisplay.setOptions({
102
+ suppressMarkers: false,
103
+ suppressInfoWindows: false,
104
+ suppressPolylines: false
105
+ });
106
+ var request = {
107
+ origin: this.direction_conf.origin,
108
+ destination: this.direction_conf.destination,
109
+ waypoints: this.direction_conf.waypoints,
110
+ optimizeWaypoints: this.direction_conf.optimizeWaypoints,
111
+ unitSystem: google.maps.DirectionsUnitSystem[this.direction_conf.unitSystem],
112
+ avoidHighways: this.direction_conf.avoidHighways,
113
+ avoidTolls: this.direction_conf.avoidTolls,
114
+ region: this.direction_conf.region,
115
+ travelMode: google.maps.DirectionsTravelMode[this.direction_conf.travelMode],
116
+ language: "en"
117
+ };
118
+ directionsService.route(request, function(response, status) {
119
+ if (status == google.maps.DirectionsStatus.OK) {
120
+ directionsDisplay.setDirections(response);
121
+ }
122
+ });
123
+ },
61
124
 
62
- //Polygon Styling
63
- polygons_conf: { // default style for polygons
64
- strokeColor: "#FFAA00",
65
- strokeOpacity: 0.8,
66
- strokeWeight: 2,
67
- fillColor: "#000000",
68
- fillOpacity: 0.35
69
- },
70
-
71
- //Polyline Styling
72
- polylines_conf: { //default style for polylines
73
- strokeColor: "#FF0000",
74
- strokeOpacity: 1,
75
- strokeWeight: 2
76
- },
77
-
78
- //Circle Styling
79
- circles_conf: { //default style for circles
80
- fillColor: "#00AAFF",
81
- fillOpacity: 0.35,
82
- strokeColor: "#FFAA00",
83
- strokeOpacity: 0.8,
84
- strokeWeight: 2,
85
- clickable: false,
86
- zIndex: null
87
- },
88
-
89
- //Direction Settings
90
- direction_conf: {
91
- panel_id: null,
92
- display_panel: false,
93
- origin: null,
94
- destination: null,
95
- waypoints: [], //[{location: "toulouse,fr", stopover: true}, {location: "Clermont-Ferrand, fr", stopover: true}]
96
- optimizeWaypoints: false,
97
- unitSystem: "METRIC", //IMPERIAL
98
- avoidHighways: false,
99
- avoidTolls: false,
100
- region: null,
101
- travelMode: "DRIVING" //WALKING, BICYCLING
102
- },
103
-
104
- //initializes the map
105
- initialize: function() {
106
-
107
- this.map = Gmaps4Rails.createMap();
108
-
109
- if (this.map_options.detect_location === true || this.map_options.center_on_user === true) {
110
- this.findUserLocation();
111
- }
112
- //resets sidebar if needed
113
- this.resetSidebarContent();
114
- },
115
-
116
- findUserLocation: function() {
117
- if(navigator.geolocation) {
118
- //try to retrieve user's position
119
- navigator.geolocation.getCurrentPosition(function(position) {
120
- //saves the position in the userLocation variable
121
- Gmaps4Rails.userLocation = Gmaps4Rails.createLatLng(position.coords.latitude, position.coords.longitude);
122
- //change map's center to focus on user's geoloc if asked
123
- if(Gmaps4Rails.map_options.center_on_user === true) {
124
- Gmaps4Rails.map.setCenter(Gmaps4Rails.userLocation);
125
- }
126
- },
127
- function() {
128
- //failure, but the navigator handles geolocation
129
- this.geolocationFailure(true);
130
- });
131
- }
132
- else {
133
- //failure but the navigator doesn't handle geolocation
134
- this.geolocationFailure(false);
135
- }
136
- },
137
-
138
- ////////////////////////////////////////////////////
139
- //////////////////// DIRECTIONS ////////////////////
140
- ////////////////////////////////////////////////////
141
-
142
- create_direction: function(){
143
- var directionsDisplay = new google.maps.DirectionsRenderer();
144
- var directionsService = new google.maps.DirectionsService();
145
-
146
- directionsDisplay.setMap(this.map);
147
- //display panel only if required
148
- if (this.direction_conf.display_panel) { directionsDisplay.setPanel(document.getElementById(this.direction_conf.panel_id)); }
149
- directionsDisplay.setOptions({
150
- suppressMarkers: false,
151
- suppressInfoWindows: false,
152
- suppressPolylines: false
153
- });
154
- var request = {
155
- origin: this.direction_conf.origin,
156
- destination: this.direction_conf.destination,
157
- waypoints: this.direction_conf.waypoints,
158
- optimizeWaypoints: this.direction_conf.optimizeWaypoints,
159
- unitSystem: google.maps.DirectionsUnitSystem[this.direction_conf.unitSystem],
160
- avoidHighways: this.direction_conf.avoidHighways,
161
- avoidTolls: this.direction_conf.avoidTolls,
162
- region: this.direction_conf.region,
163
- travelMode: google.maps.DirectionsTravelMode[this.direction_conf.travelMode],
164
- language: "en"
165
- };
166
- directionsService.route(request, function(response, status) {
167
- if (status == google.maps.DirectionsStatus.OK) {
168
- directionsDisplay.setDirections(response);
169
- }
170
- });
171
- },
172
-
173
- ////////////////////////////////////////////////////
174
- ///////////////////// CIRCLES //////////////////////
175
- ////////////////////////////////////////////////////
176
-
177
- //Loops through all circles
178
- //Loops through all circles and draws them
179
- create_circles: function() {
180
- for (var i = 0; i < this.circles.length; ++i) {
181
- //by convention, default style configuration could be integrated in the first element
182
- this.create_circle(this.circles[i]);
183
- }
184
- },
185
-
186
- create_circle: function(circle) {
187
- if ( i === 0 ) {
188
- if (this.exists(circle.strokeColor )) { this.circles_conf.strokeColor = circle.strokeColor; }
189
- if (this.exists(circle.strokeOpacity)) { this.circles_conf.strokeOpacity = circle.strokeOpacity; }
190
- if (this.exists(circle.strokeWeight )) { this.circles_conf.strokeWeight = circle.strokeWeight; }
191
- if (this.exists(circle.fillColor )) { this.circles_conf.fillColor = circle.fillColor; }
192
- if (this.exists(circle.fillOpacity )) { this.circles_conf.fillOpacity = circle.fillOpacity; }
193
- }
194
- if (this.exists(circle.latitude) && this.exists(circle.longitude)) {
195
- // always check if a config is given, if not, use defaults
196
- // NOTE: is there a cleaner way to do this? Maybe a hash merge of some sort?
197
- var newCircle = new google.maps.Circle({
198
- center: Gmaps4Rails.createLatLng(circle.latitude, circle.longitude),
199
- strokeColor: circle.strokeColor || this.circles_conf.strokeColor,
200
- strokeOpacity: circle.strokeOpacity || this.circles_conf.strokeOpacity,
201
- strokeWeight: circle.strokeWeight || this.circles_conf.strokeWeight,
202
- fillOpacity: circle.fillOpacity || this.circles_conf.fillOpacity,
203
- fillColor: circle.fillColor || this.circles_conf.fillColor,
204
- clickable: circle.clickable || this.circles_conf.clickable,
205
- zIndex: circle.zIndex || this.circles_conf.zIndex,
206
- radius: circle.radius
207
- });
208
- circle.serviceObject = newCircle;
209
- newCircle.setMap(this.map);
210
- }
211
- },
212
-
213
- // clear circles
125
+ ////////////////////////////////////////////////////
126
+ ///////////////////// CIRCLES //////////////////////
127
+ ////////////////////////////////////////////////////
128
+
129
+ //Loops through all circles
130
+ //Loops through all circles and draws them
131
+ create_circles: function() {
132
+ for (var i = 0; i < this.circles.length; ++i) {
133
+ //by convention, default style configuration could be integrated in the first element
134
+ this.create_circle(this.circles[i]);
135
+ }
136
+ },
137
+
138
+ create_circle: function(circle) {
139
+ if ( i === 0 ) {
140
+ if (this.exists(circle.strokeColor )) { this.circles_conf.strokeColor = circle.strokeColor; }
141
+ if (this.exists(circle.strokeOpacity)) { this.circles_conf.strokeOpacity = circle.strokeOpacity; }
142
+ if (this.exists(circle.strokeWeight )) { this.circles_conf.strokeWeight = circle.strokeWeight; }
143
+ if (this.exists(circle.fillColor )) { this.circles_conf.fillColor = circle.fillColor; }
144
+ if (this.exists(circle.fillOpacity )) { this.circles_conf.fillOpacity = circle.fillOpacity; }
145
+ }
146
+ if (this.exists(circle.lat) && this.exists(circle.lng)) {
147
+ // always check if a config is given, if not, use defaults
148
+ // NOTE: is there a cleaner way to do this? Maybe a hash merge of some sort?
149
+ var newCircle = new google.maps.Circle({
150
+ center: Gmaps4Rails.createLatLng(circle.lat, circle.lng),
151
+ strokeColor: circle.strokeColor || this.circles_conf.strokeColor,
152
+ strokeOpacity: circle.strokeOpacity || this.circles_conf.strokeOpacity,
153
+ strokeWeight: circle.strokeWeight || this.circles_conf.strokeWeight,
154
+ fillOpacity: circle.fillOpacity || this.circles_conf.fillOpacity,
155
+ fillColor: circle.fillColor || this.circles_conf.fillColor,
156
+ clickable: circle.clickable || this.circles_conf.clickable,
157
+ zIndex: circle.zIndex || this.circles_conf.zIndex,
158
+ radius: circle.radius
159
+ });
160
+ circle.serviceObject = newCircle;
161
+ newCircle.setMap(this.map);
162
+ }
163
+ },
164
+
165
+ // clear circles
214
166
  clear_circles: function() {
215
- for (var i = 0; i < this.circles.length; ++i) {
216
- this.clear_circle(this.circles[i]);
167
+ for (var i = 0; i < this.circles.length; ++i) {
168
+ this.clear_circle(this.circles[i]);
169
+ }
170
+ },
171
+
172
+ clear_circle: function(circle) {
173
+ circle.serviceObject.setMap(null);
174
+ },
175
+
176
+ hide_circles: function() {
177
+ for (var i = 0; i < this.circles.length; ++i) {
178
+ this.hide_circle(this.circles[i]);
217
179
  }
218
180
  },
219
181
 
220
- clear_circle: function(circle) {
221
- circle.serviceObject.setMap(null);
222
- },
223
-
224
- hide_circles: function() {
225
- for (var i = 0; i < this.circles.length; ++i) {
226
- this.hide_circle(this.circles[i]);
182
+ hide_circle: function(circle) {
183
+ circle.serviceObject.setMap(null);
184
+ },
185
+
186
+ show_circles: function() {
187
+ for (var i = 0; i < this.circles.length; ++i) {
188
+ this.show_circle(this.circles[i]);
227
189
  }
228
- },
229
-
230
- hide_circle: function(circle) {
231
- circle.serviceObject.setMap(null);
232
- },
233
-
234
- show_circles: function() {
235
- for (var i = 0; i < this.circles.length; ++i) {
236
- this.show_circle(this.circles[i]);
190
+ },
191
+
192
+ show_circle: function(circle) {
193
+ circle.serviceObject.setMap(this.map);
194
+ },
195
+
196
+ ////////////////////////////////////////////////////
197
+ ///////////////////// POLYGONS /////////////////////
198
+ ////////////////////////////////////////////////////
199
+
200
+ //polygons is an array of arrays. It loops.
201
+ create_polygons: function(){
202
+ for (var i = 0; i < this.polygons.length; ++i) {
203
+ this.create_polygon(i);
204
+ }
205
+ },
206
+
207
+ //creates a single polygon, triggered by create_polygons
208
+ create_polygon: function(i){
209
+ var polygon_coordinates = [];
210
+ var strokeColor;
211
+ var strokeOpacity;
212
+ var strokeWeight;
213
+ var fillColor;
214
+ var fillOpacity;
215
+ //Polygon points are in an Array, that's why looping is necessary
216
+ for (var j = 0; j < this.polygons[i].length; ++j) {
217
+ var latlng = Gmaps4Rails.createLatLng(this.polygons[i][j].lat, this.polygons[i][j].lng);
218
+ polygon_coordinates.push(latlng);
219
+ //first element of an Array could contain specific configuration for this particular polygon. If no config given, use default
220
+ if (j===0) {
221
+ strokeColor = this.polygons[i][j].strokeColor || this.polygons_conf.strokeColor;
222
+ strokeOpacity = this.polygons[i][j].strokeOpacity || this.polygons_conf.strokeOpacity;
223
+ strokeWeight = this.polygons[i][j].strokeWeight || this.polygons_conf.strokeWeight;
224
+ fillColor = this.polygons[i][j].fillColor || this.polygons_conf.fillColor;
225
+ fillOpacity = this.polygons[i][j].fillOpacity || this.polygons_conf.fillOpacity;
226
+ }
237
227
  }
238
- },
239
-
240
- show_circle: function(circle) {
241
- circle.serviceObject.setMap(this.map);
242
- },
243
-
244
- ////////////////////////////////////////////////////
245
- ///////////////////// POLYGONS /////////////////////
246
- ////////////////////////////////////////////////////
247
-
248
- //polygons is an array of arrays. It loops.
249
- create_polygons: function(){
250
- for (var i = 0; i < this.polygons.length; ++i) {
251
- this.create_polygon(i);
252
- }
253
- },
254
-
255
- //creates a single polygon, triggered by create_polygons
256
- create_polygon: function(i){
257
- var polygon_coordinates = [];
258
- var strokeColor;
259
- var strokeOpacity;
260
- var strokeWeight;
261
- var fillColor;
262
- var fillOpacity;
263
- //Polygon points are in an Array, that's why looping is necessary
264
- for (var j = 0; j < this.polygons[i].length; ++j) {
265
- var latlng = Gmaps4Rails.createLatLng(this.polygons[i][j].latitude, this.polygons[i][j].longitude);
266
- polygon_coordinates.push(latlng);
267
- //first element of an Array could contain specific configuration for this particular polygon. If no config given, use default
268
- if (j===0) {
269
- strokeColor = this.polygons[i][j].strokeColor || this.polygons_conf.strokeColor;
270
- strokeOpacity = this.polygons[i][j].strokeOpacity || this.polygons_conf.strokeOpacity;
271
- strokeWeight = this.polygons[i][j].strokeWeight || this.polygons_conf.strokeWeight;
272
- fillColor = this.polygons[i][j].fillColor || this.polygons_conf.fillColor;
273
- fillOpacity = this.polygons[i][j].fillOpacity || this.polygons_conf.fillOpacity;
274
- }
275
- }
276
-
277
- // Construct the polygon
278
- var new_poly = new google.maps.Polygon({
279
- paths: polygon_coordinates,
280
- strokeColor: strokeColor,
281
- strokeOpacity: strokeOpacity,
282
- strokeWeight: strokeWeight,
283
- fillColor: fillColor,
284
- fillOpacity: fillOpacity,
285
- clickable: false
286
- });
287
- //save polygon in list
288
- this.polygons[i].serviceObject = new_poly;
289
- new_poly.setMap(this.map);
290
- },
291
-
292
- ////////////////////////////////////////////////////
293
- /////////////////// POLYLINES //////////////////////
294
- ////////////////////////////////////////////////////
295
-
296
- //polylines is an array of arrays. It loops.
297
- create_polylines: function(){
298
- for (var i = 0; i < this.polylines.length; ++i) {
299
- this.create_polyline(i);
300
- }
301
- },
302
-
303
- //creates a single polyline, triggered by create_polylines
304
- create_polyline: function(i) {
305
- var polyline_coordinates = [];
306
- var strokeColor;
307
- var strokeOpacity;
308
- var strokeWeight;
309
-
310
- //2 cases here, either we have a coded array of LatLng or we have an Array of LatLng
311
- for (var j = 0; j < this.polylines[i].length; ++j) {
312
- //if we have a coded array
313
- if (this.exists(this.polylines[i][j].coded_array)) {
314
- var decoded_array = new google.maps.geometry.encoding.decodePath(this.polylines[i][j].coded_array);
315
- //loop through every point in the array
316
- for (var k = 0; k < decoded_array.length; ++k) {
317
- polyline_coordinates.push(decoded_array[k]);
318
- polyline_coordinates.push(decoded_array[k]);
319
- }
320
- }
321
- //or we have an array of latlng
322
- else{
323
- //by convention, a single polyline could be customized in the first array or it uses default values
324
- if (j===0) {
325
- strokeColor = this.polylines[i][0].strokeColor || this.polylines_conf.strokeColor;
326
- strokeOpacity = this.polylines[i][0].strokeOpacity || this.polylines_conf.strokeOpacity;
327
- strokeWeight = this.polylines[i][0].strokeWeight || this.polylines_conf.strokeWeight;
328
- }
329
- //add latlng if positions provided
330
- if (this.exists(this.polylines[i][j].latitude) && this.exists(this.polylines[i][j].longitude)) {
331
- var latlng = Gmaps4Rails.createLatLng(this.polylines[i][j].latitude, this.polylines[i][j].longitude);
332
- polyline_coordinates.push(latlng);
333
- }
334
- }
335
- }
336
- // Construct the polyline
337
- var new_poly = new google.maps.Polyline({
338
- path: polyline_coordinates,
339
- strokeColor: strokeColor,
340
- strokeOpacity: strokeOpacity,
341
- strokeWeight: strokeWeight,
342
- clickable: false
343
- });
344
- //save polyline
345
- this.polylines[i].serviceObject = new_poly;
346
- new_poly.setMap(this.map);
347
- },
348
-
349
- ////////////////////////////////////////////////////
350
- ///////////////////// MARKERS //////////////////////
351
- //////////////////tests coded///////////////////////
352
-
353
- //creates, clusterizes and adjusts map
354
- create_markers: function() {
355
- this.markers_conf.offset = 0;
356
- this.createServiceMarkersFromMarkers();
357
- this.clusterize();
358
- this.adjustMapToBounds();
359
- },
360
-
361
- //create google.maps Markers from data provided by user
362
- createServiceMarkersFromMarkers: function() {
363
- for (var i = this.markers_conf.offset; i < this.markers.length; ++i) {
364
- //check if the marker has not already been created
365
- if (!this.exists(this.markers[i].serviceObject)) {
366
- //extract options, test if value passed or use default
367
- var marker_picture = this.exists(this.markers[i].picture) ? this.markers[i].picture : this.markers_conf.picture;
368
- var marker_width = this.exists(this.markers[i].width) ? this.markers[i].width : this.markers_conf.width;
369
- var marker_height = this.exists(this.markers[i].height) ? this.markers[i].height : this.markers_conf.length;
370
- var marker_title = this.exists(this.markers[i].title) ? this.markers[i].title : null;
371
- var marker_anchor = this.exists(this.markers[i].marker_anchor) ? this.markers[i].marker_anchor : null;
372
- var shadow_anchor = this.exists(this.markers[i].shadow_anchor) ? this.markers[i].shadow_anchor : null;
373
- var shadow_picture = this.exists(this.markers[i].shadow_picture) ? this.markers[i].shadow_picture : null;
374
- var shadow_width = this.exists(this.markers[i].shadow_width) ? this.markers[i].shadow_width : null;
375
- var shadow_height = this.exists(this.markers[i].shadow_height) ? this.markers[i].shadow_height : null;
376
- var marker_draggable = this.exists(this.markers[i].draggable) ? this.markers[i].draggable : this.markers_conf.draggable;
377
- var Lat = this.markers[i].latitude;
378
- var Lng = this.markers[i].longitude;
379
-
380
- //alter coordinates if randomize is true
381
- if ( this.markers_conf.randomize) {
382
- var LatLng = Gmaps4Rails.randomize(Lat, Lng);
383
- //retrieve coordinates from the array
384
- Lat = LatLng[0]; Lng = LatLng[1];
385
- }
386
-
387
- var markerLatLng = Gmaps4Rails.createLatLng(Lat, Lng);
388
- var thisMarker;
389
-
390
- // Marker sizes are expressed as a Size of X,Y
391
- if (marker_picture === "" ) {
392
- thisMarker = Gmaps4Rails.createMarker({position: markerLatLng, map: this.map, title: marker_title, draggable: marker_draggable});
393
- } else {
394
- // calculate MarkerImage anchor location
395
- var imageAnchorPosition = this.createImageAnchorPosition(marker_anchor);
396
- var shadowAnchorPosition = this.createImageAnchorPosition(shadow_anchor);
397
-
398
- //create or retrieve existing MarkerImages
399
- var markerImage = this.createOrRetrieveImage(marker_picture, marker_width, marker_height, imageAnchorPosition);
400
- var shadowImage = this.createOrRetrieveImage(shadow_picture, shadow_width, shadow_height, shadowAnchorPosition);
401
-
402
- thisMarker = Gmaps4Rails.createMarker({position: markerLatLng, map: this.map, icon: markerImage, title: marker_title, draggable: marker_draggable, shadow: shadowImage});
403
- }
404
-
405
- //save object
406
- this.markers[i].serviceObject = thisMarker;
407
- //add infowindowstuff if enabled
408
- this.createInfoWindow(this.markers[i]);
409
- //create sidebar if enabled
410
- this.createSidebar(this.markers[i]);
411
- }
412
- }
413
- this.markers_conf.offset = this.markers.length;
414
- },
415
-
416
- // checks if MarkerImage exists before creating a new one
417
- // returns a MarkerImage or false if ever something wrong is passed as argument
418
- createOrRetrieveImage: function(currentMarkerPicture, markerWidth, markerHeight, imageAnchorPosition){
419
- if (currentMarkerPicture === "" || currentMarkerPicture === null )
420
- { return null;}
421
-
422
- var test_image_index = this.includeMarkerImage(this.markerImages, currentMarkerPicture);
423
- switch (test_image_index)
424
- {
425
- case false:
426
- var markerImage = Gmaps4Rails.createMarkerImage(currentMarkerPicture, Gmaps4Rails.createSize(markerWidth, markerHeight), null, imageAnchorPosition, null );
427
- this.markerImages.push(markerImage);
428
- return markerImage;
429
- break;
430
- default:
431
- if (typeof test_image_index == 'number') { return this.markerImages[test_image_index]; }
432
- else { return false; }
433
- break;
434
- }
435
- },
436
-
437
- // creates Image Anchor Position or return null if nothing passed
438
- createImageAnchorPosition: function(anchorLocation) {
439
- if (anchorLocation === null)
440
- { return null; }
441
- else
442
- { return Gmaps4Rails.createPoint(anchorLocation[0], anchorLocation[1]); }
443
- },
444
-
445
- // clear markers
446
- clearMarkers: function() {
447
- for (var i = 0; i < this.markers.length; ++i) {
448
- this.clearMarker(this.markers[i]);
228
+
229
+ // Construct the polygon
230
+ var new_poly = new google.maps.Polygon({
231
+ paths: polygon_coordinates,
232
+ strokeColor: strokeColor,
233
+ strokeOpacity: strokeOpacity,
234
+ strokeWeight: strokeWeight,
235
+ fillColor: fillColor,
236
+ fillOpacity: fillOpacity,
237
+ clickable: false
238
+ });
239
+ //save polygon in list
240
+ this.polygons[i].serviceObject = new_poly;
241
+ new_poly.setMap(this.map);
242
+ },
243
+
244
+ ////////////////////////////////////////////////////
245
+ /////////////////// POLYLINES //////////////////////
246
+ ////////////////////////////////////////////////////
247
+
248
+ //polylines is an array of arrays. It loops.
249
+ create_polylines: function(){
250
+ for (var i = 0; i < this.polylines.length; ++i) {
251
+ this.create_polyline(i);
449
252
  }
450
253
  },
451
254
 
452
- // show and hide markers
453
- showMarkers: function() {
454
- for (var i = 0; i < this.markers.length; ++i) {
455
- this.showMarker(this.markers[i]);
255
+ //creates a single polyline, triggered by create_polylines
256
+ create_polyline: function(i) {
257
+ var polyline_coordinates = [];
258
+ var strokeColor;
259
+ var strokeOpacity;
260
+ var strokeWeight;
261
+
262
+ //2 cases here, either we have a coded array of LatLng or we have an Array of LatLng
263
+ for (var j = 0; j < this.polylines[i].length; ++j) {
264
+ //if we have a coded array
265
+ if (this.exists(this.polylines[i][j].coded_array)) {
266
+ var decoded_array = new google.maps.geometry.encoding.decodePath(this.polylines[i][j].coded_array);
267
+ //loop through every point in the array
268
+ for (var k = 0; k < decoded_array.length; ++k) {
269
+ polyline_coordinates.push(decoded_array[k]);
270
+ polyline_coordinates.push(decoded_array[k]);
271
+ }
272
+ }
273
+ //or we have an array of latlng
274
+ else{
275
+ //by convention, a single polyline could be customized in the first array or it uses default values
276
+ if (j===0) {
277
+ strokeColor = this.polylines[i][0].strokeColor || this.polylines_conf.strokeColor;
278
+ strokeOpacity = this.polylines[i][0].strokeOpacity || this.polylines_conf.strokeOpacity;
279
+ strokeWeight = this.polylines[i][0].strokeWeight || this.polylines_conf.strokeWeight;
280
+ }
281
+ //add latlng if positions provided
282
+ if (this.exists(this.polylines[i][j].lat) && this.exists(this.polylines[i][j].lng)) {
283
+ var latlng = Gmaps4Rails.createLatLng(this.polylines[i][j].lat, this.polylines[i][j].lng);
284
+ polyline_coordinates.push(latlng);
285
+ }
286
+ }
456
287
  }
457
- },
458
-
459
- hideMarkers: function() {
460
- for (var i = 0; i < this.markers.length; ++i) {
461
- this.hideMarker(this.markers[i]);
288
+ // Construct the polyline
289
+ var new_poly = new google.maps.Polyline({
290
+ path: polyline_coordinates,
291
+ strokeColor: strokeColor,
292
+ strokeOpacity: strokeOpacity,
293
+ strokeWeight: strokeWeight,
294
+ clickable: false
295
+ });
296
+ //save polyline
297
+ this.polylines[i].serviceObject = new_poly;
298
+ new_poly.setMap(this.map);
299
+ },
300
+
301
+ ////////////////////////////////////////////////////
302
+ ///////////////////// MARKERS //////////////////////
303
+ //////////////////tests coded///////////////////////
304
+
305
+ //creates, clusterizes and adjusts map
306
+ create_markers: function() {
307
+ this.markers_conf.offset = 0;
308
+ this.createServiceMarkersFromMarkers();
309
+ this.clusterize();
310
+ this.adjustMapToBounds();
311
+ },
312
+
313
+ //create google.maps Markers from data provided by user
314
+ createServiceMarkersFromMarkers: function() {
315
+ for (var i = this.markers_conf.offset; i < this.markers.length; ++i) {
316
+ //check if the marker has not already been created
317
+ // if (!this.exists(this.markers[i].serviceObject && this.provider == "google")) {
318
+ //extract options, test if value passed or use default
319
+ var Lat = this.markers[i].lat;
320
+ var Lng = this.markers[i].lng;
321
+
322
+ //alter coordinates if randomize is true
323
+ if ( this.markers_conf.randomize) {
324
+ var LatLng = Gmaps4Rails.randomize(Lat, Lng);
325
+ //retrieve coordinates from the array
326
+ Lat = LatLng[0]; Lng = LatLng[1];
327
+ }
328
+ //save object
329
+ this.markers[i].serviceObject = Gmaps4Rails.createMarker({
330
+ "marker_picture": this.exists(this.markers[i].picture) ? this.markers[i].picture : this.markers_conf.picture,
331
+ "marker_width": this.exists(this.markers[i].width) ? this.markers[i].width : this.markers_conf.width,
332
+ "marker_height": this.exists(this.markers[i].height) ? this.markers[i].height : this.markers_conf.length,
333
+ "marker_title": this.exists(this.markers[i].title) ? this.markers[i].title : null,
334
+ "marker_anchor": this.exists(this.markers[i].marker_anchor) ? this.markers[i].marker_anchor : null,
335
+ "shadow_anchor": this.exists(this.markers[i].shadow_anchor) ? this.markers[i].shadow_anchor : null,
336
+ "shadow_picture": this.exists(this.markers[i].shadow_picture) ? this.markers[i].shadow_picture : null,
337
+ "shadow_width": this.exists(this.markers[i].shadow_width) ? this.markers[i].shadow_width : null,
338
+ "shadow_height": this.exists(this.markers[i].shadow_height) ? this.markers[i].shadow_height : null,
339
+ "marker_draggable": this.exists(this.markers[i].draggable) ? this.markers[i].draggable : this.markers_conf.draggable,
340
+ "Lat": Lat,
341
+ "Lng": Lng,
342
+ "index": i
343
+ });
344
+ //add infowindowstuff if enabled
345
+ this.createInfoWindow(this.markers[i]);
346
+ //create sidebar if enabled
347
+ this.createSidebar(this.markers[i]);
348
+ // }
462
349
  }
463
- },
464
-
350
+ this.markers_conf.offset = this.markers.length;
351
+ },
352
+
353
+
354
+ // creates Image Anchor Position or return null if nothing passed
355
+ createImageAnchorPosition: function(anchorLocation) {
356
+ if (anchorLocation === null)
357
+ { return null; }
358
+ else
359
+ { return Gmaps4Rails.createPoint(anchorLocation[0], anchorLocation[1]); }
360
+ },
361
+
465
362
  // replace old markers with new markers on an existing map
466
363
  replaceMarkers: function(new_markers){
467
- //reset previous markers
468
- this.markers = new Array;
469
- //reset current bounds
470
- this.serviceBounds = Gmaps4Rails.createLatLngBounds();
471
- //reset sidebar content if exists
472
- this.resetSidebarContent();
473
- //add new markers
474
- this.addMarkers(new_markers);
364
+ this.clearMarkers();
365
+ //reset previous markers
366
+ this.markers = new Array;
367
+ //reset current bounds
368
+ this.boundsObject = Gmaps4Rails.createLatLngBounds();
369
+ //reset sidebar content if exists
370
+ this.resetSidebarContent();
371
+ //add new markers
372
+ this.addMarkers(new_markers);
475
373
  },
476
374
 
477
- //add new markers to on an existing map
375
+ //add new markers to on an existing map
478
376
  addMarkers: function(new_markers){
479
- //update the list of markers to take into account
377
+ //update the list of markers to take into account
480
378
  this.markers = this.markers.concat(new_markers);
481
379
  //put markers on the map
482
380
  this.create_markers();
483
381
  },
484
-
485
- //creates clusters
486
- clusterize: function()
487
- {
488
- if (this.markers_conf.do_clustering === true)
489
- {
490
- //first clear the existing clusterer if any
491
- if (this.markerClusterer !== null) {
492
- this.clearClusterer();
493
- }
494
-
495
- var markers_array = new Array;
496
- for (var i = 0; i < this.markers.length; ++i) {
497
- markers_array.push(this.markers[i].serviceObject);
498
- }
499
382
 
500
- this.markerClusterer = Gmaps4Rails.createClusterer(markers_array);
501
- }
502
- },
503
-
504
- ////////////////////////////////////////////////////
505
- /////////////////// INFO WINDOW ////////////////////
506
- ////////////////////////////////////////////////////
507
-
508
- // creates infowindows
509
- createInfoWindow: function(marker_container){
510
- var info_window;
511
- if (this.markers_conf.custom_infowindow_class === null) {
512
- //create the infowindow
513
- info_window = new google.maps.InfoWindow({content: marker_container.description });
514
- //add the listener associated
515
- google.maps.event.addListener(marker_container.serviceObject, 'click', this.openInfoWindow(info_window, marker_container.serviceObject));
516
- }
517
- else { //creating custom infowindow
518
- if (this.exists(marker_container.description)) {
519
- var boxText = document.createElement("div");
520
- boxText.setAttribute("class", this.markers_conf.custom_infowindow_class); //to customize
521
- boxText.innerHTML = marker_container.description;
522
- info_window = new InfoBox(Gmaps4Rails.infobox(boxText));
523
- google.maps.event.addListener(marker_container.serviceObject, 'click', this.openInfoWindow(info_window, marker_container.serviceObject));
524
- }
525
- }
526
- },
527
-
528
- openInfoWindow: function(infoWindow, marker) {
529
- return function() {
530
- // Close the latest selected marker before opening the current one.
531
- if (Gmaps4Rails.visibleInfoWindow) {
532
- Gmaps4Rails.visibleInfoWindow.close();
533
- }
534
-
535
- infoWindow.open(Gmaps4Rails.map, marker);
536
- Gmaps4Rails.visibleInfoWindow = infoWindow;
537
- };
383
+ ////////////////////////////////////////////////////
384
+ ///////////////////// SIDEBAR //////////////////////
385
+ ////////////////////////////////////////////////////
386
+
387
+ //creates sidebar
388
+ createSidebar: function(marker_container){
389
+ if (this.markers_conf.list_container)
390
+ {
391
+ var ul = document.getElementById(this.markers_conf.list_container);
392
+ var li = document.createElement('li');
393
+ var aSel = document.createElement('a');
394
+ aSel.href = 'javascript:void(0);';
395
+ var html = this.exists(marker_container.sidebar) ? marker_container.sidebar : "Marker";
396
+ aSel.innerHTML = html;
397
+ aSel.onclick = this.sidebar_element_handler(marker_container.serviceObject, 'click');
398
+ li.appendChild(aSel);
399
+ ul.appendChild(li);
400
+ }
538
401
  },
539
402
 
540
- ////////////////////////////////////////////////////
541
- ///////////////////// SIDEBAR //////////////////////
542
- ////////////////////////////////////////////////////
543
-
544
- //creates sidebar
545
- createSidebar: function(marker_container){
546
- if (this.markers_conf.list_container)
547
- {
548
- var ul = document.getElementById(this.markers_conf.list_container);
549
- var li = document.createElement('li');
550
- var aSel = document.createElement('a');
551
- aSel.href = 'javascript:void(0);';
552
- var html = this.exists(marker_container.sidebar) ? marker_container.sidebar : "Marker";
553
- aSel.innerHTML = html;
554
- aSel.onclick = this.sidebar_element_handler(marker_container.serviceObject, 'click');
555
- li.appendChild(aSel);
556
- ul.appendChild(li);
557
- }
558
- },
559
-
560
- //moves map to marker clicked + open infowindow
403
+ //moves map to marker clicked + open infowindow
561
404
  sidebar_element_handler: function(marker, eventType) {
562
405
  return function() {
563
- Gmaps4Rails.map.panTo(marker.position);
406
+ Gmaps4Rails.map.panTo(marker.position);
564
407
  google.maps.event.trigger(marker, eventType);
565
408
  };
566
409
  },
567
410
 
568
- resetSidebarContent: function(){
569
- if (this.markers_conf.list_container !== null ){
570
- var ul = document.getElementById(this.markers_conf.list_container);
571
- ul.innerHTML = "";
572
- }
573
- },
574
-
575
- ////////////////////////////////////////////////////
576
- ////////////////// MISCELLANEOUS ///////////////////
577
- ////////////////////////////////////////////////////
578
-
579
- //to make the map fit the different LatLng points
580
- adjustMapToBounds: function(latlng) {
581
-
582
- //FIRST_STEP: retrieve all bounds
583
- //create the bounds object only if necessary
584
- if (this.map_options.auto_adjust || this.map_options.bounds !== null) {
585
- this.serviceBounds = Gmaps4Rails.createLatLngBounds();
586
- }
587
-
588
- //if autodjust is true, must get bounds from markers polylines etc...
589
- if (this.map_options.auto_adjust) {
590
- //from markers
591
- for (var i = 0; i < this.markers.length; ++i) {
592
- this.serviceBounds.extend(this.markers[i].serviceObject.position);
593
- }
594
- //from polygons:
595
- for (var i = 0; i < this.polylines.length; ++i) {
596
- this.polylines[i].serviceObject.latLngs.forEach(function(obj1){ obj1.forEach(function(obj2){ Gmaps4Rails.serviceBounds.extend(obj2);} );});
597
- }
598
- //from polylines:
599
- for (var i = 0; i < this.polygons.length; ++i) {
600
- this.polygons[i].serviceObject.latLngs.forEach(function(obj1){ obj1.forEach(function(obj2){ Gmaps4Rails.serviceBounds.extend(obj2);} );});
601
- }
602
- //from circles
603
- for (var i = 0; i < this.circles.length; ++i) {
604
- this.serviceBounds.extend(this.circles[i].serviceObject.getBounds().getNorthEast());
605
- this.serviceBounds.extend(this.circles[i].serviceObject.getBounds().getSouthWest());
606
- }
607
- }
608
- //in every case, I've to take into account the bounds set up by the user
609
- for (var i = 0; i < this.map_options.bounds.length; ++i) {
610
- //create points from bounds provided
611
- var bound = Gmaps4Rails.createLatLng(this.map_options.bounds[i].lat, this.map_options.bounds[i].lng);
612
- this.serviceBounds.extend(bound);
613
- }
614
-
615
- //SECOND_STEP: ajust the map to the bounds
616
- if (this.map_options.auto_adjust || this.map_options.bounds.length > 0) {
617
-
618
- //if autozoom is false, take user info into account
619
- if(!this.map_options.auto_zoom) {
620
- var map_center = this.serviceBounds.getCenter();
621
- this.map_options.center_longitude = map_center.lat();
622
- this.map_options.center_latitude = map_center.lng();
623
- this.map.setCenter(map_center);
624
- }
625
- else {
626
- this.map.fitBounds(this.serviceBounds);
627
- }
628
- }
629
- },
630
-
631
- ////////////////////////////////////////////////////
632
- /////////////// Abstracting API calls //////////////
633
- //(for maybe an extension to another map provider)//
634
- //////////////////mocks created/////////////////////
635
-
636
- clearMarker: function(marker) {
637
- marker.serviceObject.setMap(null);
638
- },
639
-
640
- showMarker: function(marker) {
641
- marker.serviceObject.setVisible(true);
642
- },
643
-
644
- hideMarker: function(marker) {
645
- marker.serviceObject.setVisible(false);
646
- },
647
-
648
- createPoint: function(lat, lng){
649
- return new google.maps.Point(lat, lng);
650
- },
651
-
652
- createLatLng: function(lat, lng){
653
- return new google.maps.LatLng(lat, lng);
411
+ resetSidebarContent: function(){
412
+ if (this.markers_conf.list_container !== null ){
413
+ var ul = document.getElementById(this.markers_conf.list_container);
414
+ ul.innerHTML = "";
415
+ }
654
416
  },
655
417
 
656
- createLatLngBounds: function(){
657
- return new google.maps.LatLngBounds();
658
- },
418
+ ////////////////////////////////////////////////////
419
+ ////////////////// MISCELLANEOUS ///////////////////
420
+ ////////////////////////////////////////////////////
659
421
 
660
- createMap: function(){
661
- return new google.maps.Map(document.getElementById(Gmaps4Rails.map_options.id), {
662
- maxZoom: Gmaps4Rails.map_options.maxZoom,
663
- minZoom: Gmaps4Rails.map_options.minZoom,
664
- zoom: Gmaps4Rails.map_options.zoom,
665
- center: Gmaps4Rails.createLatLng(this.map_options.center_latitude, this.map_options.center_longitude),
666
- mapTypeId: google.maps.MapTypeId[this.map_options.type],
667
- mapTypeControl: Gmaps4Rails.map_options.mapTypeControl,
668
- disableDefaultUI: Gmaps4Rails.map_options.disableDefaultUI,
669
- disableDoubleClickZoom: Gmaps4Rails.map_options.disableDoubleClickZoom,
670
- draggable: Gmaps4Rails.map_options.draggable
671
- });
672
- },
422
+ //to make the map fit the different LatLng points
423
+ adjustMapToBounds: function(latlng) {
424
+
425
+ //FIRST_STEP: retrieve all bounds
426
+ //create the bounds object only if necessary
427
+ if (this.map_options.auto_adjust || this.map_options.bounds !== null) {
428
+ this.boundsObject = Gmaps4Rails.createLatLngBounds();
429
+ }
430
+
431
+ //if autodjust is true, must get bounds from markers polylines etc...
432
+ if (this.map_options.auto_adjust) {
433
+ //from markers
434
+ Gmaps4Rails.extendBoundsWithMarkers();
673
435
 
674
- createMarkerImage: function(markerPicture, markerSize, origin, anchor, scaledSize) {
675
- return new google.maps.MarkerImage(markerPicture, markerSize, origin, anchor, scaledSize);
436
+ //from polygons:
437
+ for (var i = 0; i < this.polylines.length; ++i) {
438
+ this.polylines[i].serviceObject.latLngs.forEach(function(obj1){ obj1.forEach(function(obj2){ Gmaps4Rails.boundsObject.extend(obj2);} );});
439
+ }
440
+ //from polylines:
441
+ for (var i = 0; i < this.polygons.length; ++i) {
442
+ this.polygons[i].serviceObject.latLngs.forEach(function(obj1){ obj1.forEach(function(obj2){ Gmaps4Rails.boundsObject.extend(obj2);} );});
443
+ }
444
+ //from circles
445
+ for (var i = 0; i < this.circles.length; ++i) {
446
+ this.boundsObject.extend(this.circles[i].serviceObject.getBounds().getNorthEast());
447
+ this.boundsObject.extend(this.circles[i].serviceObject.getBounds().getSouthWest());
448
+ }
449
+ }
450
+ //in every case, I've to take into account the bounds set up by the user
451
+ for (var i = 0; i < this.map_options.bounds.length; ++i) {
452
+ //create points from bounds provided
453
+ var bound = Gmaps4Rails.createLatLng(this.map_options.bounds[i].lat, this.map_options.bounds[i].lng);
454
+ this.boundsObject.extend(bound);
455
+ }
456
+
457
+ //SECOND_STEP: ajust the map to the bounds
458
+ if (this.map_options.auto_adjust || this.map_options.bounds.length > 0) {
459
+
460
+ //if autozoom is false, take user info into account
461
+ if(!this.map_options.auto_zoom) {
462
+ var map_center = this.boundsObject.getCenter();
463
+ this.map_options.center_longitude = map_center.lat();
464
+ this.map_options.center_latitude = map_center.lng();
465
+ this.map.setCenter(map_center);
466
+ }
467
+ else {
468
+ Gmaps4Rails.fitBounds();
469
+ }
470
+ }
676
471
  },
677
472
 
678
- createMarker: function(args){
679
- return new google.maps.Marker(args);
473
+
474
+ ////////////////////////////////////////////////////
475
+ ///////////////// Basic functions //////////////////
476
+ ///////////////////tests coded//////////////////////
477
+
478
+ //basic function to check existence of a variable
479
+ exists: function(var_name) {
480
+ return (var_name !== "" && typeof var_name !== "undefined");
680
481
  },
681
482
 
682
- createSize: function(width, height){
683
- return new google.maps.Size(width, height);
483
+ //randomize
484
+ randomize: function(Lat0, Lng0) {
485
+ //distance in meters between 0 and max_random_distance (positive or negative)
486
+ var dx = this.markers_conf.max_random_distance * this.random();
487
+ var dy = this.markers_conf.max_random_distance * this.random();
488
+ var Lat = parseFloat(Lat0) + (180/Math.PI)*(dy/6378137);
489
+ var Lng = parseFloat(Lng0) + ( 90/Math.PI)*(dx/6378137)/Math.cos(Lat0);
490
+ return [Lat, Lng];
684
491
  },
685
492
 
686
- createClusterer: function(markers_array){
687
- return new MarkerClusterer( Gmaps4Rails.map,
688
- markers_array,
689
- { maxZoom: this.markers_conf.clusterer_maxZoom, gridSize: this.markers_conf.clusterer_gridSize, styles: Gmaps4Rails.customClusterer() }
690
- );
691
- },
493
+ //gives a value between -1 and 1
494
+ random: function() { return(Math.random() * 2 -1); }
692
495
 
693
- clearClusterer: function() {
694
- this.markerClusterer.clearMarkers();
695
- },
696
-
697
- //checks if obj is included in arr Array and returns the position or false
698
- includeMarkerImage: function(arr, obj) {
699
- for(var i=0; i<arr.length; i++) {
700
- if (arr[i].url == obj) {return i;}
701
- }
702
- return false;
703
- },
704
-
705
- ////////////////////////////////////////////////////
706
- ///////////////// Basic functions //////////////////
707
- ///////////////////tests coded//////////////////////
708
-
709
- //basic function to check existence of a variable
710
- exists: function(var_name) {
711
- return (var_name !== "" && typeof var_name !== "undefined");
712
- },
713
-
714
- //randomize
715
- randomize: function(Lat0, Lng0) {
716
- //distance in meters between 0 and max_random_distance (positive or negative)
717
- var dx = this.markers_conf.max_random_distance * this.random();
718
- var dy = this.markers_conf.max_random_distance * this.random();
719
- var Lat = parseFloat(Lat0) + (180/Math.PI)*(dy/6378137);
720
- var Lng = parseFloat(Lng0) + ( 90/Math.PI)*(dx/6378137)/Math.cos(Lat0);
721
- return [Lat, Lng];
722
- },
723
-
724
- //gives a value between -1 and 1
725
- random: function() { return(Math.random() * 2 -1); }
726
-
727
496
  };