3 my $field = $opt{'field'};
4 my $id = $opt{'id'} || $opt{'field'};
5 my $div_id = "div_$id";
7 my $vertices_json = $opt{'curr_value'} || '[]';
9 my $apikey = FS::Conf->new->config('google_maps_api_key');
12 <& hidden.html, %opt &>
13 <div id="<% $div_id %>" style="height: 600px; width: 600px"></div>
14 <div id="<% $div_id %>_hint" style="width: 100%; border: 2px solid black; text-align: center; box-sizing: border-box; padding: 4px"> </div>
16 <script src="https://maps.googleapis.com/maps/api/js?libraries=drawing&v=3.22<% $apikey ? "&key=$apikey" : '' %>"></script>
21 function updateFormInput(event) {
22 var path = window.polygon.getPath();
23 var vertices = []; // array of arrays, geoJSON style
24 for (var i =0; i < path.getLength(); i++) {
25 var xy = path.getAt(i);
26 vertices[i] = [ xy.lat(), xy.lng() ];
28 if (console) console.log(vertices); //XXX
29 $('#<% $field %>').prop('value', JSON.stringify(vertices));
35 center: {lat: 39.40114, lng: -96.57127}, // continental U.S.
36 mapTypeId: google.maps.MapTypeId.ROADMAP,
39 streetViewControl: false,
41 var div_map = $('#<% $div_id %>');
42 var div_hint = $('#<% $div_id %>_hint');
43 map = new google.maps.Map(div_map[0], mapOptions);
45 var set_hint = function(txt) {
49 var polygonComplete = function(p) {
52 drawingManager.setDrawingMode(null);
53 drawingManager.setOptions({ drawingControl: false });
55 // double click to delete a vertex (so long as it remains a polygon)
56 p.addListener('dblclick', function (mev) {
57 if (mev.vertex != null && window.polygon.getPath().length > 3) {
58 p.getPath().removeAt(mev.vertex);
61 // any time the polygon is modified, update the vertex list
62 p.getPath().addListener('set_at', updateFormInput);
63 p.getPath().addListener('insert_at', updateFormInput);
64 p.getPath().addListener('remove_at', updateFormInput);
69 set_hint('Edit the zone by dragging the markers. Double-click to remove a vertex.');
73 var polygonOptions = {
76 strokeColor: '#0000a0',
84 var vertex_array = <% $vertices_json %>;
85 if ( vertex_array.length > 2 ) {
86 // then we already have a polygon. make it acceptable to google maps,
87 // and also create a bounding box for it and fit the map to that.
90 var bounds = new google.maps.LatLngBounds();
91 for (var i = 0; i < vertex_array.length; i++) {
92 var xy = new google.maps.LatLng(vertex_array[i][0], vertex_array[i][1]);
97 polygonOptions.paths = [ path ];
98 polygonComplete(new google.maps.Polygon(polygonOptions));
99 map.fitBounds(bounds);
102 // there are no vertices, or not enough to make a polygon, so
103 // enable drawing mode to create a new one
105 drawingManager = new google.maps.drawing.DrawingManager({
106 drawingMode: google.maps.drawing.OverlayType.POLYGON,
107 drawingControl: true,
108 drawingControlOptions: {
109 position: google.maps.ControlPosition.TOP_CENTER,
111 google.maps.drawing.OverlayType.POLYGON,
114 polygonOptions: polygonOptions,
117 // after a single polygon is drawn: remember it, add a listener to let
118 // nodes be deleted, and exit drawing mode
119 drawingManager.addListener('polygoncomplete', polygonComplete);
120 drawingManager.setMap(map);
122 // center the map on the user (for lack of a better choice)
123 if (navigator.geolocation) {
124 navigator.geolocation.getCurrentPosition(function(position) {
126 lat: position.coords.latitude,
127 lng: position.coords.longitude
133 } // on error, or if geolocation isn't available, do nothing
135 set_hint('Click to place the corners of the zone.');