pub struct Location(/* private fields */);
Expand description
Location defines a point using its latitude and longitude.
Implementations§
Source§impl Location
impl Location
Sourcepub fn new<T: Into<f64>>(lat: T, lon: T) -> Self
pub fn new<T: Into<f64>>(lat: T, lon: T) -> Self
Create a new Location with its degree values of latitude and longitude.
Examples found in repository?
3fn main() {
4 // Create two locations
5 let berlin = Location::new(52.518611, 13.408056);
6 let moscow = Location::new(55.751667, 37.617778);
7
8 // Get distance between two locations
9 let distance = berlin.distance_to(&moscow).unwrap();
10 println!(
11 "Distance between berlin and moscow: {} meters",
12 distance.meters()
13 );
14
15 // Get the center of a number of locations
16 let center = Location::center(&[&berlin, &moscow]);
17 println!(
18 "Center of berlin and moscow: Lat={}, Lon={}",
19 center.latitude(),
20 center.longitude()
21 );
22
23 // Check radial bounds
24 let is_in_radius = berlin
25 .is_in_circle(&moscow, Distance::from_meters(1000))
26 .unwrap();
27 println!("Is Berlin in a 1000m radius of Moscow? {}", is_in_radius);
28}
Sourcepub const fn new_const(lat: f64, lon: f64) -> Self
pub const fn new_const(lat: f64, lon: f64) -> Self
Create a new const Location with its degree values of latitude and longitude.
Sourcepub fn latitude(&self) -> f64
pub fn latitude(&self) -> f64
Get the latitude.
Examples found in repository?
3fn main() {
4 // Create two locations
5 let berlin = Location::new(52.518611, 13.408056);
6 let moscow = Location::new(55.751667, 37.617778);
7
8 // Get distance between two locations
9 let distance = berlin.distance_to(&moscow).unwrap();
10 println!(
11 "Distance between berlin and moscow: {} meters",
12 distance.meters()
13 );
14
15 // Get the center of a number of locations
16 let center = Location::center(&[&berlin, &moscow]);
17 println!(
18 "Center of berlin and moscow: Lat={}, Lon={}",
19 center.latitude(),
20 center.longitude()
21 );
22
23 // Check radial bounds
24 let is_in_radius = berlin
25 .is_in_circle(&moscow, Distance::from_meters(1000))
26 .unwrap();
27 println!("Is Berlin in a 1000m radius of Moscow? {}", is_in_radius);
28}
Sourcepub fn longitude(&self) -> f64
pub fn longitude(&self) -> f64
Get the longitude.
Examples found in repository?
3fn main() {
4 // Create two locations
5 let berlin = Location::new(52.518611, 13.408056);
6 let moscow = Location::new(55.751667, 37.617778);
7
8 // Get distance between two locations
9 let distance = berlin.distance_to(&moscow).unwrap();
10 println!(
11 "Distance between berlin and moscow: {} meters",
12 distance.meters()
13 );
14
15 // Get the center of a number of locations
16 let center = Location::center(&[&berlin, &moscow]);
17 println!(
18 "Center of berlin and moscow: Lat={}, Lon={}",
19 center.latitude(),
20 center.longitude()
21 );
22
23 // Check radial bounds
24 let is_in_radius = berlin
25 .is_in_circle(&moscow, Distance::from_meters(1000))
26 .unwrap();
27 println!("Is Berlin in a 1000m radius of Moscow? {}", is_in_radius);
28}
Sourcepub fn distance_to(&self, to: &Location) -> Result<Distance, String>
pub fn distance_to(&self, to: &Location) -> Result<Distance, String>
Find the distance from itself to another point. Internally uses Vincenty’s inverse formula. For better performance and lesser accuracy, consider haversine_distance_to. This method returns Err if the formula fails to converge within 100 iterations.
Examples found in repository?
3fn main() {
4 // Create two locations
5 let berlin = Location::new(52.518611, 13.408056);
6 let moscow = Location::new(55.751667, 37.617778);
7
8 // Get distance between two locations
9 let distance = berlin.distance_to(&moscow).unwrap();
10 println!(
11 "Distance between berlin and moscow: {} meters",
12 distance.meters()
13 );
14
15 // Get the center of a number of locations
16 let center = Location::center(&[&berlin, &moscow]);
17 println!(
18 "Center of berlin and moscow: Lat={}, Lon={}",
19 center.latitude(),
20 center.longitude()
21 );
22
23 // Check radial bounds
24 let is_in_radius = berlin
25 .is_in_circle(&moscow, Distance::from_meters(1000))
26 .unwrap();
27 println!("Is Berlin in a 1000m radius of Moscow? {}", is_in_radius);
28}
Sourcepub fn haversine_distance_to(&self, to: &Location) -> Distance
pub fn haversine_distance_to(&self, to: &Location) -> Distance
Find the distance from itself to another point using Haversine formula. This is usually computationally less intensive than distance_to but is generally not as accurate.
Sourcepub fn is_in_circle(
&self,
center: &Location,
radius: Distance,
) -> Result<bool, String>
pub fn is_in_circle( &self, center: &Location, radius: Distance, ) -> Result<bool, String>
Check if the point is within a fixed radius of another point.
Examples found in repository?
3fn main() {
4 // Create two locations
5 let berlin = Location::new(52.518611, 13.408056);
6 let moscow = Location::new(55.751667, 37.617778);
7
8 // Get distance between two locations
9 let distance = berlin.distance_to(&moscow).unwrap();
10 println!(
11 "Distance between berlin and moscow: {} meters",
12 distance.meters()
13 );
14
15 // Get the center of a number of locations
16 let center = Location::center(&[&berlin, &moscow]);
17 println!(
18 "Center of berlin and moscow: Lat={}, Lon={}",
19 center.latitude(),
20 center.longitude()
21 );
22
23 // Check radial bounds
24 let is_in_radius = berlin
25 .is_in_circle(&moscow, Distance::from_meters(1000))
26 .unwrap();
27 println!("Is Berlin in a 1000m radius of Moscow? {}", is_in_radius);
28}
Sourcepub fn center(coords: &[&Location]) -> Location
pub fn center(coords: &[&Location]) -> Location
Find the center of given locations.
Examples found in repository?
3fn main() {
4 // Create two locations
5 let berlin = Location::new(52.518611, 13.408056);
6 let moscow = Location::new(55.751667, 37.617778);
7
8 // Get distance between two locations
9 let distance = berlin.distance_to(&moscow).unwrap();
10 println!(
11 "Distance between berlin and moscow: {} meters",
12 distance.meters()
13 );
14
15 // Get the center of a number of locations
16 let center = Location::center(&[&berlin, &moscow]);
17 println!(
18 "Center of berlin and moscow: Lat={}, Lon={}",
19 center.latitude(),
20 center.longitude()
21 );
22
23 // Check radial bounds
24 let is_in_radius = berlin
25 .is_in_circle(&moscow, Distance::from_meters(1000))
26 .unwrap();
27 println!("Is Berlin in a 1000m radius of Moscow? {}", is_in_radius);
28}