7d1447f9813665d9c4789d1bfc3eb8fc85c6a9e1
[freeside.git] / httemplate / elements / mapselect.html
1 <%init>
2 my $conf = new FS::Conf;
3 my $apikey = $conf->config('google_maps_api_key');
4
5 my %opt = @_;
6
7 # Currently requires two fields named 'latitude' and 'longitude'.
8 # Those should be in the edit form. This widget should NOT be in the
9 # edit form (or it will submit a bunch of spurious fields, plus pressing
10 # "enter" in the search box will submit the form).
11
12 </%init>
13 <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places&key=<% $apikey %>"></script>
14 <script src="<% $fsurl %>elements/jquery-gmaps-latlon-picker.js"></script>
15 <style>
16   .gllpLatlonPicker, .gllpMap { width: 600px; height: 600px }
17   #search_location { width: 300px }
18 </style>
19 <fieldset id="latlonpicker" class="gllpLatlonPicker" style="float: right">
20   <input type="text" id="search_location">
21   <input type="hidden" class="gllpLatitude" id="map_lat">
22   <input type="hidden" class="gllpLongitude" id="map_lon">
23   <input type="hidden" class="gllpElevation" id="map_alt">
24   <input type="hidden" class="gllpZoom" id="map_zoom" value="12">
25   <div class="gllpMap"></div>
26 </fieldset>
27 <br/>
28
29 <script>
30
31 $(function() {
32   var container = $('#latlonpicker');
33   var map = gMapsLatLonPickerState['latlonpicker'].vars.map;
34
35   var lat = $('#latitude');
36   var lon = $('#longitude');
37   var alt = $('#altitude');
38   $('#map_lat').val(lat.val());
39   $('#map_lon').val(lon.val());
40   $('#map_alt').val(alt.val());
41   $(document).trigger('gllp_update_fields');
42
43   $(document).on('location_changed', function(ev, obj) {
44     lat.val($('#map_lat').val());
45     lon.val($('#map_lon').val());
46   });
47
48   // requires the Elevation API to be enabled
49   $(document).on('elevation_changed', function(ev, obj) {
50     alt.val($('#map_alt').val());
51   });
52
53   // bypass gllp's search mechanism, use the cooler Places search
54   var searchbox_input = $('#search_location')[0];
55   var searchbox = new google.maps.places.SearchBox(searchbox_input);
56   map.controls[google.maps.ControlPosition.TOP_RIGHT].push(searchbox_input);
57
58   map.addListener('bounds_changed', function() {
59     searchbox.setBounds(map.getBounds());
60   });
61
62   searchbox.addListener('places_changed', function() {
63     var places = searchbox.getPlaces();
64     if (places[0]) {
65       $('#map_lat').val( places[0].geometry.location.lat() );
66       $('#map_lon').val( places[0].geometry.location.lng() );
67       $('#map_zoom').val(12);
68       $(document).trigger('gllp_update_fields');
69     }
70   });
71 });
72 </script>