2019-03-12 10:52:28 +00:00
|
|
|
defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
Google Maps [Geocoding service](https://developers.google.com/maps/documentation/geocoding/intro).
|
2019-03-12 10:52:28 +00:00
|
|
|
|
2019-09-22 14:26:23 +00:00
|
|
|
Note: Endpoint is hardcoded to Google Maps API.
|
2019-03-12 10:52:28 +00:00
|
|
|
"""
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2019-03-12 10:52:28 +00:00
|
|
|
alias Mobilizon.Addresses.Address
|
2019-09-22 14:26:23 +00:00
|
|
|
alias Mobilizon.Service.Geospatial.Provider
|
|
|
|
|
2019-03-12 10:52:28 +00:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
@behaviour Provider
|
|
|
|
|
|
|
|
@api_key Application.get_env(:mobilizon, __MODULE__) |> get_in([:api_key])
|
|
|
|
|
2019-08-22 13:57:44 +00:00
|
|
|
@fetch_place_details (Application.get_env(:mobilizon, __MODULE__)
|
|
|
|
|> get_in([:fetch_place_details])) in [true, "true", "True"]
|
|
|
|
|
2019-03-12 10:52:28 +00:00
|
|
|
@components [
|
|
|
|
"street_number",
|
|
|
|
"route",
|
|
|
|
"locality",
|
|
|
|
"administrative_area_level_1",
|
|
|
|
"country",
|
|
|
|
"postal_code"
|
|
|
|
]
|
|
|
|
|
|
|
|
@api_key_missing_message "API Key required to use Google Maps"
|
|
|
|
|
|
|
|
@impl Provider
|
|
|
|
@doc """
|
|
|
|
Google Maps implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
|
|
|
"""
|
|
|
|
@spec geocode(String.t(), keyword()) :: list(Address.t())
|
|
|
|
def geocode(lon, lat, options \\ []) do
|
|
|
|
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
|
|
|
|
|
|
|
|
Logger.debug("Asking Google Maps for reverse geocode with #{url}")
|
|
|
|
|
|
|
|
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
|
|
|
HTTPoison.get(url),
|
|
|
|
{:ok, %{"results" => results, "status" => "OK"}} <- Poison.decode(body) do
|
2019-08-22 13:57:44 +00:00
|
|
|
Enum.map(results, fn entry -> process_data(entry, options) end)
|
2019-03-12 10:52:28 +00:00
|
|
|
else
|
|
|
|
{:ok, %{"status" => "REQUEST_DENIED", "error_message" => error_message}} ->
|
|
|
|
raise ArgumentError, message: to_string(error_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Provider
|
|
|
|
@doc """
|
|
|
|
Google Maps implementation for `c:Mobilizon.Service.Geospatial.Provider.search/2`.
|
|
|
|
"""
|
|
|
|
@spec search(String.t(), keyword()) :: list(Address.t())
|
|
|
|
def search(q, options \\ []) do
|
|
|
|
url = build_url(:search, %{q: q}, options)
|
|
|
|
|
|
|
|
Logger.debug("Asking Google Maps for addresses with #{url}")
|
|
|
|
|
|
|
|
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
|
|
|
HTTPoison.get(url),
|
|
|
|
{:ok, %{"results" => results, "status" => "OK"}} <- Poison.decode(body) do
|
2019-08-22 13:57:44 +00:00
|
|
|
results |> Enum.map(fn entry -> process_data(entry, options) end)
|
2019-03-12 10:52:28 +00:00
|
|
|
else
|
|
|
|
{:ok, %{"status" => "REQUEST_DENIED", "error_message" => error_message}} ->
|
|
|
|
raise ArgumentError, message: to_string(error_message)
|
2019-08-22 13:57:44 +00:00
|
|
|
|
|
|
|
{:ok, %{"results" => [], "status" => "ZERO_RESULTS"}} ->
|
|
|
|
[]
|
2019-03-12 10:52:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec build_url(atom(), map(), list()) :: String.t()
|
|
|
|
defp build_url(method, args, options) do
|
|
|
|
limit = Keyword.get(options, :limit, 10)
|
|
|
|
lang = Keyword.get(options, :lang, "en")
|
|
|
|
api_key = Keyword.get(options, :api_key, @api_key)
|
|
|
|
if is_nil(api_key), do: raise(ArgumentError, message: @api_key_missing_message)
|
|
|
|
|
|
|
|
url =
|
|
|
|
"https://maps.googleapis.com/maps/api/geocode/json?limit=#{limit}&key=#{api_key}&language=#{
|
|
|
|
lang
|
|
|
|
}"
|
|
|
|
|
2019-08-22 13:57:44 +00:00
|
|
|
uri =
|
|
|
|
case method do
|
|
|
|
:search ->
|
|
|
|
url <> "&address=#{args.q}"
|
2019-03-12 10:52:28 +00:00
|
|
|
|
2019-08-22 13:57:44 +00:00
|
|
|
:geocode ->
|
|
|
|
url <> "&latlng=#{args.lat},#{args.lon}&result_type=street_address"
|
|
|
|
|
|
|
|
:place_details ->
|
|
|
|
"https://maps.googleapis.com/maps/api/place/details/json?key=#{api_key}&placeid=#{
|
|
|
|
args.place_id
|
|
|
|
}"
|
|
|
|
end
|
|
|
|
|
|
|
|
URI.encode(uri)
|
2019-03-12 10:52:28 +00:00
|
|
|
end
|
|
|
|
|
2019-08-22 13:57:44 +00:00
|
|
|
defp process_data(
|
|
|
|
%{
|
|
|
|
"formatted_address" => description,
|
|
|
|
"geometry" => %{"location" => %{"lat" => lat, "lng" => lon}},
|
|
|
|
"address_components" => components,
|
|
|
|
"place_id" => place_id
|
|
|
|
},
|
|
|
|
options
|
|
|
|
) do
|
2019-03-12 10:52:28 +00:00
|
|
|
components =
|
|
|
|
@components
|
|
|
|
|> Enum.reduce(%{}, fn component, acc ->
|
|
|
|
Map.put(acc, component, extract_component(components, component))
|
|
|
|
end)
|
|
|
|
|
2019-08-22 13:57:44 +00:00
|
|
|
description =
|
|
|
|
if Keyword.get(options, :fetch_place_details, @fetch_place_details) == true do
|
|
|
|
do_fetch_place_details(place_id, options) || description
|
|
|
|
else
|
|
|
|
description
|
|
|
|
end
|
|
|
|
|
2019-03-12 10:52:28 +00:00
|
|
|
%Address{
|
2019-03-22 14:51:23 +00:00
|
|
|
country: Map.get(components, "country"),
|
|
|
|
locality: Map.get(components, "locality"),
|
|
|
|
region: Map.get(components, "administrative_area_level_1"),
|
2019-03-12 10:52:28 +00:00
|
|
|
description: description,
|
|
|
|
floor: nil,
|
|
|
|
geom: [lon, lat] |> Provider.coordinates(),
|
2019-03-22 14:51:23 +00:00
|
|
|
postal_code: Map.get(components, "postal_code"),
|
2019-08-22 13:57:44 +00:00
|
|
|
street: street_address(components),
|
|
|
|
origin_id: "gm:" <> to_string(place_id)
|
2019-03-12 10:52:28 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp extract_component(components, key) do
|
|
|
|
case components
|
|
|
|
|> Enum.filter(fn component -> key in component["types"] end)
|
|
|
|
|> Enum.map(& &1["long_name"]) do
|
|
|
|
[] -> nil
|
|
|
|
component -> hd(component)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp street_address(body) do
|
|
|
|
if Map.has_key?(body, "street_number") && !is_nil(Map.get(body, "street_number")) do
|
|
|
|
Map.get(body, "street_number") <> " " <> Map.get(body, "route")
|
|
|
|
else
|
|
|
|
Map.get(body, "route")
|
|
|
|
end
|
|
|
|
end
|
2019-08-22 13:57:44 +00:00
|
|
|
|
|
|
|
defp do_fetch_place_details(place_id, options) do
|
|
|
|
url = build_url(:place_details, %{place_id: place_id}, options)
|
|
|
|
|
|
|
|
Logger.debug("Asking Google Maps for details with #{url}")
|
|
|
|
|
|
|
|
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
|
|
|
HTTPoison.get(url),
|
|
|
|
{:ok, %{"result" => %{"name" => name}, "status" => "OK"}} <- Poison.decode(body) do
|
|
|
|
name
|
|
|
|
else
|
|
|
|
{:ok, %{"status" => "REQUEST_DENIED", "error_message" => error_message}} ->
|
|
|
|
raise ArgumentError, message: to_string(error_message)
|
|
|
|
|
|
|
|
{:ok, %{"status" => "INVALID_REQUEST"}} ->
|
|
|
|
raise ArgumentError, message: "Invalid Request"
|
|
|
|
|
|
|
|
{:ok, %{"results" => [], "status" => "ZERO_RESULTS"}} ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2019-03-12 10:52:28 +00:00
|
|
|
end
|