1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
//! # `google_maps`
//! [![Docs](https://docs.rs/google_maps/badge.svg)](https://docs.rs/google_maps)
//! [![Crates.io](https://img.shields.io/crates/v/google_maps.svg?maxAge=2592000)](https://crates.io/crates/google_maps)
//! [![msrv](https://img.shields.io/badge/rustc-1.70.0+-red)](https://blog.rust-lang.org/2023/06/01/Rust-1.70.0.html)
//!
//! 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, Time Zone API, and parts of the Places and
//! Roads API.
//!
//! <img src="https://www.arkiteq.ca/crates/google_maps/banner.jpg" alt="Unofficial Google Maps Platform Client for Rust" width="400"/>
//!
//! # Installation
//!
//! Configure the dependencies:
//!
//! ```toml
//! [dependencies]
//! google_maps = "3.4"
//! ```
//!
//! Optionally, add `rust_decimal = "1"` and `rust_decimal_macros = "1"` for
//! access to the `dec!` macro. This macro can be used to define decimal numbers
//! in your program.
//!
//! This is useful for hard-coding latitudes and longitudes into your code for
//! testing.
//!
//! ## Feature Flags
//!
//! The desired Google Maps APIs can be enabled individually via feature flags.
//!
//! Additionally, usage of rustls for Reqwest is supported.
//!
//! ### Google Maps Client Feature Flags:
//!
//! * `autocomplete`
//! * `directions`
//! * `distance_matrix`
//! * `elevation`
//! * `geocoding`
//! * `places`
//! * `roads`
//! * `time_zone`
//! * `enable-reqwest` (uses [reqwest](https://crates.io/crates/reqwest) for
//! querying the Google Maps API).
//! * `geo` (support for the [geo](https://crates.io/crates/geo-types) crate's
//! types)
//!
//! Note: The Places API's autocomplete feature have been put in the
//! `autocomplete` feature flag. The rest of the Places APIs will be put under
//! the `places` feature flag.
//!
//! ### Reqwest Feature Flags
//!
//! For use with `enable-reqwest` only.
//!
//! * `native-tls`
//! * `rustls`
//! * `gzip`
//! * `brotli`
//!
//! ### Default Feature Flags
//!
//! By default, the Google Maps client includes all implemented Google Maps
//! APIs. Reqwest will secure the connection using the system-native TLS
//! (`native-tls`), and has gzip compression enabled (`gzip`).
//!
//! ```toml
//! default = [
//! # Google Maps crate features:
//! "directions",
//! "distance_matrix",
//! "elevation",
//! "geocoding",
//! "time_zone",
//! "autocomplete",
//! "roads",
//! "places",
//!
//! # reqwest features:
//! "enable-reqwest",
//! "reqwest/default-tls",
//! "reqwest/gzip",
//! "enable-reqwest-middleware",
//!
//! # rust_decimal features:
//! "rust_decimal/serde"
//! ]
//! ```
//!
//! #### Feature flag usage example
//!
//! This example will only include the Google Maps Directions API. Reqwest will
//! secure the connection using the Rustls library, and has brotli compression
//! enabled.
//!
//! ```toml
//! google_maps = {
//! version = "3.0",
//! default-features = false,
//! features = [
//! "directions",
//! "enable-reqwest",
//! "rustls",
//! "brotli"
//! ]
//! }
//! ```
//!
//! # Release Notes
//!
//! The [full changelog is available
//! here](https://github.com/leontoeides/google_maps/blob/master/CHANGELOG.md).
//!
//! Releases [are available on
//! GitHub](https://github.com/leontoeides/google_maps/releases).
//!
//! # Examples
//!
//! ## Directions API
//!
//! 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.
//!
//! ```rust
//! use google_maps::prelude::*;
//!
//! let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE");
//!
//! // Example request:
//!
//! 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_f64(45.403_509, -75.618_904)?),
//! )
//! .with_travel_mode(TravelMode::Driving)
//! .execute()
//! .await?;
//!
//! // Dump entire response:
//!
//! println!("{:#?}", directions);
//! ```
//!
//! ## Distance Matrix API
//!
//! 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.
//!
//! ```rust
//! use google_maps::prelude::*;
//!
//! let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE");
//!
//! // Example request:
//!
//! let distance_matrix = 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))?),
//! ],
//! ).execute().await?;
//!
//! // Dump entire response:
//!
//! println!("{:#?}", distance_matrix);
//! ```
//!
//! ## Elevation API (Positional)
//!
//! 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).
//!
//! ```rust
//! use google_maps::prelude::*;
//!
//! let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE");
//!
//! // Example request:
//!
//! let elevation = google_maps_client.elevation()
//! // Denver, Colorado, the "Mile High City"
//! .for_positional_request(LatLng::try_from_dec(dec!(39.739_154), dec!(-104.984_703))?)
//! .execute()
//! .await?;
//!
//! // Dump entire response:
//!
//! println!("{:#?}", elevation);
//!
//! // Display all results:
//!
//! if let Some(results) = &elevation.results {
//! for result in results {
//! println!("Elevation: {} meters", result.elevation)
//! }
//! }
//! ```
//!
//! ## Geocoding API
//!
//! 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.
//!
//! ```rust
//! use google_maps::prelude::*;
//!
//! let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE");
//!
//! // Example request:
//!
//! let location = google_maps_client.geocoding()
//! .with_address("10 Downing Street London")
//! .execute()
//! .await?;
//!
//! // Dump entire response:
//!
//! println!("{:#?}", location);
//!
//! // Print latitude & longitude coordinates:
//!
//! for result in location.results {
//! println!("{}", result.geometry.location)
//! }
//! ```
//!
//! ## Reverse Geocoding API
//!
//! 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.
//!
//! ```rust
//! use google_maps::prelude::*;
//!
//! let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE");
//!
//! // Example request:
//!
//! let location = google_maps_client.reverse_geocoding(
//! // 10 Downing St, Westminster, London
//! LatLng::try_from_dec(dec!(51.503_364), dec!(-0.127_625))?,
//! )
//! .with_result_type(PlaceType::StreetAddress)
//! .execute()
//! .await?;
//!
//! // Dump entire response:
//!
//! println!("{:#?}", location);
//!
//! // Display all results:
//!
//! for result in location.results {
//! println!(
//! "{}",
//! result.address_components.iter()
//! .map(|address_component| address_component.short_name.to_string())
//! .collect::<Vec<String>>()
//! .join(", ")
//! );
//! }
//! ```
//!
//! ## Time Zone API
//!
//! 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.
//!
//! ```rust
//! use google_maps::prelude::*;
//!
//! let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE");
//!
//! // Example request:
//!
//! 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))?,
//! // The time right now in UTC (Coordinated Universal Time)
//! Utc::now()
//! ).execute().await?;
//!
//! // Dump entire response:
//!
//! println!("{:#?}", time_zone);
//!
//! // Usage example:
//!
//! println!("Time at your computer: {}", Local::now().to_rfc2822());
//!
//! if let Some(time_zone_id) = time_zone.time_zone_id {
//! println!(
//! "Time in {}: {}",
//! time_zone_id.name(),
//! Utc::now().with_timezone(&time_zone_id).to_rfc2822()
//! );
//! }
//! ```
//!
//! ### [Geolocation API](https://developers.google.com/maps/documentation/geolocation/intro)
//!
//! 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.
//!
//! ### Controlling Request Settings
//!
//! The Google Maps client settings can be used to change the request rate and
//! automatic retry parameters.
//!
//! ```rust
//! use google_maps::prelude::*;
//!
//! let google_maps_client = GoogleMapsClient::new("YOUR_GOOGLE_API_KEY_HERE")
//! // For all Google Maps Platform APIs, the client will limit 2 sucessful
//! // requests for every 10 seconds:
//! .with_rate(Api::All, 2, std::time::Duration::from_secs(10))
//! // Returns the `GoogleMapsClient` struct to the caller. This struct is used
//! // to make Google Maps Platform requests.
//! .build();
//! ```
//!
//! # 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](https://github.com/leontoeides/google_maps/issues)! I'm not always fast
//! at responding but I will respond. Thanks!
//!
//! # Roadmap
//!
//! - [ ] Track both _requests_ and request _elements_ for rate limiting.
//! - [ ] Make a generic `get()` function for that can be used by all APIs.
//! - [ ] Convert explicit query validation to session types wherever
//! reasonable.
//! - [ ] [Places API](https://developers.google.com/places/web-service/intro).
//! Only partly implemented. If you would like to have any missing pieces
//! implemented, please contact me.
//! - [ ] [Roads API](https://developers.google.com/maps/documentation/roads/intro).
//! Only partly implemented. If you would like to have any missing pieces
//! implemented, please contact me.
//!
//! # Author's Note
//!
//! This crate is expected to work well and have the more important Google Maps
//! features implemented. It should work well because
//! [serde](https://crates.io/crates/serde) and, by default,
//! [reqwest](https://crates.io/crates/reqwest) do most of the heavy lifting!
//!
//! 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.
#![forbid(unsafe_code)]
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(
clippy::cast_precision_loss,
clippy::missing_errors_doc,
clippy::module_name_repetitions,
clippy::multiple_crate_versions,
clippy::too_many_lines
)]
#![doc(html_favicon_url = "https://www.arkiteq.ca/crates/google_maps/icon.png")]
#![doc(html_logo_url = "https://www.arkiteq.ca/crates/google_maps/logo.png")]
// Common / global modules:
mod client;
pub mod error;
pub mod prelude;
mod serde;
pub mod types;
// Optional Google Maps API modules. Their inclusion can be changed with
// feature flags:
#[cfg(any(feature = "directions", feature = "distance_matrix"))]
pub mod directions;
#[cfg(feature = "distance_matrix")]
pub mod distance_matrix;
#[cfg(feature = "elevation")]
pub mod elevation;
#[cfg(feature = "geocoding")]
pub mod geocoding;
pub mod places;
#[cfg(feature = "roads")]
pub mod roads;
#[cfg(feature = "time_zone")]
pub mod time_zone;
// Re-exports. Not great for organization but needed for backward compatibility.
pub use crate::{
client::GoogleMapsClient as ClientSettings, client::GoogleMapsClient,
error::Error as GoogleMapsError, error::Error, types::error::Error as TypeError,
}; // crate
#[cfg(any(feature = "geocoding", feature = "places", feature = "geocoding"))]
pub use crate::types::address_component::AddressComponent;
#[cfg(any(
feature = "directions",
feature = "distance_matrix",
feature = "geocoding",
feature = "places"
))]
pub use crate::types::bounds::Bounds;
#[cfg(any(
feature = "autocomplete",
feature = "directions",
feature = "geocoding"
))]
pub use crate::types::country::Country;
#[cfg(any(feature = "geocoding", feature = "places", feature = "geocoding"))]
pub use crate::types::geometry::Geometry;
#[cfg(any(
feature = "autocomplete",
feature = "directions",
feature = "distance_matrix",
feature = "geocoding",
feature = "places",
feature = "time_zone"
))]
pub use crate::types::language::Language;
#[cfg(any(
feature = "autocomplete",
feature = "directions",
feature = "distance_matrix",
feature = "elevation",
feature = "geocoding",
feature = "places",
feature = "roads",
feature = "time_zone"
))]
pub use crate::types::latlng::LatLng;
#[cfg(any(feature = "geocoding", feature = "places", feature = "geocoding"))]
pub use crate::types::location_type::LocationType;
#[cfg(any(
feature = "autocomplete",
feature = "directions",
feature = "distance_matrix",
feature = "geocoding",
feature = "places"
))]
pub use crate::types::place_type::PlaceType;
#[cfg(any(
feature = "autocomplete",
feature = "directions",
feature = "distance_matrix",
feature = "geocoding",
feature = "places"
))]
pub use crate::types::region::Region;
// Optional dependencies:
#[cfg(feature = "enable-reqwest")]
mod request_rate;
#[cfg(feature = "enable-reqwest")]
pub use crate::request_rate::api::Api;
#[cfg(feature = "enable-reqwest")]
type ReqError = reqwest_maybe_middleware::Error;