-
Notifications
You must be signed in to change notification settings - Fork 211
/
_geocoder.js.erb
50 lines (45 loc) · 1.62 KB
/
_geocoder.js.erb
1
2
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
var geocoder = new google.maps.Geocoder();
jQuery(document).ready(function($) {
$('#map_location').submit(function(e) {
e.preventDefault();
})
window.mapKnitter.getMap().on('drag',function(e) {
$("#map_lat").val(window.mapKnitter.getMap().getCenter().lat);
$("#map_lon").val(window.mapKnitter.getMap().getCenter().lng);
$("#map_zoom").val(window.mapKnitter.getMap().getZoom());
})
$("#map_location").autocomplete({
autoFocus: true,
//This bit uses the geocoder to fetch address values
source: function(request, response) {
geocoder.geocode( {'address': request.term }, function(results, status) {
response($.map(results, function(item) {
return {
label: item.formatted_address,
value: item.formatted_address,
latitude: item.geometry.location.lat(),
longitude: item.geometry.location.lng(),
bounds: item.geometry.bounds
}
}));
})
},
//This bit is executed upon selection of an address
select: function(event, ui) {
var b = ui.item.bounds
$("#map_lat").val(ui.item.latitude);
$("#map_lon").val(ui.item.longitude);
if (b) {
window.mapKnitter.getMap().fitBounds([
[b.getNorthEast().lat(), b.getNorthEast().lng()],
[b.getSouthWest().lat(), b.getSouthWest().lng()]
]);
} else {
window.mapKnitter.getMap().panTo([ui.item.latitude,
ui.item.longitude])
window.mapKnitter.getMap().setZoom(14)
}
$("#map_zoom").val(window.mapKnitter.getMap().getZoom());
}
});
})