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.

Welcome

There are many breaking changes with version 0.4.0. Please review the new examples and change log on how to reformat your code if it no longer compiles.

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.

Example Directions API Request

```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); ```

Example Distance Matrix API Request

```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); ```

Example Elevation API Positional Request

```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); ```

Example Geocoding API Request

```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) } ```

Example Reverse Geocoding API Request

```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. } ```

Example Time Zone API Request

```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 ); ```

Geolocation API

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.

Feedback

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!

Change Log

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)?), )

To do

  1. Track both requests and request elements for rate limiting.
  2. Make a generic get() function for that can be used by all APIs.
  3. Look into making APIs optional, i.e. features. Possible? Desirable?
  4. Look into the prelude::* convention.
  5. Look into integrating yaiouom.
  6. Convert explicit query validation to session types wherever reasonable.
  7. Places API. There are no immediate plans for supporting this API. It's quite big and I have no current need for it. If you would like to have to implemented, please contact me.
  8. Roads API. There are no immediate plans for supporting this API. It's quite big and I have no current need for it. If you would like to have to implemented, please contact me.