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 <& hidden.html, %opt &>
10 <div id="<% $div_id %>" style="height: 600px; width: 600px"></div>
12 <script src="<% $fsurl %>elements/jquery.js"></script>
13 <script src="https://maps.googleapis.com/maps/api/js?libraries=drawing&v=2"></script>
18 function updateFormInput(event) {
19 var path = window.polygon.getPath();
20 var vertices = []; // array of arrays, geoJSON style
21 for (var i =0; i < path.getLength(); i++) {
22 var xy = path.getAt(i);
23 vertices[i] = [ xy.lat(), xy.lng() ];
25 if (console) console.log(vertices); //XXX
26 $('#<% $field %>').prop('value', JSON.stringify(vertices));
32 center: {lat: 39.40114, lng: -96.57127}, // continental U.S.
33 mapTypeId: google.maps.MapTypeId.ROADMAP,
36 streetViewControl: false,
38 map = new google.maps.Map($('#<% $div_id %>')[0], mapOptions);
40 var polygonComplete = function(p) {
43 drawingManager.setDrawingMode(null);
44 drawingManager.setOptions({ drawingControl: false });
46 // double click to delete a vertex (so long as it remains a polygon)
47 p.addListener('dblclick', function (mev) {
48 if (mev.vertex != null && window.polygon.getPath().length > 3) {
49 p.getPath().removeAt(mev.vertex);
52 // any time the polygon is modified, update the vertex list
53 p.getPath().addListener('set_at', updateFormInput);
54 p.getPath().addListener('insert_at', updateFormInput);
55 p.getPath().addListener('remove_at', updateFormInput);
61 var polygonOptions = {
64 strokeColor: '#0000a0',
72 var vertex_array = <% $vertices_json %>;
73 if ( vertex_array.length > 2 ) {
74 // then we already have a polygon. make it acceptable to google maps,
75 // and also create a bounding box for it and fit the map to that.
78 var bounds = new google.maps.LatLngBounds();
79 for (var i = 0; i < vertex_array.length; i++) {
80 var xy = new google.maps.LatLng(vertex_array[i][0], vertex_array[i][1]);
85 polygonOptions.paths = [ path ];
86 polygonComplete(new google.maps.Polygon(polygonOptions));
87 map.fitBounds(bounds);
90 // there are no vertices, or not enough to make a polygon, so
91 // enable drawing mode to create a new one
93 drawingManager = new google.maps.drawing.DrawingManager({
94 drawingMode: google.maps.drawing.OverlayType.POLYGON,
96 drawingControlOptions: {
97 position: google.maps.ControlPosition.TOP_CENTER,
99 google.maps.drawing.OverlayType.POLYGON,
102 polygonOptions: polygonOptions,
105 // after a single polygon is drawn: remember it, add a listener to let
106 // nodes be deleted, and exit drawing mode
107 drawingManager.addListener('polygoncomplete', polygonComplete);
108 drawingManager.setMap(map);
110 // center the map on the user (for lack of a better choice)
111 if (navigator.geolocation) {
112 navigator.geolocation.getCurrentPosition(function(position) {
114 lat: position.coords.latitude,
115 lng: position.coords.longitude
121 } // on error, or if geolocation isn't available, do nothing