This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.js
More file actions
112 lines (101 loc) · 4.28 KB
/
sample.js
File metadata and controls
112 lines (101 loc) · 4.28 KB
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
109
110
111
112
//Defining map as a global variable to access from other functions
var map;
function initMap() {
//Enabling new cartography and themes
google.maps.visualRefresh = true;
//extend the Polygon class to have getArea() function
google.maps.Polygon.prototype.getArea=function(){
var area = google.maps.geometry.spherical.computeArea(this.getPath());
return area;
};
//extend the Polygon class to have getLength() function
google.maps.Polygon.prototype.getLength=function(){
var length = google.maps.geometry.spherical.computeLength(this.getPath());
return length;
};
//Setting starting options of map
var mapOptions = {
center: new google.maps.LatLng(-6.206518, 106.799918),
zoom: 18,
mapTypeId: google.maps.MapTypeId.SATELLITE
};
var geocoder = new google.maps.Geocoder();
document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder, map);
});
//Getting map DOM element
var mapElement = document.getElementById("mapDiv");
//Creating a map with DOM element which is just obtained
map = new google.maps.Map(mapElement, mapOptions);
//creating drawingManager
var drawingManager = new google.maps.drawing.DrawingManager({
//initial drawing tool to be enabled
drawingMode:null,
//enable the drawingControl to be seen in the UI
drawingControl:true,
//select which drawing modes to be seen in the drawingControl and position them
drawingControlOptions: {
//select a position in the UI
position: google.maps.ControlPosition.BOTTOM_CENTER,
//selected drawing modes to be seen in the control
drawingModes: [
google.maps.drawing.OverlayType.POLYGON,
google.maps.drawing.OverlayType.POLYLINE,
]
},
//specific drawing mode options, this one for polyline
polylineOptions: {
strokeColor:'red',
strokeWeight:3
},
//specific drawing mode options, this one for polygon
polygonOptions: {
strokeColor:'blue',
strokeWeight:3,
fillColor:'yellow',
fillOpacity:0.2
}
});
//enable drawing functionality
drawingManager.setMap(map);
//add event listener for completion of your polygon
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
if (event.type == google.maps.drawing.OverlayType.CIRCLE) {
var radius = event.overlay.getRadius();
alert("Radius of circle =" + radius + " m");
}
if (event.type == google.maps.drawing.OverlayType.POLYGON) {
var geom = event.overlay.getPath();
alert("Area of POLYGON =" + calculateArea(geom) + " sq. m");
}
if (event.type == google.maps.drawing.OverlayType.POLYLINE) {
var geom = event.overlay.getPath();
alert("Length of POLYLINE =" + calculateLength(geom) + " m");
}
});
}
// Calculates area in sq meters
function calculateArea(geom) {
var areaInMetres = google.maps.geometry.spherical.computeArea(geom);
//var areaInHectare = areaInMetres * .0001;
return areaInMetres;
}
// Calculates length in meters
function calculateLength(geom) {
var areaInMetre = google.maps.geometry.spherical.computeLength(geom);
return areaInMetre;
}
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
geocoder.geocode({'address': address}, function(results, status) {
if (status === 'OK') {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
} else {
alert('Gunakan satu spasi sebelum input lat/long: ' + status);
}
});
}