Struct Location

Source
pub struct Location(/* private fields */);
Expand description

Location defines a point using its latitude and longitude.

Implementations§

Source§

impl Location

Source

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?
examples/location.rs (line 5)
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}
Source

pub const fn new_const(lat: f64, lon: f64) -> Self

Create a new const Location with its degree values of latitude and longitude.

Source

pub fn latitude(&self) -> f64

Get the latitude.

Examples found in repository?
examples/location.rs (line 19)
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}
Source

pub fn longitude(&self) -> f64

Get the longitude.

Examples found in repository?
examples/location.rs (line 20)
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}
Source

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?
examples/location.rs (line 9)
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}
Source

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.

Source

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?
examples/location.rs (line 25)
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}
Source

pub fn center(coords: &[&Location]) -> Location

Find the center of given locations.

Examples found in repository?
examples/location.rs (line 16)
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}

Trait Implementations§

Source§

impl Clone for Location

Source§

fn clone(&self) -> Location

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Location

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Location

Source§

fn eq(&self, other: &Location) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Location

Source§

impl StructuralPartialEq for Location

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.