Class: Inputmap::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/inputmap.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
# File 'lib/inputmap.rb', line 7

def initialize(config = {})
  @key = config[:key] || 'AIzaSyB70Hu6l9Ekf1hnbPKJy2sCgsxrYFfTTO4'
  @coordinates = config[:coordinates].gsub(/[()]/,'') || '-23.583233, -46.634771'
  @input_id = config[:input_id] || 'input_id'
  @element_id = config[:element_id] = 'map'
  @zoom = config[:zoom] || 15
end

Instance Method Details

#renderObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/inputmap.rb', line 15

def render
  "<script src='https://maps.googleapis.com/maps/api/js?key=#{@key}'></script>
   <script type='text/javascript'>
        var map;
    var marker;

    function initialize() {
      var my_position = new google.maps.LatLng(#{@coordinates});
        var map = new google.maps.Map(document.getElementById('#{@element_id}'), {
          center: my_position,
          zoom: #{@zoom}
      });
      var marker = new google.maps.Marker({
          position: my_position,
          map: map
      });
      // double click event
      google.maps.event.addListener(map, 'click', function(e) {
        var positionDoubleclick = e.latLng;
        marker.setPosition(positionDoubleclick);
        var input = document.getElementById('#{@input_id}');
        input.value = positionDoubleclick;
      });
    }
    google.maps.event.addDomListener(window, 'load', initialize);

   </script>".html_safe
end