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.
```rust use googlemaps::*; let mut mysettings = ClientSettings::new(YOURGOOGLEAPIKEYHERE);
// Example request:
let directions = DirectionsRequest::new( &mut mysettings, // 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) .witharrivaltime(PrimitiveDateTime::new( // Ensure this date is a weekday in the future or this query will return // zero results. Date::tryfromymd(2020, 2, 6).unwrap(), Time::tryfromhms(13, 00, 0).unwrap() )) .execute().unwrap();
// Dump entire response:
println!("{:#?}", directions); ```
```rust use googlemaps::*; let mut mysettings = ClientSettings::new(YOURGOOGLEAPIKEYHERE);
// Example request:
let distancematrix = DistanceMatrixRequest::new( &mut mysettings, // 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().unwrap();
// Dump entire response:
println!("{:#?}", distance_matrix); ```
```rust use googlemaps::*; let mut mysettings = ClientSettings::new(YOURGOOGLEAPIKEYHERE);
// Example request:
let elevation = ElevationRequest::new(&mut mysettings) // Denver, Colorado, the "Mile High City" .forpositionalrequest(LatLng::tryfrom(39.739154, -104.984703).unwrap()) .execute().unwrap();
// Dump entire response:
println!("{:#?}", elevation);
// Parsing example:
println!("Elevation: {} meters", elevation.results.unwrap()[0].elevation); ```
```rust use googlemaps::*; let mut mysettings = ClientSettings::new(YOURGOOGLEAPIKEYHERE);
// Example request:
let location = GeocodingRequest::new(&mut mysettings) .withaddress("10 Downing Street London") .execute().unwrap();
// Dump entire response:
println!("{:#?}", location);
// Parsing example:
for result in &location.results { println!("{}", result.geometry.location) } ```
```rust use googlemaps::*; let mut mysettings = ClientSettings::new(YOURGOOGLEAPIKEYHERE);
// Example request:
let location = GeocodingReverseRequest::new( &mut mysettings, // 10 Downing St, Westminster, London LatLng::tryfrom(51.503364, -0.127625).unwrap(), ) .withresulttype(PlaceType::StreetAddress) .execute().unwrap();
// Dump entire response:
println!("{:#?}", location);
// Parsing example:
for result in &location.results { for addresscomponent in &result.addresscomponents { print!("{} ", addresscomponent.shortname); } println!(""); // New line. } ```
```rust use googlemaps::*; let mut mysettings = ClientSettings::new(YOURGOOGLEAPIKEYHERE);
// Example request:
let timezone = TimeZoneRequest::new( &mut mysettings, // St. Vitus Cathedral in Prague, Czechia LatLng::tryfrom(50.090903, 14.400512).unwrap(), PrimitiveDateTime::new( // Tuesday February 15, 2022 Date::tryfromymd(2022, 2, 15).unwrap(), // 6:00:00 pm Time::tryfrom_hms(18, 00, 0).unwrap(), ), ).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 ); ```
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.4.6: 2020-02-19: Emergency update! Case conflict for TransitMode. Had to force to lower case in URL query string builder.
0.4.6: 2020-02-19: Connected Travis CI.
0.4.6: 2020-02-19: Added support for sub-steps in Directions API.
0.4.5: 2020-02-19: Emergency update! Custom deserializer for Durations was not included in the 0.4.4 release.
0.4.4: 2020-02-19: Interface should be stablizing.
0.4.4: Added some helper functions for destructuring responses.
0.4.4: Ensured response structures are all declared as public.
0.4.4: 2020-02-18: Aliased Distance
and Duration
structs to
DirectionsDistance
and DirectionsDuration
respectively to prevent name
collisions.
0.4.4: 2020-02-18: Changed DirectionsDuration.value
type from u32
to
time::Duration
type.
0.4.4: 2020-02-18: Dropped my custom Serde deserializer in favour of the
time
crate's built-in Serde feature.
0.4.4: 2020-02-17: Added support for waypoint optimization.
0.4.3: 2020-02-09: Happy 15th birthday to Google Maps!
0.4.3: 2020-02-09: Ensured request rate limiting was applied to all API requests.
0.4.2: 2020-02-06: Unix timestamps received from the Google Maps Platform are
now automatically deserialized into time::PrimitiveDateTime
structs for
convenience.
0.4.2: 2020-02-06: Removed precision limit for Google Maps Platform requests.
0.4.1: 2020-02-06: Added time zone and currency enumerations for look-up tables, conversions, and additional handling to be added in the future.
0.4.1: 2020-02-06: Fixed some errors in the examples.
0.4.1: 2020-02-05: Some internal restructuring to make the library more consistent. Improved many comments, better documentation.
0.4.0: ⚠ Breaking change: API keys are no longer passed directly to Google Maps requests. Now, a structure containing your API key, and several optional settings, is passed instead. For example:
Before:
rust
let location = GeocodingReverseRequest::new(
YOUR_GOOGLE_API_KEY_HERE,
// 10 Downing St, Westminster, London
LatLng { lat: 51.5033635, lng: -0.1276248 }
)
After. Note to Rust newbies: you may need to change the ?
to an .unwrap()
if you're running these examples in your main()
function.
rust
let my_settings = ClientSettings::new(YOUR_GOOGLE_API_KEY_HERE);
let location = GeocodingReverseRequest::new(
&mut my_settings,
// 10 Downing St, Westminster, London
LatLng(LatLng::try_from(51.5033635, -0.1276248)?),
)
0.4.0: ⚠ Breaking change: All response structures, such as
DirectionsResponse
, have been altered.
0.4.0: ⚠ Breaking change: All LatLng enum variants have had the
{ lat, lng } fields removed in favour of LatLng structs. Use
LatLng::try_from(lat, lng)
to define latitude/longitude pairs. See the
updated examples.
0.4.0: ⚠ Breaking change: The Elevation API methods
positional_request()
& sampled_path_request()
have been renamed to
for_positional_request()
& for_sampled_path_request()
respectively. See the
updated examples.
0.4.0: ⚠ Breaking change: All f32
fields have been increased to f64
fields.
0.4.0: Implemented automatic retry with exponential backoff. This client library will now attempt to query the Google Cloud Platform several times before giving up and returning an error. Temporary network hiccups will no longer cause your program to fail.
0.4.0: Implemented request rate limiting. Each API can have different request rate limits.
0.4.0: Now implements the log
crate with some logging messages for
debugging.