-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.js
More file actions
79 lines (64 loc) · 2.18 KB
/
map.js
File metadata and controls
79 lines (64 loc) · 2.18 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
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
let map, infoWindow;
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: { lat: -34.397, lng: 150.644 },
zoom: 6,
});
infoWindow = new google.maps.InfoWindow();
const locationButton = document.createElement("button");
locationButton.textContent = "Pan to Current Location";
locationButton.classList.add("custom-map-control-button");
map.controls[google.maps.ControlPosition.TOP_CENTER].push(locationButton);
locationButton.addEventListener("click", () => {
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => {
const pos = {
lat: position.coords.latitude,
lng: position.coords.longitude,
};
infoWindow.setPosition(pos);
infoWindow.setContent("Location found.");
infoWindow.open(map);
map.setCenter(pos);
},
() => {
handleLocationError(true, infoWindow, map.getCenter());
},
);
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
});
}
const emailButton = document.getElementById("emailButton");
emailButton.addEventListener("click", () => {
const userEmail = prompt("Enter your email"," ");
if (userEmail) {
sendLocationByEmail(userEmail);
}
});
function sendLocationByEmail(email) {
const location = {
lat: infoWindow.getPosition().lat(),
lng: infoWindow.getPosition().lng(),
};
// Use Email.js API for sending email, replace with your actual Email.js template and user ID
emailjs.send("service_z7kfwpv", "template_9lkri2t", {
to_email: email,
location: `Latitude: ${location.lat}, Longitude: ${location.lng}`,
})
.then((response) => {
console.log('Email sent:', response);
})
.catch((error) => {
console.error('Error sending email:', error);
});
}
window.initMap = initMap;