3 releases
0.1.2 | Jul 27, 2024 |
---|---|
0.1.1 | Jul 27, 2024 |
0.1.0 | Jul 26, 2024 |
#826 in Web programming
97 downloads per month
11KB
106 lines
Geocoder
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
Installation
Add this to your Cargo.toml
:
[dependencies]
geocoder = "0.1.0"
Usage
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),
}
}
Examples
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),
}
}
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
- Create an issue (optional)
- Fork the repo
- Make your changes
- Commit your changes (
git commit -am 'Some cool feature'
) - Push to the branch (
git push origin master
) - Create a new Pull Request
Using async functions
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();
}
Dependencies
~6–17MB
~239K SLoC