Skip to content

Kociamber/ex_maps

Repository files navigation

ExMaps

Hex Version Hex Docs License Total Download Last Updated

Fast and simple Google Maps API client for Elixir applications.

Overview

Currently the wrapper handles Directions and Distance Matrix Google API calls.

This API client currently supports the Directions and Distance Matrix Google API calls.

Each coordinate entry in the parameter list spawns a separate Elixir process (task) to retrieve data from the Google API or the cache if the query has already been sent. This allows for the simultaneous execution of a large number of queries, returning results quickly. Additionally, each interface type spawns its own "coordinator" process to prevent long queue times when making multiple API calls simultaneously.

The application uses the super-fast generational caching library Nebulex.

Installation

Add ExMaps as a dependency to your mix.exs file:

defp deps() do
  [{:ex_maps, "1.1.3"}]
end

Configuration

To use the Google API, you need a free or commercial API key. Set the environment variable MAPS_API_KEY to your key's value.

If you are using this application as a dependency in your project, add the following configuration to your config/config.exs file:

config :ex_maps, api_key: System.get_env("MAPS_API_KEY")

config :ex_maps, ExMaps.Cache,
  adapter: Nebulex.Adapters.Local,
  n_shards: 2,
  gc_interval: 3600

You are now ready to go!

Basic Usage

Note that a free Google API key has limitations and it's easy to exceed the query limit per second. The first parameter is a list of coordinates, and the second is a list of additional options explained on hexdocs and the Google Dev Guide.

ExMaps.get_directions([%{origin: "Warsaw", destination: "Amsterdam"}], units: :metric)
[%{"geocoded_waypoints" => ... }]

You can mix coordinate types, such as city names and latitude/longitude pairs.

ExMaps.get_directions([
  %{origin: "Warsaw", destination: "Amsterdam"},
  %{origin: {52.3714894, 4.8957388}, destination: {52.3719729, 4.8903469}}
], units: :metric)
[%{"geocoded_waypoints" => ... }, %{"geocoded_waypoints" => ... }]
ExMaps.get_distance_matrix([
  %{origins: ["Warsaw", "Kraków"], destinations: ["Amsterdam", "Utrecht"]}
], language: "pl")
[%{"destination_addresses" => ...}]

To do

  • Add Elevation service.
  • Simplify configuration.

License

This project is MIT licensed. See the LICENSE.md file for more details.