Opencage

Struct Opencage 

Source
pub struct Opencage<'a> {
    pub parameters: Parameters<'a>,
    /* private fields */
}
Expand description

An instance of the Opencage Geocoding service

Fields§

§parameters: Parameters<'a>

Implementations§

Source§

impl Opencage<'_>

Source

pub fn new(api_key: String) -> Self

Create a new OpenCage geocoding instance

Source

pub async fn remaining_calls(&self) -> Option<i32>

Retrieve the remaining API calls in your daily quota

Initially, this value is None. Any OpenCage API call using a “Free Tier” key will update this value to reflect the remaining quota for the API key. See the API docs for details.

Source

pub async fn reverse_full<T>( &self, point: &Point<T>, ) -> Result<OpencageResponse<T>, GeocodingError>

A reverse lookup of a point, returning an annotated response.

This method passes the no_record parameter to the API.

§Examples
 use geocoding::{Opencage, Point};

 #[tokio::main]
 async fn main() {
     let oc = Opencage::new("dcdbf0d783374909b3debee728c7cc10".to_string());
     let p = Point::new(2.12870, 41.40139);
     // a full `OpencageResponse` struct
     let res = oc.reverse_full(&p).await.unwrap();
     // responses may include multiple results
     let first_result = &res.results[0];
     assert_eq!(
         first_result.components["road"],
         "Carrer de Calatrava"
     );
 }
Source

pub async fn forward_full<T, U>( &self, place: &str, bounds: U, ) -> Result<OpencageResponse<T>, GeocodingError>

A forward-geocoding lookup of an address, returning an annotated response.

it is recommended that you restrict the search space by passing a bounding box to search within. If you don’t need or want to restrict the search using a bounding box (usually not recommended), you may pass the NOBOX static value instead.

Please see the documentation for details of best practices in order to obtain good-quality results.

This method passes the no_record parameter to the API.

§Examples
 use geocoding::{Opencage, InputBounds, Point};

 #[tokio::main]
 async fn main() {
     let oc = Opencage::new("dcdbf0d783374909b3debee728c7cc10".to_string());
     let address = "UCL Centre for Advanced Spatial Analysis";
     // Optionally restrict the search space using a bounding box.
     // The first point is the bottom-left corner, the second is the top-right.
     let bbox = InputBounds::new(
         Point::new(-0.13806939125061035, 51.51989264641164),
         Point::new(-0.13427138328552246, 51.52319711775629),
     );
     let res = oc.forward_full(&address, bbox).await.unwrap();
     let first_result = &res.results[0];
     // the first result is correct
     assert!(first_result.formatted.contains("90 Tottenham Court Road"));
 }
// You can pass NOBOX if you don't need bounds.
use geocoding::{Opencage, InputBounds, Point};
use geocoding::opencage::{NOBOX};

#[tokio::main]
async fn main() {
    let oc = Opencage::new("dcdbf0d783374909b3debee728c7cc10".to_string());
    let address = "Moabit, Berlin";
    let res = oc.forward_full(&address, NOBOX).await.unwrap();
    let first_result = &res.results[0];
    assert_eq!(
        first_result.formatted,
        "Moabit, Berlin, Germany"
    );
}
// There are several ways to construct a Point, such as from a tuple
use geocoding::{Opencage, InputBounds, Point};

#[tokio::main]
async fn main() {
    let oc = Opencage::new("dcdbf0d783374909b3debee728c7cc10".to_string());
    let address = "UCL Centre for Advanced Spatial Analysis";
    let bbox = InputBounds::new(
        (-0.13806939125061035, 51.51989264641164),
        (-0.13427138328552246, 51.52319711775629),
    );
    let res = oc.forward_full(&address, bbox).await.unwrap();
    let first_result = &res.results[0];
    assert!(
        first_result.formatted.contains(
            "90 Tottenham Court Road"
    ));
}

Trait Implementations§

Source§

impl<T> Forward<T> for Opencage<'_>

Source§

fn forward<'life0, 'life1, 'async_trait>( &'life0 self, place: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<Point<T>>, GeocodingError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

A forward-geocoding lookup of an address. Please see the documentation for details of best practices in order to obtain good-quality results.

This method passes the no_annotations and no_record parameters to the API.

Source§

impl<T> Reverse<T> for Opencage<'_>

Source§

fn reverse<'life0, 'life1, 'async_trait>( &'life0 self, point: &'life1 Point<T>, ) -> Pin<Box<dyn Future<Output = Result<Option<String>, GeocodingError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

A reverse lookup of a point. More detail on the format of the returned String can be found here

This method passes the no_annotations and no_record parameters to the API.

Auto Trait Implementations§

§

impl<'a> Freeze for Opencage<'a>

§

impl<'a> !RefUnwindSafe for Opencage<'a>

§

impl<'a> Send for Opencage<'a>

§

impl<'a> Sync for Opencage<'a>

§

impl<'a> Unpin for Opencage<'a>

§

impl<'a> !UnwindSafe for Opencage<'a>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more