Class: GoogleMaps::MainController

Inherits:
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



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

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`))


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

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
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_removedObject



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



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

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

#set_center(address) ⇒ Object



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

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



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_zoomObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/google-maps/controllers/main_controller.rb', line 98

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/google-maps/controllers/main_controller.rb', line 117

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_zoomObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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();`

      @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