Last active
May 22, 2016 08:53
-
-
Save yasuhiro-okada-aktsk/247d5a98be1aadcda697cbd446a3a0c4 to your computer and use it in GitHub Desktop.
elixir: google place api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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