Skip to main content

ReverseGeocoder

Struct ReverseGeocoder 

Source
pub struct ReverseGeocoder { /* private fields */ }
Expand description

A local, read-only reverse geocoder.

Implementations§

Source§

impl ReverseGeocoder

Source

pub fn embedded() -> Result<Self, Error>

Loads the cn_admin.bin bundled with this crate. No separate data-file download or local build is needed for ordinary users.

Examples found in repository?
examples/minimal.rs (line 4)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let geocoder = ReverseGeocoder::embedded()?;
5
6    // Recorded location : 42°16.9552000'N 126°36.5127000'E 896m
7    let first = geocoder
8        .reverse_geocode_degree_minutes("42°16.9552000'N", "126°36.5127000'E")?
9        .expect("point is in data coverage");
10    println!(
11        "{} {} {} ({})",
12        first.province.name, first.city.name, first.county.name, first.county.code
13    );
14
15    // Recorded location : 22°09.8935000'N 113°33.4033000'E 13m
16    let second = geocoder
17        .reverse_geocode(22.164891666666667,  113.556721666666667).unwrap();
18    println!(
19        "{} {} {} ({})",
20        second.province.name, second.city.name, second.county.name, second.county.code
21    );
22
23    Ok(())
24}
Source

pub fn open(path: impl AsRef<Path>) -> Result<Self, Error>

Opens a binary data file generated by build-data.

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error>

Loads a binary data file from memory, including one embedded with include_bytes!.

Source

pub fn reverse_geocode(&self, latitude: f64, longitude: f64) -> Option<Address>

Looks up decimal (latitude, longitude) coordinates. A coordinate outside the bundled China boundaries returns None.

Examples found in repository?
examples/minimal.rs (line 17)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let geocoder = ReverseGeocoder::embedded()?;
5
6    // Recorded location : 42°16.9552000'N 126°36.5127000'E 896m
7    let first = geocoder
8        .reverse_geocode_degree_minutes("42°16.9552000'N", "126°36.5127000'E")?
9        .expect("point is in data coverage");
10    println!(
11        "{} {} {} ({})",
12        first.province.name, first.city.name, first.county.name, first.county.code
13    );
14
15    // Recorded location : 22°09.8935000'N 113°33.4033000'E 13m
16    let second = geocoder
17        .reverse_geocode(22.164891666666667,  113.556721666666667).unwrap();
18    println!(
19        "{} {} {} ({})",
20        second.province.name, second.city.name, second.county.name, second.county.code
21    );
22
23    Ok(())
24}
Source

pub fn reverse_geocode_degree_minutes( &self, latitude: &str, longitude: &str, ) -> Result<Option<Address>, CoordinateParseError>

Looks up degree-minute latitude and longitude, for example reverse_geocode_degree_minutes("42°16.9552000'N", "126°36.5127000'E").

A valid coordinate outside the bundled China boundaries returns Ok(None). Invalid coordinate text returns CoordinateParseError.

Examples found in repository?
examples/minimal.rs (line 8)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let geocoder = ReverseGeocoder::embedded()?;
5
6    // Recorded location : 42°16.9552000'N 126°36.5127000'E 896m
7    let first = geocoder
8        .reverse_geocode_degree_minutes("42°16.9552000'N", "126°36.5127000'E")?
9        .expect("point is in data coverage");
10    println!(
11        "{} {} {} ({})",
12        first.province.name, first.city.name, first.county.name, first.county.code
13    );
14
15    // Recorded location : 22°09.8935000'N 113°33.4033000'E 13m
16    let second = geocoder
17        .reverse_geocode(22.164891666666667,  113.556721666666667).unwrap();
18    println!(
19        "{} {} {} ({})",
20        second.province.name, second.city.name, second.county.name, second.county.code
21    );
22
23    Ok(())
24}

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.