ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - CyrusOfEden/geocoder: An Elixir Geocoder/Reverse Geocoder
An Elixir Geocoder/Reverse Geocoder. Contribute to CyrusOfEden/geocoder development by creating an account on GitHub.
Visit Site

GitHub - CyrusOfEden/geocoder: An Elixir Geocoder/Reverse Geocoder

GitHub - CyrusOfEden/geocoder: An Elixir Geocoder/Reverse Geocoder

Geocoder

Build Status Inline docs Coverage Status Module Version Hex Docs Total Download License Last Updated

A simple, efficient geocoder/reverse geocoder with a built-in cache. This libary is quite felxible and extensible. You can pretty much extend any part of geocoder: caching store, workers, http client, JSON Codec, Provider, etc..

It supports the current providers out of box

  • Google map
  • OpenCageData
  • Openstreet maps

It supports the current http clients out of the box

  • :httpoison
  • :hackney

It supports the current JSON clients out of the box

  • :jason
  • :jsx

It supports the current caching store out of the box

  • In memory store (Geocoder.Store)

Installation

add :geocoder to your mix.exs dependencies:

def deps do
  [
    {:geocoder, "~> 2.0"}
  ]
end

Update your mix dependencies:

mix deps.get

Notes: If you are Elixir < 1.9, you'll need to use a version before 1.0.

Configuration

Configuration is done by passing options to the Geocoder.Supervisor. See the Geocoder.Config for all possible configuration options. But you can just get started by adding this to your application tree

[
  Geocoder.Supervisor,
]

This will start the geocoder processes with all the default options, and should be compatible with 1.x (process + API + functionality)

Using a different Provider

The default client for Geocoder is OpenStreeMaps. You can easily switch to other providers option. Or you can build your own as long as you implement the behavior Geocoder.Provider correctly. You can look at some examples inside the lib/geocoder/providers/*

Then you can configure the different or custom provider with the following configuration:

[
  {Geocoder.Supervisor, worker_config: [provider: MyApp.Client,  key: "some_api_key"]},
]

NOTES: OpenStreetMaps does not require key. Others provider do.

Using a different HTTP Client

The default client for Geocoder is HTTPoison. You can easily switch to Hackney as it comes as an option. Or you can build your own as long as you implement the behavior Geocoder.HttpClient correctly in your client

Then you can configure the different or custom client with the following configuration:

[
  {Geocoder.Supervisor, worker_config: [http_client: MyApp.Client,  http_client_opts: []]}
]

If you need to set a proxy (or any other option supported by HTTPoison.get/3):

[
  {Geocoder.Supervisor, worker_config:
    [
      http_client_opts: [proxy: "my.proxy.server:3128", proxy_auth: {"username", "password"}]
    ]
   }
  ...
]

Using a different Store

The default caching store is Geocoder.Store. This is a simple in memory cache (in a process basically) and is started by default.

If you want to change the Store, you can provide your own store implementation as well. See how the Geocoder.Store is implemented for an example. Then you will need to configure it using:

[
  {Geocoder.Supervisor, store_module: MyApp.MyStore},
  ...
]

JSON Codec Configuration

The default JSON codec is Jason. You can create you custom codec or use different one as long as you implement the behavior Geocoder.JSONCodec.

Then you can configure the different or custom JSON codec with the following configuration:

[
  {Geocoder.Supervisor, worker_config:
    [
      json_codec: Jason
    ]
   }
  ...
]

Usage

{:ok, coordinates } = Geocoder.call("Toronto, ON")
{:ok, coordinates } = Geocoder.call({43.653226, -79.383184})

coordinates.location.formatted_address

You can pass options to the function that will be passed to the geocoder provider, for example:

Geocoder.call(address: "Toronto, ON", language: "es", key: "...", ...)

You can also change the provider on a per-call basis:

{:ok, coordinates } =
  with
    # use the default provider
    {:error, nil} <- Geocoder.call(query),
    # use an alternative provider. If `key` is not specified here the globally defined key will be used.
    {:error, nil} <- Geocoder.call(query, worker_config: [provider: Geocoder.Providers.OpenCageData, key: "123"]),
    do: {:error}

See here and here for a list of supported parameters for the google maps geocoder provider (Geocoder.Provider.GoogleMaps).

And you're done! How simple was that?

Development

Right now, :geocoder supports three external providers (i.e. sources):

  • Geocoder.Providers.GoogleMaps
  • Geocoder.Providers.OpenCageData
  • Geocoder.Providers.OpenStreetMaps

To run the tests for these, and any future providers, you'll want to pass a PROVIDER environment variable as well as the API_KEY:

PROVIDER=google API_KEY="mykey" mix test

By default, the tests against the Fake provider.

To avoid making external requests in the context of the test suite, usage of the Fake provider is recommended.

The fake provider can be configured by adding a :data tuple to the configuration as shown below.

The keys of the data map must be in either regex or tuple format (specifically a {lat, lng} style pair of floats).

[
  {Geocoder.Supervisor, worker_config:
    [
      data: %{
        ~r/.*New York, NY.*/ => %{
          lat: 40.7587905,
          lon: -73.9787755,
          bounds: %{
            bottom: 40.7587405,
            left: -73.9788255,
            right: -73.9787255,
            top: 40.7588405,
          },
          location: %{
            city: "New York",
            country: "United States",
            country_code: "us",
            county: "New York County",
            formatted_address: "30 Rockefeller Plaza, New York, NY 10112, United States of America",
            postal_code: "10112",
            state: "New York",
            street: "Rockefeller Plaza",
            street_number: "30"
          },
        },
        {40.7587905, -73.9787755} => %{
          lat: 40.7587905,
          lon: -73.9787755,
          bounds: %{
            bottom: 40.7587405,
            left: -73.9788255,
            right: -73.9787255,
            top: 40.7588405,
          },
          location: %{
            city: "New York",
            country: "United States",
            country_code: "us",
            county: "New York County",
            formatted_address: "30 Rockefeller Plaza, New York, NY 10112, United States of America",
            postal_code: "10112",
            state: "New York",
            street: "Rockefeller Plaza",
            street_number: "30"
          },
        }
      }
    ]
   }
  ...
]

Related & Alternative Packages

Copyright and License

Copyright (c) 2023 Cyrus Nouroozi

The source code is licensed under the MIT License.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory