Class: GoogleMaps::MainController

Inherits:
Volt::ModelController
  • Object
show all
Defined in:
app/google-maps/controllers/main_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sectionObject

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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/google-maps/controllers/main_controller.rb', line 161

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

Yields:

  • (Native(`latlng`))


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/google-maps/controllers/main_controller.rb', line 144

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_readyObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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
end

#index_removedObject



26
27
28
29
30
31
32
33
34
# File 'app/google-maps/controllers/main_controller.rb', line 26

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



186
187
188
# File 'app/google-maps/controllers/main_controller.rb', line 186

def remove_marker(marker)
  `marker.setMap(null);`
end

#set_center(address) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'app/google-maps/controllers/main_controller.rb', line 133

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_markersObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/google-maps/controllers/main_controller.rb', line 36

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_zoomObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/google-maps/controllers/main_controller.rb', line 90

def set_zoom
  -> do
    attrs.zoom
    puts "SET 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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/google-maps/controllers/main_controller.rb', line 110

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);
    console.log('map setup');
  }

  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_zoomObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/google-maps/controllers/main_controller.rb', line 70

def setup_zoom
  puts "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