Class: ActiveAdmin::Views::GoogleMapProxy

Inherits:
LatlngProxy
  • Object
show all
Defined in:
lib/activeadmin/views/activeadmin_form.rb

Instance Method Summary collapse

Methods inherited from LatlngProxy

#build

Instance Method Details

#to_sObject



31
32
33
34
35
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/activeadmin/views/activeadmin_form.rb', line 31

def to_s
  loading_map_code = @loading_map ? "<script src=\"https://maps.googleapis.com/maps/api/js?language=#{@lang}&callback=googleMapObject.init\" async defer></script>" : ''
  "<li>" \
  "#{loading_map_code}" \
  "<div id=\"google_map\" style=\"height: #{@height}px\"></div>" \
  "<script>
    var googleMapObject = {
      coords: null,
      map: null,
      marker: null,

      getCoordinates: function() {
        return {
          lat: parseFloat($(\"##{@id_lat}\").val()) || 55.7522200,
          lng: parseFloat($(\"##{@id_lng}\").val()) || 37.6155600,
        };
      },

      saveCoordinates: function() {
        $(\"##{@id_lat}\").val( googleMapObject.coords.lat.toFixed(10) );
        $(\"##{@id_lng}\").val( googleMapObject.coords.lng.toFixed(10) );
      },

      init: function() {
        googleMapObject.coords = googleMapObject.getCoordinates();
        googleMapObject.saveCoordinates();

        googleMapObject.map = new google.maps.Map(document.getElementById('google_map'), {
          center: googleMapObject.coords,
          zoom: 12
        });
        
        var latLngCoord = new google.maps.LatLng(googleMapObject.coords.lat, googleMapObject.coords.lng);
        googleMapObject.marker = new google.maps.Marker({
          position: latLngCoord,
          map: googleMapObject.map,
          draggable: true
        });
        googleMapObject.map.addListener('click', function(e) {
          googleMapObject.coords = { lat: e.latLng.lat(), lng: e.latLng.lng() };
          googleMapObject.saveCoordinates();
          googleMapObject.marker.setPosition(googleMapObject.coords);
        });
        googleMapObject.marker.addListener('drag', function(e) {
          googleMapObject.coords = { lat: e.latLng.lat(), lng: e.latLng.lng() };
          googleMapObject.saveCoordinates();
        });
      }
    }
  </script>" \
  "</li>"
end