Class: GoogleMaps::MainController
- Inherits:
-
ModelController
- Object
- 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
- #geocode(address) {|Native(`latlng`)| ... } ⇒ Object
- #index_ready ⇒ Object
- #index_removed ⇒ Object
- #remove_marker(marker) ⇒ Object
- #set_center(address) ⇒ Object
- #set_markers ⇒ Object
- #set_zoom ⇒ Object
- #setup_map(node, latlng) ⇒ Object
- #setup_zoom ⇒ 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
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'app/google-maps/controllers/main_controller.rb', line 156 def add_marker(marker_data) if marker_data.is_a?(String) address = marker_data content = marker_data else address = marker_data._address.or('').to_n content = marker_data._content.or(address).or('').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 |
#geocode(address) {|Native(`latlng`)| ... } ⇒ Object
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 139 def geocode(address) unless address.is_a?(String) yield({lat: -34.397, lng: 150.644}) return end `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 30 31 32 |
# 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) geocode(attrs.center) do |latlng| setup_map(node, latlng.to_n) end else setup_map(node, {lat: -34.397, lng: 150.644}.to_n) end if attrs.respond_to?(:zoom) set_zoom end if attrs.respond_to?(:markers) set_markers end end |
#index_removed ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'app/google-maps/controllers/main_controller.rb', line 35 def index_removed %x{ delete this.map; delete this.geocoder; } @add_listener.remove if @add_listener @remove_listener.remove if @remove_listener end |
#remove_marker(marker) ⇒ Object
181 182 183 |
# File 'app/google-maps/controllers/main_controller.rb', line 181 def remove_marker(marker) `marker.setMap(null);` end |
#set_center(address) ⇒ Object
128 129 130 131 132 133 134 135 136 137 |
# File 'app/google-maps/controllers/main_controller.rb', line 128 def set_center(address) if @first @first = false else geocode(address) do |latlng| latlng_n = latlng.to_n `self.map.setCenter(latlng_n);` end end end |
#set_markers ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/google-maps/controllers/main_controller.rb', line 45 def set_markers -> do markers = attrs.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] puts "ADD MARKER AT: #{index}" add_marker(marker) do |result| @markers[index] = result end end @remove_listener = markers.on('removed') do |index| puts "DELETE AT INDEX: #{index}" marker = @markers.delete_at(index) `console.log('marker: ', marker);` remove_marker(marker.to_n) end end end.watch! end |
#set_zoom ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'app/google-maps/controllers/main_controller.rb', line 94 def set_zoom -> do level = attrs.zoom if level.blank? level = 8 else level = (level || 8).to_i end puts "SET LEVEL: #{level.inspect}" level_n = level.to_n `if (self.map.getZoom() != level_n) {` `self.map.setZoom(level_n);` `}` end.watch! end |
#setup_map(node, latlng) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'app/google-maps/controllers/main_controller.rb', line 111 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); } if attrs.respond_to?(:center) @first = true -> { set_center(attrs.center) }.watch! end setup_zoom if attrs.respond_to?(:zoom) end |
#setup_zoom ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/google-maps/controllers/main_controller.rb', line 79 def setup_zoom `google.maps.event.addListener(self.map, 'zoom_changed', function() { var zoomLevel = self.map.getZoom();` new_zoom = Native(`zoomLevel`) if attrs.zoom != new_zoom && attrs.respond_to?(:zoom=) attrs.zoom = new_zoom end # Setup listener again set_zoom `});` end |