-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathds.html
More file actions
133 lines (117 loc) · 4.85 KB
/
Copy pathds.html
File metadata and controls
133 lines (117 loc) · 4.85 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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="with=device-width" , initial-scale="1.0">
<!--helps make the website responsive-->
<title>CARS</title>
<link rel="stylesheet" href="style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;600;700&family=Rubik+Bubbles&display=swap"
rel="stylesheet">
</head>
<body>
<section class="header">
<nav>
<a href="index.html"><img src="/static/assets/logo.png"></a>
<div class="nav-links">
<ul>
<li><a href="https://teamorborb.github.io/CarHub/index.html">HOME</a></li>
<li><a href="https://teamorborb.github.io/CarHub/about.html">ABOUT</a></li>
<li><a href="https://teamorborb.github.io/CarHub/inventory.html">INVENTORY</a></li>
<li><a href=".">DEALERSHIP</a></li>
<li><a href="https://teamorborb.github.io/CarHub/Quiz.html">QUIZ</a></li>
<li><a href="https://teamorborb.github.io/CarHub/profile.html">PROFILE</a></li>
</ul>
</div>
</nav>
</section>
<div class="text-box">
<h3><u> LOCAL DEALERSHIPS</u></h3>
<!--<iframe
src="https://www.google.com/maps/embed?pb=!1m16!1m12!1m3!1d13383.443500762725!2d-117.11470920128966!3d33.0074432278256!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!2m1!1sdealerships!5e0!3m2!1sen!2sus!4v1675374477551!5m2!1sen!2sus"
width="1000" height="650" style="border: 5px solid rgb(255, 255, 255);" allowfullscreen="" loading="lazy"
referrerpolicy="no-referrer-when-downgrade"></iframe> -->
<div>
<div id="info-box" class="area">
<h2>Your location:</h2>
<div id="location"></div>
</div>
<div id="info-box" class="area">
<h2>Closest dealership</h2>
<div id="dealership"></div>
</div>
</div>
<button class= "button-29" onclick="getLocation()">Find the closest dealership!</button>
</div>
</div>
<script>
// gets the location of the user
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
document.getElementById("location").innerHTML = "Geolocation is not supported by this browser.";
}
}
function findShortestLocation(userLocation, dealershipLocations) {
// Calculate the distance between the user's location and each dealership
let shortestDistance = Infinity
let nearestDealership = dealershipLocations.pop();
for(let location of dealershipLocations) {
let distance = calculateDistance(
userLocation.latitude,
userLocation.longitude,
location.latitude,
location.longitude,
);
// Determine which dealership is closer
if(distance < shortestDistance) {
nearestDealership = location;
shortestDistance = distance;
}
}
if(!nearestDealership) {
alert("couldn't find the shortest distance. Possibly due to empty dealership location list");
}
return nearestDealership;
}
function showPosition(position) {
document.getElementById("location").innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
let locationsURL = `https://cars.nighthawkcodingsociety.com/dealerships`;
// let locationsURL = `${location.origin}/dealerships`;
fetch(locationsURL).then(d => d.json()).then(dealershipLocations => {
let nearestDealership = findShortestLocation(
{
latitude: position.coords.latitude,
longitude: position.coords.longitude
},
dealershipLocations
);
//displays the database info
document.getElementById("dealership").innerHTML = `
The nearest dealership is located at:<br />
Name: ${nearestDealership.name}<br />
Address: ${nearestDealership.address}<br />
Latitude: ${nearestDealership.latitude}<br />
Longitude: ${nearestDealership.longitude}
`;
});
}
function calculateDistance(lat1, lng1, lat2, lng2) {
// Haversine formula to calculate the distance between two coordinates
var radius = 6371; // Earth's radius in kilometers
var dLat = (lat2 - lat1) * (Math.PI / 180);
var dLng = (lng2 - lng1) * (Math.PI / 180);
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1 * (Math.PI / 180)) * Math.cos(lat2 * (Math.PI / 180)) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = radius * c;
return d;
}
</script>
</body>
</html>