-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
135 lines (109 loc) · 4.45 KB
/
index.html
File metadata and controls
135 lines (109 loc) · 4.45 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<script async defer
src="https://maps.googleapis.com/maps/api/js?key={your-api-key}&callback=initMap&libraries=drawing,places"
loading="async"></script>
<button id="drawButton">Enable Drawing</button>
<button id="newDrawButton">New Drawing</button>
<div id="map" style="height: 400px;"></div>
<div id="areaDisplay"></div>
<script>
let map;
let drawingManager;
let placesService;
let nearbyPlacesArray = [];
let drawnPolygons = []; // Added array to store drawn polygons
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: { lat: 0, lng: 0 },
zoom: 2,
});
drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: null,
drawingControl: true,
});
drawingManager.setMap(map);
placesService = new google.maps.places.PlacesService(map);
console.log(placesService);
// Button click event
document.getElementById("drawButton").addEventListener("click", function () {
toggleDrawing();
});
document.getElementById("newDrawButton").addEventListener("click", function () {
clearDrawings();
});
// Overlay complete event
google.maps.event.addListener(drawingManager, 'overlaycomplete', function (event) {
if (event.type === 'polyline') {
const path = event.overlay.getPath();
// Change drawing mode to null (normal mode)
toggleDrawing();
// Fill the drawn area with dark blue color
const polygon = new google.maps.Polygon({
paths: path.getArray(),
strokeColor: "#0000FF",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#0000FF",
fillOpacity: 0.35,
});
// Set the polygon on the map
polygon.setMap(map);
// Store the drawn polygon in the array
drawnPolygons.push(polygon);
// Display the area coordinates in the console
console.log(`Area Coordinates: ${JSON.stringify(path.getArray())}`);
// Perform a nearby search based on the first point of the drawn area
const centerPoint = path.getArray()[0];
performNearbySearch(centerPoint);
}
});
}
function toggleDrawing() {
if (drawingManager.getDrawingMode()) {
drawingManager.setDrawingMode(null);
} else {
drawingManager.setDrawingMode(google.maps.drawing.OverlayType.POLYLINE);
}
}
function clearDrawings() {
drawingManager.setDrawingMode(google.maps.drawing.OverlayType.POLYLINE);
// Clear drawings from the map.data layer
map.data.forEach(function (feature) {
map.data.remove(feature);
});
// Remove drawn polygons from the map
drawnPolygons.forEach(function (polygon) {
polygon.setMap(null);
});
drawnPolygons = [];
nearbyPlacesArray = [];
document.getElementById("areaDisplay").innerHTML = '<h3>Nearby Places:</h3>';
}
function performNearbySearch(center) {
// Define a radius for the nearby search (adjust as needed)
const radius = 5000; // 5000 meters (5 km)
const request = {
location: center,
radius: radius,
type: 'establishment' // You can adjust the type of places you are interested in
};
placesService.nearbySearch(request, function (results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
displayNearbyPlaces(results);
} else {
console.error('Nearby Search failed:', status);
}
});
}
function displayNearbyPlaces(places) {
console.log('New Places');
// Display only the names of nearby places
const areaDisplay = document.getElementById("areaDisplay");
areaDisplay.innerHTML = '<h3>Nearby Places:</h3>';
nearbyPlacesArray = [];
places.forEach(place => {
areaDisplay.innerHTML += `<p>${place.name}</p>`;
nearbyPlacesArray.push(place.name);
});
console.log('Nearby Places Array:', nearbyPlacesArray);
}
</script>