Struct google_maps::ClientSettings
source · [−]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
sourceimpl GoogleMapsClient
impl GoogleMapsClient
sourcepub fn build(&self) -> GoogleMapsClient
pub fn build(&self) -> GoogleMapsClient
sourcepub fn finalize(&self) -> GoogleMapsClient
pub fn finalize(&self) -> GoogleMapsClient
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.
sourceimpl GoogleMapsClient
impl GoogleMapsClient
sourcepub fn new(key: &str) -> GoogleMapsClient
pub fn new(key: &str) -> GoogleMapsClient
Initialize the settings needed for a Google Cloud Maps API transaction.
Arguments:
This method accepts no arguments.
sourcepub fn directions(&self, origin: Location, destination: Location) -> Request<'_>
pub fn directions(&self, origin: Location, destination: Location) -> Request<'_>
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))?),
)
sourcepub fn distance_matrix(
&self,
origins: Vec<Waypoint>,
destinations: Vec<Waypoint>
) -> Request<'_>
pub fn distance_matrix(
&self,
origins: Vec<Waypoint>,
destinations: Vec<Waypoint>
) -> Request<'_>
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))?),
],
)
sourcepub fn elevation(&self) -> Request<'_>
pub fn elevation(&self) -> Request<'_>
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).
sourcepub fn geocoding(&self) -> ForwardRequest<'_>
pub fn geocoding(&self) -> ForwardRequest<'_>
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.
sourcepub fn reverse_geocoding(&self, latlng: LatLng) -> ReverseRequest<'_>
pub fn reverse_geocoding(&self, latlng: LatLng) -> ReverseRequest<'_>
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))?,
)
sourcepub fn time_zone(&self, location: LatLng, timestamp: DateTime<Utc>) -> Request<'_>
pub fn time_zone(&self, location: LatLng, timestamp: DateTime<Utc>) -> Request<'_>
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)
)
sourcepub fn place_autocomplete(&self, input: String) -> Request<'_>
pub fn place_autocomplete(&self, input: String) -> Request<'_>
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.
sourcepub fn query_autocomplete(&self, input: String) -> Request<'_>
pub fn query_autocomplete(&self, input: String) -> Request<'_>
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);
sourcepub fn snap_to_roads(&self, path: Vec<LatLng>) -> Request<'_>
pub fn snap_to_roads(&self, path: Vec<LatLng>) -> Request<'_>
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?;
sourcepub fn nearest_roads(&self, path: Vec<LatLng>) -> Request<'_>
pub fn nearest_roads(&self, path: Vec<LatLng>) -> Request<'_>
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?;
sourceimpl GoogleMapsClient
impl GoogleMapsClient
sourcepub fn with_rate(
&mut self,
api: Api,
requests: u16,
per_duration: Duration
) -> &mut GoogleMapsClient
pub fn with_rate(
&mut self,
api: Api,
requests: u16,
per_duration: Duration
) -> &mut GoogleMapsClient
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. TheApi::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 thestd::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)
)
- Elevation API. $0.005 USD per request.
with_rate(Api::Elevation, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))
- Geocoding API. $0.005 USD per request.
with_rate(Api::Geocoding, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))
- Time Zone API. $0.005 USD per request.
with_rate(Api::TimeZone, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))
sourceimpl GoogleMapsClient
impl GoogleMapsClient
sourcepub fn with_reqwest_client(
&mut self,
reqwest_client: Client
) -> &mut GoogleMapsClient
pub fn with_reqwest_client(
&mut self,
reqwest_client: Client
) -> &mut GoogleMapsClient
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 thereqwest::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
sourceimpl Clone for GoogleMapsClient
impl Clone for GoogleMapsClient
sourcefn clone(&self) -> GoogleMapsClient
fn clone(&self) -> GoogleMapsClient
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
Auto Trait Implementations
impl !RefUnwindSafe for GoogleMapsClient
impl Send for GoogleMapsClient
impl Sync for GoogleMapsClient
impl Unpin for GoogleMapsClient
impl !UnwindSafe for GoogleMapsClient
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for Twhere
V: MultiLane<T>,
impl<V, T> VZip<V> for Twhere
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more