Crates.io | geocoder |
lib.rs | geocoder |
version | 0.1.2 |
source | src |
created_at | 2024-07-26 22:16:09.33137 |
updated_at | 2024-07-27 03:06:13.221477 |
description | A Rust crate that provides an easy way to use the Google Geocoding API. |
homepage | |
repository | https://github.com/arferreira/geocoder |
max_upload_size | |
id | 1316812 |
size | 11,861 |
Rust crate that provides an easy way to use the Google Geocoding API.
See more information about the Google Geocoding API at the following link: Google Geocoding API
Add this to your Cargo.toml
:
[dependencies]
geocoder = "0.1.0"
Example of usage:
use geocoder::Geocoder;
use std::env;
#[tokio::main]
async fn main() {
// Set your API key as an environment variable.
let api_key = env::var("GEOCODER_API_KEY").expect("GEOCODER_API_KEY must be set");
// Create a new Geocoder instance.
let geocoder = Geocoder::new(&api_key);
// Geocode an address to a location (latitude, longitude).
match geocoder.geocode("1600 Amphitheatre Parkway, Mountain View, CA").await {
Ok(address) => {
println!("Formatted address: {}", address.formatted_address);
}
Err(e) => eprintln!("Error: {}", e),
}
}
Convert an address to a latitude and longitude:
use geocoder::Geocoder;
use std::env;
#[tokio::main]
async fn main() {
let api_key = env::var("GEOCODER_API_KEY").expect("GEOCODER_API_KEY must be set");
let geocoder = Geocoder::new(&api_key);
match geocoder.geocode("1600 Amphitheatre Parkway, Mountain View, CA").await {
Ok(address) => {
println!("Formatted address: {}", address.formatted_address);
println!("Latitude: {}", address.geometry.location.lat);
println!("Longitude: {}", address.geometry.location.lng);
}
Err(e) => eprintln!("Error: {}", e),
}
}
Reverse Geocoding Convert a latitude and longitude to an address:
use geocoder::{Geocoder, structs::LatLng};
use std::env;
#[tokio::main]
async fn main() {
let api_key = env::var("GEOCODER_API_KEY").expect("GEOCODER_API_KEY must be set");
let geocoder = Geocoder::new(&api_key);
let location = LatLng { lat: 37.4224764, lng: -122.0842499 };
match geocoder.reverse_geocode(location).await {
Ok(addresses) => {
if let Some(address) = addresses.first() {
println!("Formatted address: {}", address.formatted_address);
}
}
Err(e) => eprintln!("Error: {}", e),
}
}
This project is licensed under the MIT License - see the LICENSE file for details.
git commit -am 'Some cool feature'
)git push origin master
)With a little more effort you can use async functions with tokio:
use geocoder::Geocoder;
use std::env;
use tokio::task;
async fn geocode_address(geocoder: Geocoder, address: &str) {
match geocoder.geocode(address).await {
Ok(result) => {
println!("Formatted address: {}", result.formatted_address);
}
Err(e) => eprintln!("Error: {}", e),
}
}
#[tokio::main]
async fn main() {
let api_key = env::var("GEOCODER_API_KEY").expect("GEOCODER_API_KEY must be set");
let geocoder = Geocoder::new(&api_key);
let address = "1600 Amphitheatre Parkway, Mountain View, CA";
let handle = task::spawn(async move {
geocode_address(geocoder, address).await;
});
handle.await.unwrap();
}