Module: GmapPointPicker::JavascriptHelper

Defined in:
lib/gmap_point_picker/javascript_helper.rb

Instance Method Summary collapse

Instance Method Details

#gmap_point_picker_bind(options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
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
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/gmap_point_picker/javascript_helper.rb', line 3

def gmap_point_picker_bind options={}
  key             = options[:key]             ||= GmapPointPicker.configuration.api_key
  load_js         = options[:load_js]         ||= GmapPointPicker.configuration.load_js
  lat_field_id    = options[:lat_field_id]    ||= GmapPointPicker.configuration.lat_field_id
  lng_field_id    = options[:lng_field_id]    ||= GmapPointPicker.configuration.lng_field_id
  map_div_id      = options[:map_div_id]      ||= GmapPointPicker.configuration.map_div_id
  latitude        = options[:latitude]        ||= GmapPointPicker.configuration.latitude
  longitude       = options[:longitude]       ||= GmapPointPicker.configuration.longitude
  search_field_id = options[:search_field_id] ||= GmapPointPicker.configuration.search_field_id

  html = ""

  if load_js
    html << "    <script type=\"text/javascript\" src=\"https://maps.googleapis.com/maps/api/js?key=\#{key}&libraries=places\"></script>\n    EOS\n  end\n\n  html << <<-EOS\n  <script>\n  jQuery(document).ready(function() {\n    var marker = false;\n    var center, input, map, search_box;\n    if (jQuery(\"#\#{map_div_id}\").length === 0) {\n      return;\n    }\n    center = new google.maps.LatLng(\#{latitude}, \#{longitude});\n    map    = new google.maps.Map(document.getElementById(\"\#{map_div_id}\"), {\n      mapTypeId: google.maps.MapTypeId.ROADMAP,\n      center: center\n    });\n    map.setZoom(16);\n    input      = document.getElementById(\"\#{search_field_id}\");\n    search_box = new google.maps.places.SearchBox(input);\n    google.maps.event.addListener(search_box, \"places_changed\", function() {\n      var bounds, i, image, place, places;\n      places = search_box.getPlaces();\n      i = 0;\n      bounds = new google.maps.LatLngBounds();\n      place = void 0;\n      place = places[0];\n      if (place) {\n        image = {\n          url: place.icon,\n          size: new google.maps.Size(71, 71),\n          origin: new google.maps.Point(0, 0),\n          anchor: new google.maps.Point(17, 34),\n          scaledSize: new google.maps.Size(25, 25)\n        };\n        bounds.extend(place.geometry.location);\n      }\n      map.fitBounds(bounds);\n      if (map.getZoom() > 16) {\n        map.setZoom(16);\n      }\n      return google.maps.event.addListener(map, \"bounds_changed\", function() {\n        bounds = map.getBounds();\n        return search_box.setBounds(bounds);\n      });\n    });\n    return google.maps.event.addListener(map, \"click\", function(event) {\n      var location, mapZoom;\n      mapZoom = map.getZoom();\n      location = event.latLng;\n      jQuery(\"#\#{lat_field_id}\").val(location.lat());\n      jQuery(\"#\#{lng_field_id}\").val(location.lng());\n      if (marker) {\n        marker.setMap(null);\n      }\n      return marker = new google.maps.Marker({\n        position: event.latLng,\n        map: map,\n        title: \"GmapPointPicker\"\n      });\n    });\n  });\n  </script>\n  EOS\n  return (html.respond_to?(:html_safe) && html.html_safe) || html\nend\n"

#gmap_point_picker_bind_no_jquery(options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/gmap_point_picker/javascript_helper.rb', line 84

def gmap_point_picker_bind_no_jquery options={}
  key             = options[:key]             ||= GmapPointPicker.configuration.api_key
  load_js         = options[:load_js]         ||= GmapPointPicker.configuration.load_js
  lat_field_id    = options[:lat_field_id]    ||= GmapPointPicker.configuration.lat_field_id
  lng_field_id    = options[:lng_field_id]    ||= GmapPointPicker.configuration.lng_field_id
  map_div_id      = options[:map_div_id]      ||= GmapPointPicker.configuration.map_div_id
  latitude        = options[:latitude]        ||= GmapPointPicker.configuration.latitude
  longitude       = options[:longitude]       ||= GmapPointPicker.configuration.longitude
  search_field_id = options[:search_field_id] ||= GmapPointPicker.configuration.search_field_id

  html = ""

  if load_js
    html << "    <script type=\"text/javascript\" src=\"https://maps.googleapis.com/maps/api/js?key=\#{key}&libraries=places\"></script>\n    EOS\n  end\n\n  html << <<-EOS\n  <script>\n  document.addEventListener(\"DOMContentLoaded\", function(event) {\n    var marker = false;\n    var center, input, map, search_box;\n    if (!document.getElementById(\"\#{map_div_id}\")) {\n      return;\n    }\n    center = new google.maps.LatLng(\#{latitude}, \#{longitude});\n    map    = new google.maps.Map(document.getElementById(\"\#{map_div_id}\"), {\n      mapTypeId: google.maps.MapTypeId.ROADMAP,\n      center: center\n    });\n    map.setZoom(16);\n    input      = document.getElementById(\"\#{search_field_id}\");\n    search_box = new google.maps.places.SearchBox(input);\n    google.maps.event.addListener(search_box, \"places_changed\", function() {\n      var bounds, i, image, place, places;\n      places = search_box.getPlaces();\n      i = 0;\n      bounds = new google.maps.LatLngBounds();\n      place = void 0;\n      place = places[0];\n      if (place) {\n        image = {\n          url: place.icon,\n          size: new google.maps.Size(71, 71),\n          origin: new google.maps.Point(0, 0),\n          anchor: new google.maps.Point(17, 34),\n          scaledSize: new google.maps.Size(25, 25)\n        };\n        bounds.extend(place.geometry.location);\n      }\n      map.fitBounds(bounds);\n      if (map.getZoom() > 16) {\n        map.setZoom(16);\n      }\n      return google.maps.event.addListener(map, \"bounds_changed\", function() {\n        bounds = map.getBounds();\n        return search_box.setBounds(bounds);\n      });\n    });\n    return google.maps.event.addListener(map, \"click\", function(event) {\n      var location, mapZoom;\n      mapZoom = map.getZoom();\n      location = event.latLng;\n      document.getElementById(\"\#{lat_field_id}\").value = location.lat();\n      document.getElementById(\"\#{lng_field_id}\").value = location.lng();\n      if (marker) {\n        marker.setMap(null);\n      }\n      return marker = new google.maps.Marker({\n        position: event.latLng,\n        map: map,\n        title: \"GmapPointPicker\"\n      });\n    });\n  });\n  </script>\n  EOS\n  return (html.respond_to?(:html_safe) && html.html_safe) || html\nend\n"