-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
107 lines (92 loc) · 3.19 KB
/
script.js
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
var map;
var drawingManager;
var geocoder;
var polygonCoordinates = [];
// Callback function to initialize the map
function initMap() {
// Set the center coordinates
var center = { lat: -34.397, lng: 150.644 };
// Create the map
map = new google.maps.Map(document.getElementById('map'), {
center: center,
zoom: 8
});
// Create a DrawingManager object
drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.POLYGON,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [google.maps.drawing.OverlayType.POLYGON]
},
polygonOptions: {
editable: true
}
});
// Set the map to the DrawingManager
drawingManager.setMap(map);
// Add an event listener for when the polygon is complete
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
if (event.type === google.maps.drawing.OverlayType.POLYGON) {
var polygon = event.overlay;
// Clear the previous polygon coordinates
polygonCoordinates = [];
// Get the coordinates of the polygon vertices
var path = polygon.getPath();
path.forEach(function(latLng) {
polygonCoordinates.push({ lat: latLng.lat(), lng: latLng.lng() });
reverseGeocodeLatLng(latLng.lat(), latLng.lng());
});
// Disable drawing mode after the polygon is complete
drawingManager.setDrawingMode(null);
// Display the polygonCoordinates output
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = JSON.stringify(polygonCoordinates);
}
});
// Create a geocoder object
geocoder = new google.maps.Geocoder();
}
// Perform reverse geocoding to retrieve the city name and state
function reverseGeocodeLatLng(lat, lng) {
var latLng = { lat: lat, lng: lng };
geocoder.geocode({ location: latLng }, function(results, status) {
if (status === 'OK') {
if (results[0]) {
var city = '';
var state = '';
// Find the city and state components in the geocoding results
results[0].address_components.forEach(function(component) {
if (component.types.includes('locality')) {
city = component.long_name;
}
if (component.types.includes('administrative_area_level_1')) {
state = component.long_name;
}
});
// Display the city name and state on the map
var infowindow = new google.maps.InfoWindow({
content: city + ', ' + state
});
var marker = new google.maps.Marker({
position: latLng,
map: map
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
}
} else {
console.log('Geocoder failed due to: ' + status);
}
});
}
// Load the Google Maps JavaScript API
function loadGoogleMapsAPI() {
var script = document.createElement('script');
script.src = 'https://maps.googleapis.com/maps/api/js?key=API_KEY=initMap&libraries=drawing';
script.defer = true;
document.head.appendChild(script);
}
// Call the function to load the API
loadGoogleMapsAPI();