-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocation.java
32 lines (28 loc) · 1.13 KB
/
Location.java
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
public class Location {
private float latitude;
private float longitude;
// Constructor
public Location(float latitude, float longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
// Getters
public float getLatitude() {
return latitude;
}
public float getLongitude() {
return longitude;
}
// Method to calculate distance to another location (optional)
public double distanceTo(Location other) {
// Implement distance calculation using Haversine formula, or simple Cartesian distance for simplicity
double latDiff = Math.toRadians(this.latitude - other.latitude);
double lonDiff = Math.toRadians(this.longitude - other.longitude);
double a = Math.sin(latDiff / 2) * Math.sin(latDiff / 2) +
Math.cos(Math.toRadians(this.latitude)) * Math.cos(Math.toRadians(other.latitude)) *
Math.sin(lonDiff / 2) * Math.sin(lonDiff / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double radiusOfEarth = 6371.0; // in kilometers
return radiusOfEarth * c;
}
}