Class: GoogleMaps::MainController
- Inherits:
-
Volt::ModelController
- Object
- Volt::ModelController
- GoogleMaps::MainController
- Defined in:
- app/google_maps/controllers/main_controller.rb
Instance Attribute Summary collapse
-
#section ⇒ Object
Returns the value of attribute section.
Instance Method Summary collapse
- #add_marker(marker_data) ⇒ Object
- #before_index_remove ⇒ Object
- #geocode(address) {|Native(`latlng`)| ... } ⇒ Object
- #index_ready ⇒ Object
- #remove_marker(marker) ⇒ Object
- #set_center(address) ⇒ Object
- #set_markers ⇒ Object
- #set_zoom ⇒ Object
- #setup_map(node, latlng) ⇒ Object
- #setup_zoom ⇒ Object
- #track_markers(markers) ⇒ Object
Instance Attribute Details
#section ⇒ Object
Returns the value of attribute section.
7 8 9 |
# File 'app/google_maps/controllers/main_controller.rb', line 7 def section @section end |
Instance Method Details
#add_marker(marker_data) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'app/google_maps/controllers/main_controller.rb', line 189 def add_marker(marker_data) if marker_data.is_a?(String) address = marker_data content = marker_data else address = (marker_data._address || '').to_n content = (marker_data._content || address || '').to_n end geocode(address) do |latlng| latlng_n = latlng.to_n marker = nil %x{ marker = new google.maps.Marker({ position: latlng_n, map: self.map, title: content }); } yield Native(marker) end end |
#before_index_remove ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'app/google_maps/controllers/main_controller.rb', line 32 def before_index_remove @center_watch.stop if @center_watch %x{ delete this.map; delete this.geocoder; } @add_listener.remove if @add_listener @remove_listener.remove if @remove_listener end |
#geocode(address) {|Native(`latlng`)| ... } ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'app/google_maps/controllers/main_controller.rb', line 169 def geocode(address) unless address.is_a?(String) yield({lat: -34.397, lng: 150.644}) return end # needed for some reason, sometimes strings come in weird `address = address + "";` `this.geocoder.geocode( { 'address': address}, function(results, status) {` `if (status == google.maps.GeocoderStatus.OK) {` `var latlng = results[0].geometry.location;` yield(Native(`latlng`)) `} else {` yield({lat: -34.397, lng: 150.644}) `}` `}.bind(this));` end |
#index_ready ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/google_maps/controllers/main_controller.rb', line 9 def index_ready @markers = [] `this.geocoder = new google.maps.Geocoder();` node = section.container_node if attrs.respond_to?(:center) @center_watch = proc do attrs.center.then do |center| if center geocode(center) do |latlng| setup_map(node, latlng.to_n) end end end end.watch! else setup_map(node, {lat: -34.397, lng: 150.644}.to_n) end end |
#remove_marker(marker) ⇒ Object
214 215 216 |
# File 'app/google_maps/controllers/main_controller.rb', line 214 def remove_marker(marker) `marker.setMap(null);` end |
#set_center(address) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/google_maps/controllers/main_controller.rb', line 156 def set_center(address) if @first @first = false else geocode(address) do |latlng| latlng_n = latlng.to_n if (`!!self.map`) `self.map.setCenter(latlng_n);` end end end end |
#set_markers ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/google_maps/controllers/main_controller.rb', line 43 def set_markers -> do # clear existing markers on full re-render if @markers @markers.each do |marker| remove_marker(marker.to_n) end end @markers = [] markers = attrs.markers if markers.is_a?(Promise) markers.then do |markers| track_markers(markers) end else track_markers(markers) end end.watch! end |
#set_zoom ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'app/google_maps/controllers/main_controller.rb', line 112 def set_zoom -> do attrs.zoom unless @changing_zoom level = attrs.zoom if level.blank? level = 8 else level = (level || 8).to_i end level_n = level.to_n `if (self.map.getZoom() != level_n) {` `self.map.setZoom(level_n);` `}` end end.watch! end |
#setup_map(node, latlng) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'app/google_maps/controllers/main_controller.rb', line 131 def setup_map(node, latlng) %x{ var mapOptions = { center: latlng, zoom: 8 }; this.map = new google.maps.Map($(node).find('.google-map-instance').get(0), mapOptions); this.map.addListener('click', function(e) { self.$trigger('click', e.latLng.lat(), e.latLng.lng()); }); } if attrs.respond_to?(:center) @first = true -> { set_center(attrs.center) }.watch! end set_zoom if attrs.respond_to?(:zoom) set_markers if attrs.respond_to?(:markers) setup_zoom if attrs.respond_to?(:zoom=) end |
#setup_zoom ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'app/google_maps/controllers/main_controller.rb', line 93 def setup_zoom `google.maps.event.addListener(self.map, 'zoom_changed', function() { var zoomLevel = self.map.getZoom();` @changing_zoom = true new_zoom = Native(`zoomLevel`) if attrs.zoom != new_zoom && attrs.respond_to?(:zoom=) attrs.zoom = new_zoom end # Setup listener again set_zoom @changing_zoom = false `});` end |
#track_markers(markers) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/google_maps/controllers/main_controller.rb', line 66 def track_markers(markers) markers.each do |marker| add_marker(marker) do |result| @markers << result end end @add_listener.remove if @add_listener @remove_listener.remove if @remove_listener if markers.respond_to?(:on) @add_listener = markers.on('added') do |index| marker = markers[index] add_marker(marker) do |result| @markers[index] = result end end @remove_listener = markers.on('removed') do |index| marker = @markers.delete_at(index) remove_marker(marker.to_n) end end end |