Struct GeocodioProxy

Source
pub struct GeocodioProxy {
    pub client: Client,
    pub base_url: Url,
    pub api_key: String,
}
Expand description

A struct used to interface with the Geocodio API.

The client and URL are already provided, all that’s needed from you is your Geocodio API key. There’s an option if you have it in a .env file with the name ‘GEOCODIO_API_KEY’ (GeocodioProxy::new), or if you’re using another method to obtain your key and assigning it to a variable (GeocodioProxy::new_from_key()).

let geocodio = GeocodioProxy::new().unwrap();
// or
let geocodio = GeocodioProxy::new_from_key(my_api_key).unwrap();

Once you instantiate the struct, you can either geocode or reverse geocode a single address or a batch of addresses (up to 10,000 lookups).

Fields§

§client: Client§base_url: Url§api_key: String

Implementations§

Source§

impl GeocodioProxy

Source

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

Create a new instance of GeocodioProxy via a variable named GEOCODIO_API_KEY you define in a .env file.

Source

pub fn new_from_key(api_key: String) -> Result<Self, Error>

Create a new instance of GeocodioProxy via a variable you pass into the method.

Source§

impl GeocodioProxy

Source

pub async fn geocode( &self, address: AddressParams, fields: Option<&[&str]>, ) -> Result<GeocodeResponse, Error>

Geocode a single address.

§Example
use geocodio_lib_rust::{request::address::{AddressInput, AddressParams}, GeocodioProxy};

#[tokio::main]
async fn main() {
   let geocodio = GeocodioProxy::new().unwrap();
   let response = geocodio
       .geocode(
           AddressParams::AddressInput(AddressInput {
                line_1: Some("1500 Sugar Bowl Dr".to_string()),
                line_2: None,
                city: Some("New Orleans".to_string()),
                state: Some("LA".to_string()),
                country: Some("US".to_string()),
                postal_code: Some("70112".to_string()),
           }),
           None,
       )
       .await
       .unwrap();
   println!(
       "The coordinates for the Superdome are: {}, {}", 
       response.results[0].location.latitude, 
       response.results[0].location.longitude
   )
}
Source

pub async fn geocode_batch( &self, addresses: Vec<AddressParams>, ) -> Result<GeocodeBatchResponse, Error>

Batch Geocode up to 10,000 addresses.

§Example
use geocodio_lib_rust::{request::address::AddressParams, response::BatchResult, GeocodioProxy};

#[tokio::main]
async fn main() {
   let addresses = vec![
       AddressParams::String("1500 Sugar Bowl Dr, New Orleans, LA 70112".to_string()),
       AddressParams::String("1 MetLife Stadium Dr, East Rutherford, NJ 07073".to_string()),
       AddressParams::String("1 AT&T Way, Arlington, TX 76011".to_string())
   ];

   let geocodio = GeocodioProxy::new().unwrap();
   let response = geocodio
       .geocode_batch(addresses)
       .await
       .unwrap();

   response.results.map(|res: Vec<BatchResult>| {
       res.iter().map(|address: &BatchResult| {
           if let Some(input) = &address.query {
               println!("INPUT ADDRESS: {:?}", input);
           };
           if let Some(response) = &address.response {
               if let Some(results) = &response.results {
                   println!("ADDRESS COMPONENTS: {:?}", results[0].address_components);
                   println!("FORMATTED ADDRESS: {:?}", results[0].formatted_address);
                   println!("LOCATION: {:?}", results[0].location);
                   println!("ACCURACY: {:?}", results[0].accuracy);
                   println!("ACCURACY TYPE: {:?}", results[0].accuracy_type);
                   println!("SOURCE: {:?}", results[0].source);
               }
           };
           println!("============================")
       }).collect::<Vec<_>>()
   });
}
Source§

impl GeocodioProxy

Source

pub async fn reverse_geocode( &self, coordinates: Coordinates, ) -> Result<GeocodeReverseResponse, Error>

Reverse geocode Coordinates to get the address and other location information.

§Example
use geocodio_lib_rust::{request::address::Coordinates, GeocodioProxy};
 
#[tokio::main]
async fn main() {
    let geocodio = GeocodioProxy::new().unwrap();
    let coordinates = Coordinates { latitude: 40.81352, longitude: -74.074333 };
 
    let response = geocodio
        .reverse_geocode(coordinates)
        .await
        .unwrap();
    println!("{:?}", response);
}
Source

pub async fn reverse_geocode_batch( &self, coordinates: Vec<Coordinates>, ) -> Result<GeocodeBatchResponse, Error>

Reverse geocode a vector of Coordinates to get the addresses and other location information.

§Example
use geocodio_lib_rust::{request::address::Coordinates, GeocodioProxy};

#[tokio::main]
async fn main() {
    let geocodio = GeocodioProxy::new().unwrap();
 
    let coordinates = vec![
        Coordinates { latitude: 40.81352, longitude: -74.074333 },
        Coordinates { latitude: 35.9746000, longitude: -77.9658000 },
        Coordinates { latitude: 32.8793700, longitude: -96.6303900 },
    ];
 
    let response = geocodio
        .reverse_geocode_batch(coordinates)
        .await
        .unwrap();
    println!("{:?}", response);
}

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> 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
Source§

impl<T> ErasedDestructor for T
where T: 'static,