Skip to content

Instantly share code, notes, and snippets.

@yasuhiro-okada-aktsk
Last active May 22, 2016 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasuhiro-okada-aktsk/247d5a98be1aadcda697cbd446a3a0c4 to your computer and use it in GitHub Desktop.
Save yasuhiro-okada-aktsk/247d5a98be1aadcda697cbd446a3a0c4 to your computer and use it in GitHub Desktop.
elixir: google place api
defmodule GooglePlaces do
@moduledoc false
# https://developers.google.com/places/web-service/?hl=ja
use HTTPoison.Base
#require Logger
@text_search "/textsearch/json?key=<api key>"
def process_url(url) do
"https://maps.googleapis.com/maps/api/place" <> url
end
def process_response_body(body) do
body
|> Poison.decode!
end
def text_search(text) do
case get(@text_search <> "&query=" <> URI.encode(text)) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
{:ok, body}
{:ok, %HTTPoison.Response{status_code: status}} ->
{:error, "error status : " <> to_string(status)}
{:error, %HTTPoison.Error{reason: reason}} ->
{:error, reason}
end
end
def latlon("unit test") do
%{"lat" => 0.0, "lon" => 0.0}
end
def latlon(address) when is_binary(address) do
latlng = address
|> text_search
|> google_latlng
%{"lat" => latlng["lat"], "lon" => latlng["lng"]}
end
def latlon(_) do
%{"lat" => 0.0, "lon" => 0.0}
end
defp google_latlng({:ok, result}) do
result
|> Map.get("results")
|> hd
|> Map.get("geometry")
|> Map.get("location")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment