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
impl GeocodioProxy
Sourcepub fn new() -> Result<Self, Error>
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.
Sourcepub fn new_from_key(api_key: String) -> Result<Self, Error>
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
impl GeocodioProxy
Sourcepub async fn geocode(
&self,
address: AddressParams,
fields: Option<&[&str]>,
) -> Result<GeocodeResponse, Error>
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
)
}
Sourcepub async fn geocode_batch(
&self,
addresses: Vec<AddressParams>,
) -> Result<GeocodeBatchResponse, Error>
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
impl GeocodioProxy
Sourcepub async fn reverse_geocode(
&self,
coordinates: Coordinates,
) -> Result<GeocodeReverseResponse, Error>
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);
}
Sourcepub async fn reverse_geocode_batch(
&self,
coordinates: Vec<Coordinates>,
) -> Result<GeocodeBatchResponse, Error>
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);
}