gmaps4rails 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,163 @@
1
+ ######################################################################################################
2
+ ############################################## Bing Maps ##########################################
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 @Gmaps4RailsBing extends Gmaps4Rails
10
+
11
+ constructor: ->
12
+ super
13
+ @map_options =
14
+ type: "road" # aerial, auto, birdseye, collinsBart, mercator, ordnanceSurvey, road
15
+ @markers_conf =
16
+ infobox: "description" #description or htmlContent
17
+
18
+ @mergeWithDefault("map_options")
19
+ @mergeWithDefault("markers_conf")
20
+
21
+ #////////////////////////////////////////////////////
22
+ #/////////////// Basic Objects //////////////
23
+ #////////////////////////////////////////////////////
24
+
25
+ getMapType: ->
26
+ switch @map_options.type
27
+ when "road" then return Microsoft.Maps.MapTypeId.road
28
+ when "aerial" then return Microsoft.Maps.MapTypeId.aerial
29
+ when "auto" then return Microsoft.Maps.MapTypeId.auto
30
+ when "birdseye" then return Microsoft.Maps.MapTypeId.birdseye
31
+ when "collinsBart" then return Microsoft.Maps.MapTypeId.collinsBart
32
+ when "mercator" then return Microsoft.Maps.MapTypeId.mercator
33
+ when "ordnanceSurvey" then return Microsoft.Maps.MapTypeId.ordnanceSurvey
34
+ else return Microsoft.Maps.MapTypeId.auto
35
+
36
+ createPoint: (lat, lng) ->
37
+ return new Microsoft.Maps.Point(lat, lng)
38
+
39
+ createLatLng:(lat, lng) ->
40
+ return new Microsoft.Maps.Location(lat, lng)
41
+
42
+ createLatLngBounds: ->
43
+
44
+ createMap: ->
45
+ return new Microsoft.Maps.Map(document.getElementById(@map_options.id), {
46
+ credentials: @map_options.provider_key,
47
+ mapTypeId: @getMapType(),
48
+ center: @createLatLng(@map_options.center_latitude, @map_options.center_longitude),
49
+ zoom: @map_options.zoom
50
+ })
51
+
52
+ createSize: (width, height) ->
53
+ return new google.maps.Size(width, height)
54
+
55
+ #////////////////////////////////////////////////////
56
+ #////////////////////// Markers /////////////////////
57
+ #////////////////////////////////////////////////////
58
+
59
+ createMarker: (args) ->
60
+ markerLatLng = @createLatLng(args.Lat, args.Lng)
61
+ marker
62
+ #// Marker sizes are expressed as a Size of X,Y
63
+ if args.marker_picture == ""
64
+ marker = new Microsoft.Maps.Pushpin(@createLatLng(args.Lat, args.Lng), {
65
+ draggable: args.marker_draggable,
66
+ anchor: @createImageAnchorPosition(args.Lat, args.Lng),
67
+ text: args.marker_title
68
+ }
69
+ );
70
+ else
71
+ marker = new Microsoft.Maps.Pushpin(@createLatLng(args.Lat, args.Lng), {
72
+ draggable: args.marker_draggable,
73
+ anchor: @createImageAnchorPosition(args.Lat, args.Lng),
74
+ icon: args.marker_picture,
75
+ height: args.marker_height,
76
+ text: args.marker_title,
77
+ width: args.marker_width
78
+ }
79
+ );
80
+ @addToMap(marker)
81
+ return marker
82
+
83
+ #// clear markers
84
+ clearMarkers: ->
85
+ for marker in @markers
86
+ @clearMarker marker
87
+
88
+ clearMarker: (marker) ->
89
+ @removeFromMap(marker.serviceObject)
90
+
91
+ #//show and hide markers
92
+ showMarkers: ->
93
+ for marker in @markers
94
+ @showMarker marker
95
+
96
+ showMarker: (marker) ->
97
+ marker.serviceObject.setOptions({ visible: true })
98
+
99
+ hideMarkers: ->
100
+ for marker in @markers
101
+ @hideMarker marker
102
+
103
+ hideMarker: (marker) ->
104
+ marker.serviceObject.setOptions({ visible: false })
105
+
106
+ extendBoundsWithMarkers: ->
107
+ locationsArray = []
108
+ for marker in @markers
109
+ locationsArray.push(marker.serviceObject.getLocation())
110
+ @boundsObject = Microsoft.Maps.LocationRect.fromLocations(locationsArray)
111
+
112
+ #////////////////////////////////////////////////////
113
+ #/////////////////// Clusterer //////////////////////
114
+ #////////////////////////////////////////////////////
115
+
116
+ createClusterer: (markers_array) ->
117
+
118
+ clearClusterer: ->
119
+
120
+ #//creates clusters
121
+ clusterize: ->
122
+
123
+ #////////////////////////////////////////////////////
124
+ #/////////////////// INFO WINDOW ////////////////////
125
+ #////////////////////////////////////////////////////
126
+
127
+ #// creates infowindows
128
+ createInfoWindow: (marker_container) ->
129
+ if marker_container.description?
130
+ #//create the infowindow
131
+ if @markers_conf.infobox == "description"
132
+ marker_container.info_window = new Microsoft.Maps.Infobox(marker_container.serviceObject.getLocation(), { description: marker_container.description, visible: false, showCloseButton: true})
133
+ else
134
+ marker_container.info_window = new Microsoft.Maps.Infobox(marker_container.serviceObject.getLocation(), { htmlContent: marker_container.description, visible: false})
135
+
136
+ #//add the listener associated
137
+ currentMap = this
138
+ Microsoft.Maps.Events.addHandler(marker_container.serviceObject, 'click', @openInfoWindow(currentMap, marker_container.info_window))
139
+ @addToMap(marker_container.info_window)
140
+
141
+ openInfoWindow: (currentMap, infoWindow) ->
142
+ return ->
143
+ # Close the latest selected marker before opening the current one.
144
+ if currentMap.visibleInfoWindow
145
+ currentMap.visibleInfoWindow.setOptions({ visible: false })
146
+ infoWindow.setOptions({ visible:true })
147
+ currentMap.visibleInfoWindow = infoWindow
148
+
149
+ #////////////////////////////////////////////////////
150
+ #/////////////////// Other methods //////////////////
151
+ #////////////////////////////////////////////////////
152
+
153
+ fitBounds: ->
154
+ @map.setView({bounds: @boundsObject})
155
+
156
+ addToMap: (object)->
157
+ @map.entities.push(object)
158
+
159
+ removeFromMap: (object)->
160
+ @map.entities.remove(object)
161
+
162
+ centerMapOnUser: ->
163
+ @map.setView({ center: @userLocation})
@@ -0,0 +1,253 @@
1
+ #######################################################################################################
2
+ ############################################## Google maps ##########################################
3
+ #######################################################################################################
4
+
5
+ class @Gmaps4RailsGoogle extends Gmaps4Rails
6
+
7
+ constructor: ->
8
+ super
9
+ #Map settings
10
+ @map_options =
11
+ disableDefaultUI: false
12
+ disableDoubleClickZoom: false
13
+ type: "ROADMAP" # HYBRID, ROADMAP, SATELLITE, TERRAIN
14
+
15
+ #markers + info styling
16
+ @markers_conf =
17
+ clusterer_gridSize: 50
18
+ clusterer_maxZoom: 5
19
+ custom_cluster_pictures: null
20
+ custom_infowindow_class: null
21
+
22
+ @mergeWithDefault("map_options")
23
+ @mergeWithDefault("markers_conf")
24
+
25
+ @kml_options =
26
+ clickable: true
27
+ preserveViewport: false
28
+ suppressInfoWindows: false
29
+
30
+ #Polygon Styling
31
+ @polygons_conf = # default style for polygons
32
+ strokeColor: "#FFAA00"
33
+ strokeOpacity: 0.8
34
+ strokeWeight: 2
35
+ fillColor: "#000000"
36
+ fillOpacity: 0.35
37
+
38
+ #Polyline Styling
39
+ @polylines_conf = #default style for polylines
40
+ strokeColor: "#FF0000"
41
+ strokeOpacity: 1
42
+ strokeWeight: 2
43
+
44
+ #Circle Styling
45
+ @circles_conf = #default style for circles
46
+ fillColor: "#00AAFF"
47
+ fillOpacity: 0.35
48
+ strokeColor: "#FFAA00"
49
+ strokeOpacity: 0.8
50
+ strokeWeight: 2
51
+ clickable: false
52
+ zIndex: null
53
+
54
+ #Direction Settings
55
+ @direction_conf =
56
+ panel_id: null
57
+ display_panel: false
58
+ origin: null
59
+ destination: null
60
+ waypoints: [] #[{location: "toulouse,fr", stopover: true}, {location: "Clermont-Ferrand, fr", stopover: true}]
61
+ optimizeWaypoints: false
62
+ unitSystem: "METRIC" #IMPERIAL
63
+ avoidHighways: false
64
+ avoidTolls: false
65
+ region: null
66
+ travelMode: "DRIVING" #WALKING, BICYCLING
67
+
68
+ #////////////////////////////////////////////////////
69
+ #/////////////// Basic Objects //////////////
70
+ #////////////////////////////////////////////////////
71
+
72
+ createPoint : (lat, lng) ->
73
+ return new google.maps.Point(lat, lng)
74
+
75
+ createLatLng : (lat, lng) ->
76
+ return new google.maps.LatLng(lat, lng)
77
+
78
+ createLatLngBounds : ->
79
+ return new google.maps.LatLngBounds()
80
+
81
+ createMap : ->
82
+ return new google.maps.Map document.getElementById(@map_options.id), {
83
+ maxZoom: @map_options.maxZoom
84
+ minZoom: @map_options.minZoom
85
+ zoom: @map_options.zoom
86
+ center: @createLatLng(@map_options.center_latitude, @map_options.center_longitude)
87
+ mapTypeId: google.maps.MapTypeId[@map_options.type]
88
+ mapTypeControl: @map_options.mapTypeControl
89
+ disableDefaultUI: @map_options.disableDefaultUI
90
+ disableDoubleClickZoom: @map_options.disableDoubleClickZoom
91
+ draggable: @map_options.draggable
92
+ }
93
+
94
+
95
+ createMarkerImage : (markerPicture, markerSize, origin, anchor, scaledSize) ->
96
+ return new google.maps.MarkerImage(markerPicture, markerSize, origin, anchor, scaledSize)
97
+
98
+ createSize : (width, height) ->
99
+ return new google.maps.Size(width, height)
100
+
101
+ #////////////////////////////////////////////////////
102
+ #////////////////////// Markers /////////////////////
103
+ #////////////////////////////////////////////////////
104
+
105
+ createMarker : (args) ->
106
+ markerLatLng = @createLatLng(args.Lat, args.Lng)
107
+
108
+ #Marker sizes are expressed as a Size of X,Y
109
+ if args.marker_picture and args.rich_marker == null
110
+ return new google.maps.Marker({position: markerLatLng, map: @map, title: args.marker_title, draggable: args.marker_draggable})
111
+
112
+ else if (args.rich_marker != null)
113
+ return new RichMarker({
114
+ position: markerLatLng
115
+ map: @this.map
116
+ draggable: args.marker_draggable
117
+ content: args.rich_marker
118
+ flat: if args.marker_anchor == null then false else args.marker_anchor[1]
119
+ anchor: if args.marker_anchor == null then 0 else args.marker_anchor[0]
120
+ })
121
+
122
+ else
123
+ #calculate MarkerImage anchor location
124
+ imageAnchorPosition = @createImageAnchorPosition args.marker_anchor
125
+ shadowAnchorPosition = @createImageAnchorPosition args.shadow_anchor
126
+
127
+ #create or retrieve existing MarkerImages
128
+ markerImage = @createOrRetrieveImage(args.marker_picture, args.marker_width, args.marker_height, imageAnchorPosition)
129
+ shadowImage = @createOrRetrieveImage(args.shadow_picture, args.shadow_width, args.shadow_height, shadowAnchorPosition)
130
+ return new google.maps.Marker({position: markerLatLng, map: this.map, icon: markerImage, title: args.marker_title, draggable: args.marker_draggable, shadow: shadowImage})
131
+
132
+ #checks if obj is included in arr Array and returns the position or false
133
+ includeMarkerImage : (arr, obj) ->
134
+ for object, index in arr
135
+ return index if object.url == obj
136
+ return false
137
+
138
+ #checks if MarkerImage exists before creating a new one
139
+ #returns a MarkerImage or false if ever something wrong is passed as argument
140
+ createOrRetrieveImage : (currentMarkerPicture, markerWidth, markerHeight, imageAnchorPosition) ->
141
+ return null if (currentMarkerPicture == "" or currentMarkerPicture == null )
142
+
143
+ test_image_index = @includeMarkerImage(@markerImages, currentMarkerPicture)
144
+ switch test_image_index
145
+ when false
146
+ markerImage = @createMarkerImage(currentMarkerPicture, @createSize(markerWidth, markerHeight), null, imageAnchorPosition, null )
147
+ @markerImages.push(markerImage)
148
+ return markerImage
149
+ break
150
+ else
151
+ return @markerImages[test_image_index] if typeof test_image_index == 'number'
152
+ return false
153
+
154
+ #clear markers
155
+ clearMarkers : ->
156
+ for marker in @markers
157
+ @clearMarker marker
158
+
159
+ #show and hide markers
160
+ showMarkers : ->
161
+ for marker in @markers
162
+ @showMarker marker
163
+
164
+ hideMarkers : ->
165
+ for marker in @markers
166
+ @hideMarker marker
167
+
168
+ clearMarker : (marker) ->
169
+ marker.serviceObject.setMap(null)
170
+
171
+ showMarker : (marker) ->
172
+ marker.serviceObject.setVisible(true)
173
+
174
+ hideMarker : (marker) ->
175
+ marker.serviceObject.setVisible(false)
176
+
177
+ extendBoundsWithMarkers : ->
178
+ for marker in @markers
179
+ @boundsObject.extend(marker.serviceObject.position)
180
+
181
+ #////////////////////////////////////////////////////
182
+ #/////////////////// Clusterer //////////////////////
183
+ #////////////////////////////////////////////////////
184
+
185
+ createClusterer : (markers_array) ->
186
+ return new MarkerClusterer( @map, markers_array, { maxZoom: this.markers_conf.clusterer_maxZoom, gridSize: @markers_conf.clusterer_gridSize, styles: @customClusterer() })
187
+
188
+ clearClusterer : ->
189
+ @markerClusterer.clearMarkers()
190
+
191
+ #creates clusters
192
+ clusterize : ->
193
+ if @markers_conf.do_clustering == true
194
+ #first clear the existing clusterer if any
195
+ @clearClusterer() if @markerClusterer != null
196
+
197
+ markers_array = new Array
198
+ for marker in @markers
199
+ markers_array.push(marker.serviceObject)
200
+
201
+ @markerClusterer = @createClusterer(markers_array)
202
+
203
+ #////////////////////////////////////////////////////
204
+ #/////////////////// INFO WINDOW ////////////////////
205
+ #////////////////////////////////////////////////////
206
+
207
+ #// creates infowindows
208
+ createInfoWindow : (marker_container) ->
209
+ if @markers_conf.custom_infowindow_class == null && marker_container.description?
210
+ #create the infowindow
211
+ info_window = new google.maps.InfoWindow({content: marker_container.description })
212
+ #add the listener associated
213
+ currentMap = this
214
+ google.maps.event.addListener(marker_container.serviceObject, 'click', @openInfoWindow(currentMap, info_window, marker_container.serviceObject))
215
+ else #creating custom infowindow
216
+ if marker_container.description?
217
+ boxText = document.createElement("div")
218
+ boxText.setAttribute("class", @markers_conf.custom_infowindow_class) #to customize
219
+ boxText.innerHTML = marker_container.description
220
+ info_window = new InfoBox(@infobox(boxText))
221
+ currentMap = this
222
+ google.maps.event.addListener(marker_container.serviceObject, 'click', @openInfoWindow(currentMap, info_window, marker_container.serviceObject))
223
+
224
+
225
+ openInfoWindow : (currentMap, infoWindow, marker) ->
226
+ return ->
227
+ # Close the latest selected marker before opening the current one.
228
+ currentMap.visibleInfoWindow.close() if currentMap.visibleInfoWindow != null
229
+ infoWindow.open(currentMap.map, marker)
230
+ currentMap.visibleInfoWindow = infoWindow
231
+
232
+ #////////////////////////////////////////////////////
233
+ #///////////////// KML //////////////////
234
+ #////////////////////////////////////////////////////
235
+
236
+ createKmlLayer : (kml) ->
237
+ kml_options = kml.options || {}
238
+ kml_options = @mergeObjectWithDefault(kml_options, @kml_options)
239
+ kml = new google.maps.KmlLayer( kml.url, kml_options)
240
+ kml.setMap(@map)
241
+ return kml
242
+
243
+
244
+ #////////////////////////////////////////////////////
245
+ #/////////////////// Other methods //////////////////
246
+ #////////////////////////////////////////////////////
247
+
248
+ fitBounds : ->
249
+ @map.fitBounds(@boundsObject)
250
+
251
+ centerMapOnUser : ->
252
+ @map.setCenter(@userLocation)
253
+
@@ -0,0 +1,134 @@
1
+ #######################################################################################################
2
+ ############################################## Map Quest #############################################
3
+ #######################################################################################################
4
+ # http://www.mapquestapi.com/sdk/js/v6.0.0/poi.html
5
+
6
+ class @Gmaps4RailsMapquest extends Gmaps4Rails
7
+
8
+ constructor: ->
9
+ super
10
+ #Map settings
11
+ @map_options = {type: "map"} #map type (map, sat, hyb)
12
+ @markers_conf = {}
13
+ @mergeWithDefault "markers_conf"
14
+ @mergeWithDefault "map_options"
15
+
16
+ #////////////////////////////////////////////////////
17
+ #/////////////// Basic Objects //////////////
18
+ #////////////////////////////////////////////////////
19
+
20
+ createPoint: (lat, lng) ->
21
+ return new MQA.Poi({lat: lat, lng: lng})
22
+
23
+ createLatLng: (lat, lng) ->
24
+ return {lat: lat, lng: lng}
25
+
26
+ createLatLngBounds: ->
27
+
28
+ createMap: ->
29
+ map = new MQA.TileMap( #// Constructs an instance of MQA.TileMap
30
+ document.getElementById(@map_options.id), #//the id of the element on the page you want the map to be added into
31
+ @map_options.zoom, #//intial zoom level of the map
32
+ {lat: @map_options.center_latitude, lng: @map_options.center_longitude},
33
+ @map_options.type) #//map type (map, sat, hyb)
34
+
35
+ MQA.withModule('zoomcontrol3', (->
36
+ map.addControl(
37
+ new MQA.LargeZoomControl3(),
38
+ new MQA.MapCornerPlacement(MQA.MapCorner.TOP_LEFT)
39
+ )
40
+ ))
41
+ return map
42
+
43
+ createMarkerImage: (markerPicture, markerSize, origin, anchor, scaledSize) ->
44
+
45
+ #////////////////////////////////////////////////////
46
+ #////////////////////// Markers /////////////////////
47
+ #////////////////////////////////////////////////////
48
+
49
+ createMarker: (args)->
50
+ marker = new MQA.Poi( {lat: args.Lat, lng: args.Lng} )
51
+
52
+ if args.marker_picture != ""
53
+ icon = new MQA.Icon(args.marker_picture, args.marker_height, args.marker_width)
54
+ marker.setIcon(icon)
55
+ if args.marker_anchor != null
56
+ marker.setBias({x: args.marker_anchor[0], y: args.marker_anchor[1]})
57
+
58
+ if args.shadow_picture != ""
59
+ icon = new MQA.Icon(args.shadow_picture, args.shadow_height, args.shadow_width)
60
+ marker.setShadow(icon)
61
+
62
+ if args.shadow_anchor != null
63
+ marker.setShadowOffset({x: args.shadow_anchor[0], y: args.shadow_anchor[1]})
64
+
65
+ @addToMap marker
66
+ return marker
67
+
68
+
69
+ #// clear markers
70
+ clearMarkers: ->
71
+ for marker in markers
72
+ @clearMarker marker
73
+
74
+ #//show and hide markers
75
+ showMarkers: ->
76
+ for marker in markers
77
+ @showMarker marker
78
+
79
+ hideMarkers: ->
80
+ for marker in markers
81
+ @hideMarker marker
82
+
83
+ clearMarker: (marker) ->
84
+ @removeFromMap(marker.serviceObject)
85
+
86
+ showMarker: (marker) ->
87
+ #// marker.serviceObject
88
+
89
+ hideMarker: (marker) ->
90
+ #// marker.serviceObject
91
+
92
+ extendBoundsWithMarkers: ->
93
+ if @markers.length >=2
94
+ @boundsObject = new MQA.RectLL(@markers[0].serviceObject.latLng, @markers[1].serviceObject.latLng)
95
+ for marker in @markers
96
+ @boundsObject.extend marker.serviceObject.latLng
97
+
98
+ #////////////////////////////////////////////////////
99
+ #/////////////////// Clusterer //////////////////////
100
+ #////////////////////////////////////////////////////
101
+
102
+ createClusterer: (markers_array) ->
103
+
104
+ clearClusterer: ->
105
+
106
+ #//creates clusters
107
+ clusterize: ->
108
+
109
+ #////////////////////////////////////////////////////
110
+ #/////////////////// INFO WINDOW ////////////////////
111
+ #////////////////////////////////////////////////////
112
+
113
+ #// creates infowindows
114
+ createInfoWindow: (marker_container) ->
115
+ marker_container.serviceObject.setInfoTitleHTML(marker_container.description)
116
+ #//TODO: how to disable the mouseover display when using setInfoContentHTML?
117
+ #//marker_container.serviceObject.setInfoContentHTML(marker_container.description);
118
+
119
+ #////////////////////////////////////////////////////
120
+ #/////////////////// Other methods //////////////////
121
+ #////////////////////////////////////////////////////
122
+
123
+ fitBounds: ->
124
+ @map.zoomToRect @boundsObject if @markers.length >=2
125
+ @map.setCenter @markers[0].serviceObject.latLng if @markers.length == 1
126
+
127
+ centerMapOnUser: ->
128
+ @map.setCenter @userLocation
129
+
130
+ addToMap: (object) ->
131
+ @map.addShape object
132
+
133
+ removeFromMap: (object)->
134
+ @map.removeShape object