logo
pub struct ClientSettings {
    pub key: String,
    pub rate_limit: RequestRate,
    pub reqwest_client: Client,
}
Expand description

Use the GoogleMapsClient struct’s implemented methods to set your Google API key and other settings such as: rate limiting, maxium retries, & retry delay times for your requests.

This structure contains your API key - the preferred way for authenticating with the Google Maps Platform APIs, your request rate limit settings, and your automatic retry settings.

How to use this structure’s methods in a builder pattern:

let mut my_settings = GoogleMapsClient::new(YOUR_GOOGLE_API_KEY_HERE)
    .with_max_delay(std::time::Duration::from_secs(32))
    .with_max_retries(10)
    .with_rate(Api::All, 1, std::time::Duration::from_secs(2))
    .build();

Fields

key: String

Your application’s API key. This key identifies your application for purposes of quota management. Learn how to get a key. Contains the application’s API key and other settings.

rate_limit: RequestRate

Rate limits for each of the Google Cloud Maps Platform APIs.

reqwest_client: Client

Allows you to optionally provide your own pre-configured reqwest client that will be used by the Google Maps client.

Implementations

Completes the builder pattern into a final structure.

Arguments:

This method accepts no arguments.

Completes the builder pattern into a final structure.

GoogleMapsClient::build() is preferred. finalize has been kept for backward compatibility.

Arguments:

This method accepts no arguments.

Initialize the settings needed for a Google Cloud Maps API transaction.

Arguments:

This method accepts no arguments.

The Directions API is a service that calculates directions between locations. You can search for directions for several modes of transportation, including transit, driving, walking, or cycling.

use google_maps::prelude::*;
use rust_decimal_macros::dec;

let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE");

let directions = google_maps_client.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::try_from_dec(dec!(45.403_509), dec!(-75.618_904))?),
)

The Distance Matrix API is a service that provides travel distance and time for a matrix of origins and destinations, based on the recommended route between start and end points.

use google_maps::prelude::*;
use rust_decimal_macros::dec;

let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE");

let distances = google_maps_client.distance_matrix(
    // 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::try_from_dec(dec!(37.387_316), dec!(-122.060_008))?),
    ],
)

The Elevation API provides elevation data for all locations on the surface of the earth, including depth locations on the ocean floor (which return negative values).

The Geocoding API is a service that provides geocoding and reverse geocoding of addresses. Geocoding is the process of converting addresses (like a street address) into geographic coordinates (like latitude and longitude), which you can use to place markers on a map, or position the map.

The Geocoding API is a service that provides geocoding and reverse geocoding of addresses. Reverse geocoding is the process of converting geographic coordinates into a human-readable address.

use google_maps::prelude::*;
use rust_decimal_macros::dec;

let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE");

let address = google_maps_client.reverse_geocoding(
    // 10 Downing St, Westminster, London
    LatLng::try_from_dec(dec!(51.503_364), dec!(-0.127_625))?,
)

The Time Zone API provides time offset data for locations on the surface of the earth. You request the time zone information for a specific latitude/longitude pair and date. The API returns the name of that time zone, the time offset from UTC, and the daylight savings offset.

use google_maps::prelude::*;
use rust_decimal_macros::dec;

let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE");

let time_zone = google_maps_client.time_zone(
     // St. Vitus Cathedral in Prague, Czechia
     LatLng::try_from_dec(dec!(50.090_903), dec!(14.400_512))?,
     // Tuesday February 23, 2020 @ 6:00:00 pm
     NaiveDate::from_ymd(2020, 2, 23).and_hms(18, 00, 0)
)

The Place API Place Autocomplete service returns place predictions. The request specifies a textual search string and optional geographic bounds. The service can be used to provide autocomplete functionality for text-based geographic searches, by returning places such as businesses, addresses and points of interest as a user types.

The Place API Query Autocomplete service allows you to add on-the-fly geographic query predictions to your application. Instead of searching for a specific location, a user can type in a categorical search, such as “pizza near New York” and the service responds with a list of suggested queries matching the string. As the Query Autocomplete service can match on both full words and substrings, applications can send queries as the user types to provide on-the-fly predictions.

use google_maps::prelude::*;
use rust_decimal_macros::dec;

let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE");

let predictions = google_maps_client.place_autocomplete("51".to_string())
    .with_location_and_radius(LatLng::try_from_dec(dec!(54), dec!(-114))?, 1_000)
    .with_type(AutocompleteType::Address)
    .execute()
    .await?;

println!("{:#?}", predictions);

The Roads API Snap To Roads service takes up to 100 GPS points collected along a route, and returns a similar set of data, with the points snapped to the most likely roads the vehicle was traveling along. Optionally, you can request that the points be interpolated, resulting in a path that smoothly follows the geometry of the road.

use google_maps::prelude::*;
use rust_decimal_macros::dec;

let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE");

let snapped_points = google_maps_client.snap_to_roads(vec![
    LatLng::try_from_dec(dec!(-35.27801), dec!(149.12958))?,
    LatLng::try_from_dec(dec!(-35.28032), dec!(149.12907))?,
    LatLng::try_from_dec(dec!(-35.28099), dec!(149.12929))?,
    LatLng::try_from_dec(dec!(-35.28144), dec!(149.12984))?,
    LatLng::try_from_dec(dec!(-35.28194), dec!(149.13003))?,
    LatLng::try_from_dec(dec!(-35.28282), dec!(149.12956))?,
    LatLng::try_from_dec(dec!(-35.28302), dec!(149.12881))?,
    LatLng::try_from_dec(dec!(-35.28473), dec!(149.12836))?,
])
.with_interpolation(true)
.execute()
.await?;

The Roads API Nearest Roads service returns individual road segments for a given set of GPS coordinates. This services takes up to 100 GPS points and returns the closest road segment for each point. The points passed do not need to be part of a continuous path.

If you are working with sequential GPS points, use Nearest Roads.

use google_maps::prelude::*;
use rust_decimal_macros::dec;

let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE");

let snapped_points = google_maps_client.nearest_roads(vec![
    LatLng::try_from_dec(dec!(-35.27801), dec!(149.12958))?,
    LatLng::try_from_dec(dec!(60.170880), dec!(24.942795))?,
    LatLng::try_from_dec(dec!(60.170879), dec!(24.942796))?,
    LatLng::try_from_dec(dec!(60.170877), dec!(24.942796))?,
])
.execute()
.await?;

Sets the rate limit for the specified API.

Arguments
  • api ‧ Which Google Maps API are you setting the rate limit for? For example, Api::Directions, Api::DistanceMatrix, Api::Elevation, Api::Geocoding, Api::TimeZone, and so on. The Api::All rate limit is applied to all Google Maps API requests in addition to the per-API rate limits.

  • requests ‧ The number of requests the client library is attempting to target. For example, 2 requests per 1 hour.

  • duration ‧ The duration for the targeted request rate. For example, 1 request per 1 minute. This can be defined using the std::time::Duration methods.

Examples:

The following examples show how one might try to limit the request rate to achieve maximum throughput while minimizing charges by Google.

The following rates are subject to change by Google. Please review the current Google Maps Platform billing rates.

This Google client library’s rate limiting is not persistent. If your program is often restarted, it is easily possible to exceed Google’s monthly free credit. These are approximations and examples.

To accurately minimize billing charges by Google, please use the Google Cloud Platform Console IAM & admin to set quotas for each API on the server’s side.

You are responsible for all charges. Use with care.

// Assumptions:
// - $200.00 USD monthly free credit from Google. Thanks, guys!
// - 2,629,746 seconds in a month.
const GOOGLE_CREDIT: f64 = 200.0;
const SECONDS_PER_MONTH: u64 = 2_629_746;
  • Directions API. You are billed for this SKU when your request does not use live traffic information, arrival or departure times, < 10 waypoints, and no waypoint optimization, $0.005 USD per request.
with_rate(Api::Directions, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))
  • Directions Advanced API. You are billed for this SKU when your request requires live traffic information, > 10 waypoints, and/or waypoint optimization, $0.01 per request.
with_rate(Api::Directions, (GOOGLE_CREDIT / 0.01) as u16, Duration::from_secs(SECONDS_PER_MONTH))
  • Distance Matrix API. You are billed for this SKU when your requests do not require live traffic information, $0.005 per element. The below rate assumes an average of 10 elements per request.
with_rate(
    Api::DistanceMatrix,
    (GOOGLE_CREDIT / (0.005 * 10.0)) as u16,
    Duration::from_secs(SECONDS_PER_MONTH)
)
  • Distance Matrix Advanced API. You are billed for this SKU when your requests require live traffic information, $0.01 USD per element. The below rate assumes an average of 10 elements per request.
with_rate(
    Api::DistanceMatrix,
    (GOOGLE_CREDIT / (0.01 * 10.0)) as u16,
    Duration::from_secs(SECONDS_PER_MONTH)
)
with_rate(Api::Elevation, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))
with_rate(Api::Geocoding, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))
with_rate(Api::TimeZone, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))

Passes a user configured reqwest client for the Google Maps client to use. This allows the you to have more control over the how the Google Maps client connects to the Google Maps server.

Mause mentioned that this feature could be useful for writing tests. Thanks for the suggestion!

Arguments
  • reqwest_client ‧ A reqwest client built using the reqwest::Client::builder() function.
Examples:
let reqwest_client = reqwest::Client::builder()
    .user_agent("My Cool App v1.0")
    .build()?;

let mut google_maps_client = GoogleMapsClient::new("YOUR_API_KEY_HERE")
    .with_reqwest_client(reqwest_client)
    .build();

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more