🗺 An unofficial Google Maps Platform client library for the Rust programming language. This client currently implements the Directions API, Distance Matrix API, Elevation API, Geocoding API, and Time Zone API.
This crate is expected to work well and have the more important Google Maps features implemented. It should work well because Reqwest and Serde do most of the heavy lifting! While it's an early release, this crate should work fine as is for most people.
I created this client library because I needed several Google Maps Platform features for a project that I'm working on. So, I've decided to spin my library off into a public crate. This is a very small token of gratitude and an attempt to give back to the Rust community. I hope it saves someone out there some work.
Add google_maps = "0.5.3"
to your project's Cargo.toml
file. You should
check crates.io for the latest version
number. Add this line under the [dependencies]
section.
The full documentation is available at docs.rs
```rust use googlemaps::prelude::*; let mut googlemapsclient = ClientSettings::new(YOURGOOGLEAPIKEY_HERE);
// Example request:
let directions = googlemapsclient.directions( // Origin: Canadian Museum of Nature Location::Address(String::from("240 McLeod St, Ottawa, ON K2P 2R1")), // Destination: Canada Science and Technology Museum Location::LatLng(LatLng::tryfrom(45.403509, -75.618904).unwrap()), ) .withtravelmode(TravelMode::Transit) // Ensure this date is a weekday in the future or this query will return zero // results. .witharrivaltime(NaiveDate::fromymd(2020, 3, 2).and_hms(13, 00, 0)) .execute();
// Dump entire response:
println!("{:#?}", directions); ```
```rust use googlemaps::prelude::*; let mut googlemapsclient = ClientSettings::new(YOURGOOGLEAPIKEY_HERE);
// Example request:
let distancematrix = googlemapsclient.distancematrix( // Origins vec![ // Microsoft Waypoint::Address(String::from("One Microsoft Way, Redmond, WA 98052, United States")), // Cloudflare Waypoint::Address(String::from("101 Townsend St, San Francisco, CA 94107, United States")), ], // Destinations vec![ // Google Waypoint::PlaceId(String::from("ChIJj61dQgK6j4AR4GeTYWZsKWw")), // Mozilla Waypoint::LatLng(LatLng::tryfrom(37.387316, -122.060_008).unwrap()), ], ) .execute();
// Dump entire response:
println!("{:#?}", distance_matrix); ```
```rust use googlemaps::prelude::*; let mut googlemapsclient = ClientSettings::new(YOURGOOGLEAPIKEY_HERE);
// Example request:
let elevation = googlemapsclient.elevation() // Denver, Colorado, the "Mile High City" .forpositionalrequest(LatLng::tryfrom(39.739154, -104.984_703).unwrap()) .execute();
// Dump entire response:
println!("{:#?}", elevation);
// Parsing example:
println!("Elevation: {} meters", elevation.unwrap().results.unwrap()[0].elevation); ```
```rust use googlemaps::prelude::*; let mut googlemapsclient = ClientSettings::new(YOURGOOGLEAPIKEY_HERE);
// Example request:
let location = googlemapsclient.geocoding() .with_address("10 Downing Street London") .execute();
// Dump entire response:
println!("{:#?}", location);
// Parsing example:
for result in &location.unwrap().results { println!("{}", result.geometry.location) } ```
```rust use googlemaps::prelude::*; let mut googlemapsclient = ClientSettings::new(YOURGOOGLEAPIKEY_HERE);
// Example request:
let location = googlemapsclient.reversegeocoding( // 10 Downing St, Westminster, London LatLng::tryfrom(51.503364, -0.127625).unwrap(), ) .withresulttype(PlaceType::StreetAddress) .execute();
// Dump entire response:
println!("{:#?}", location);
// Parsing example:
for result in &location.unwrap().results { for addresscomponent in &result.addresscomponents { print!("{} ", addresscomponent.shortname); } println!(""); // New line. } ```
```rust use googlemaps::prelude::*; let mut googlemapsclient = ClientSettings::new(YOURGOOGLEAPIKEY_HERE);
// Example request:
let timezone = googlemapsclient.timezone( // St. Vitus Cathedral in Prague, Czechia LatLng::tryfrom(50.090903, 14.400512).unwrap(), // Tuesday February 23, 2020 @ 6:00:00 pm NaiveDate::fromymd(2020, 2, 23).and_hms(18, 00, 0) ).execute().unwrap();
// Dump entire response:
println!("{:#?}", time_zone);
// Parsing example:
use std::time::{SystemTime, UNIX_EPOCH};
let unixtimestamp = SystemTime::now().durationsince(UNIXEPOCH).unwrap().assecs();
println!("Time at your computer: {}", unix_timestamp);
println!("Time in {:#?}: {}", timezone.timezoneid.unwrap(), unixtimestamp as i64 + timezone.dstoffset.unwrap() as i64 + timezone.rawoffset.unwrap() as i64 ); ```
```rust use google_maps::prelude::*; use std::time::Duration;
let mut googlemapsclient = ClientSettings::new(YOURGOOGLEAPIKEYHERE)
// For all Google Maps Platform APIs, the client will limit 2 sucessful
// requests for every 10 seconds:
.withrate(Api::All, 2, Duration::fromsecs(10))
// For unsuccessful request attempts, the client will attempt 10 retries
// before giving up:
.withmaxretries(10)
// For unsuccessful requests, the delay between retries is increased after
// each attempt. This parameter ensures that the client will not delay for
// more than 32 seconds between retries:
.withmaxdelay(Duration::from_secs(32))
// Returns the ClientSettings
struct to the caller. This struct is used to
// make Google Maps Platform requests.
.finalize();
```
Google's Geolocation API seems to be offline. While the online documentation
is still available and the API appears configurable through the Google Cloud
Platform console, the Geolocation API responds Status code 404 Not Found
with
an empty body to all requests. This API cannot be implemented until the server
responds as expected.
I would like for you to be successful with your project! If this crate is not working for you, doesn't work how you think it should, or if you have requests, or suggestions - please report them to me! I'm not always fast at responding but I will respond. Thanks!
0.6.0: 2020-02-29: Cleaned up the mod
and use
declarations. To glob import
everything from google_maps into your module, you can use the
use google_maps::prelude::*
convention now.
0.5.2: 2020-02-29: I'm a procedural programmer at heart, so using handles is second nature to me. In an oversight, I was forcing library users to use handles without being consciously aware of it. I have improved the ergonomics of the library. Check out the new examples.
0.5.2: 2020-02-29: There were inaccuracies in the rate limiting examples. Sorry if these poor examples caused any frustration.
The full change log is available on GitHub.
reqwest
to a lighter-weight HTTP client.