-
Notifications
You must be signed in to change notification settings - Fork 1
/
geofence.html
108 lines (93 loc) · 2.86 KB
/
geofence.html
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
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
83
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
<!DOCTYPE html>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD2CTWJJwCD7k9B8PTVwVFDvGuTjom55u0&callback=initMap&libraries=drawing" async defer></script>
<html>
<head>
<title>Geofence Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var selectedShape;
function clearSelection() {
if (selectedShape) {
selectedShape.setEditable(false);
selectedShape = null;
}
}
function setSelection(shape) {
clearSelection();
selectedShape = shape;
shape.setEditable(true);
}
function deleteSelectedShape() {
if (selectedShape) {
selectedShape.setMap(null);
}
}
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 44.859244, lng: -93.3832576},
zoom: 8
});
var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.MARKER,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.RECTANGLE
]
},
markerOptions: {icon: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'}
});
drawingManager.setMap(map);
google.maps.event.addListener(drawingManager, 'rectanglecomplete', function(rectangle) {
bounds = rectangle.getBounds();
var leftLon = bounds.b.b;
var rightLon = bounds.b.f;
var topLat = bounds.f.b;
var botLat = bounds.f.f;
alert ("leftLon: "+leftLon+"\n" +
"rightLon: "+rightLon+"\n" +
"topLat: "+topLat+"\n" +
"botLat: "+botLat+"\n");
});
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
if (e.type != google.maps.drawing.OverlayType.MARKER) {
// Switch back to non-drawing mode after drawing a shape.
drawingManager.setDrawingMode(null);
// Add an event listener that selects the newly-drawn shape when the user mouses down on it.
var newShape = e.overlay;
newShape.type = e.type;
google.maps.event.addListener(newShape, 'click', function() {
setSelection(newShape);
});
setSelection(newShape);
}
});
// Clear the current selection when the drawing mode is changed, or when the map is clicked.
google.maps.event.addListener(drawingManager, 'drawingmode_changed', clearSelection);
google.maps.event.addListener(map, 'click', clearSelection);
document.onkeydown = function(e) {
e = e || window.event;
//the delete key appears to be keyCode 46
if(e.keyCode == 46){
deleteSelectedShape();
}
}
}
</script>
</body>
</html>