-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
208 lines (171 loc) · 7.34 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Initialize the Google Map
function initMap() {
const map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 0, lng: 0 }, // Set the initial map center
zoom: 8, // Set the initial zoom level
});
// Adding a marker to the map
const marker = new google.maps.Marker({
position: { lat: 0, lng: 0 }, // Set the marker's position
map: map,
title: "Green Space",
});
// Adding an info window to the marker
const infowindow = new google.maps.InfoWindow({
content: "This is a green space.",
});
marker.addListener('click', function () {
infowindow.open(map, marker);
});
}
// Load the Google Maps JavaScript API
function loadGoogleMapsScript() {
const script = document.createElement('script');
script.src = `http://maps.google.com/maps/api/js?sensor=false`;
script.defer = true;
script.async = true;
document.head.appendChild(script);
}
// Load the Google Maps API when the page loads
window.addEventListener('load', loadGoogleMapsScript);
document.addEventListener('DOMContentLoaded', function () {
const getCurrentLocationButton = document.getElementById('getCurrentLocation');
const iframe = document.getElementById('gmap_canvas');
getCurrentLocationButton.addEventListener('click', function () {
// Check if the Geolocation API is available in the user's browser
if ("geolocation" in navigator) {
// Get the user's current position
navigator.geolocation.getCurrentPosition(function (position) {
// Construct the new Google Maps URL with the user's latitude and longitude
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
const newMapUrl = `https://maps.google.com/maps?q=${latitude},${longitude}&z=13&ie=UTF8&iwloc=&output=embed`;
// Update the iframe's src with the new map URL
iframe.src = newMapUrl;
// Display an alert to the user
alert("Map updated with your current location.");
});
} else {
alert("Geolocation is not available in your browser.");
}
});
});
function updateMapWithSearchLocation() {
const placeInput = document.getElementById('placeInput');
const searchButton = document.getElementById('searchButton');
const iframe = document.getElementById('gmap_canvas');
const geocoder = new google.maps.Geocoder();
searchButton.addEventListener('click', () => {
const placeName = placeInput.value;
geocoder.geocode({ address: placeName }, (results, status) => {
if (status === 'OK' && results[0]) {
const location = results[0].geometry.location;
const latitude = location.lat();
const longitude = location.lng();
alert(`Coordinates: Latitude - ${latitude}, Longitude - ${longitude}`);
const newMapUrl = `https://maps.google.com/maps?q=${latitude},${longitude}&z=13&ie=UTF8&iwloc=&output=embed`;
iframe.src = newMapUrl;
alert("Map updated with your current location.");
} else {
alert('Location not found');
}
});
});
}
function initMap() {
navigator.geolocation.getCurrentPosition(function (position) {
// Construct the new Google Maps URL with the user's latitude and longitude
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
const map = new google.maps.Map(document.getElementById('map'), {
center: { lat: latitude, lng: longitude }, // Set your current location's coordinates
zoom: 15, // Adjust the zoom level as needed
});
// Create a circle with a 500-meter radius around the center
const circle = new google.maps.Circle({
map: map,
center: map.getCenter(),
radius: 500,
fillColor: '#00FF00',
fillOpacity: 0.2,
strokeColor: '#00FF00',
strokeOpacity: 0.8,
strokeWeight: 2,
});
});
}
// Load the Google Maps API when the page loads
function loadGoogleMapsScript() {
const script = document.createElement('script');
script.src = `http://maps.google.com/maps/api/js?sensor=false`;
script.defer = true;
script.async = true;
document.head.appendChild(script);
}
window.addEventListener('load', loadGoogleMapsScript);
// if ("geolocation" in navigator) {
// navigator.geolocation.getCurrentPosition(function (position) {
// const latitude = position.coords.latitude;
// const longitude = position.coords.longitude;
// const newMapUrl = `https://maps.google.com/maps?q=${latitude},${longitude}&z=13&ie=UTF8&iwloc=&output=embed`;
// iframe.src = newMapUrl;
// alert("Map updated with your current location.");
// });
// } else {
// alert("Geolocation is not available in your browser.");
// }
const getSearchLocationButton = document.getElementById('searchButton');
getSearchLocationButton.addEventListener('click', updateMapWithSearchLocation);
// // Function to get directions using Google Maps Directions API
// function getDirections() {
// const directionsService = new google.maps.DirectionsService();
// const directionsDisplay = new google.maps.DirectionsRenderer();
// directionsDisplay.setMap(map);
// const request = {
// origin: 'Starting Location',
// destination: 'Ending Location',
// travelMode: 'DRIVING' // You can choose different travel modes
// };
// directionsService.route(request, function (response, status) {
// if (status === 'OK') {
// directionsDisplay.setDirections(response);
// } else {
// alert('Directions request failed');
// }
// });
// }
// // Function to search for places using Google Maps Places API
// function searchPlaces() {
// const placesService = new google.maps.places.PlacesService(map);
// const request = {
// location: { lat: 0, lng: 0 }, // Specify a location for nearby search
// radius: 1000, // Specify the search radius in meters
// keyword: 'restaurant' // Specify a keyword to search for
// };
// placesService.nearbySearch(request, function (results, status) {
// if (status === google.maps.places.PlacesServiceStatus.OK) {
// // Handle the list of places in the results
// } else {
// alert('Places request failed');
// }
// });
// }
// // Function to get elevation data using Google Maps Elevation API
// function getElevation() {
// const elevator = new google.maps.ElevationService();
// const locations = [
// { location: { lat: 0, lng: 0 } }, // Add coordinates for elevation data
// ];
// elevator.getElevationForLocations({ locations }, function (results, status) {
// if (status === 'OK') {
// // Handle elevation data in the results
// } else {
// alert('Elevation request failed');
// }
// });
// }
// // Function to get traffic data using Google Maps Traffic Layer
// function getTraffic() {
// const trafficLayer = new google.maps.TrafficLayer();
// trafficLayer.setMap(map);
// }