Erlang geocoding via Google Maps API v3

Decided to implement geocoding module for Erlang. If you are folowing my blog, a couple of days ago I have published article about geocoding with PHP. But as I am developing some services using Erlan/OTP I decided to publish Erlang module too.  You may notice that module required ibrowse erlang application to be installed and mochijson2 module from the mochiweb webserver package.

Here it the code of the module:

-module(geocoder).
-author('Sergiy Dzysyak <dzysyak@gmail.com>').

-compile(export_all).

start()->
	ibrowse:start().
	
getLocation(Address)->
	ApiUrl = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=",
	UrlAddress = ibrowse_lib:url_encode(Address),
	case ibrowse:send_req(ApiUrl++UrlAddress, [], get) of
	{ok, _, _, ResJson}->
		{_, Res} = mochijson2:decode(ResJson),
		case lists:keyfind(<<"status">>, 1, Res) of
		{<<"status">>, <<"OK">>} ->
			{<<"results">>, [{_, Res1}]} = lists:keyfind(<<"results">>, 1, Res),
			{<<"geometry">>, {_, Res2}} = lists:keyfind(<<"geometry">>, 1, Res1),
			{<<"location">>, {_, Res3}} = lists:keyfind(<<"location">>, 1, Res2),
			%io:format("Res: ~p~n",[Res3]);
			Res3;
		_ ->
			false
		end;
	_ ->
		false
	end.

Here is actual geocoding usage example:

Eshell V5.8.5  (abort with ^G)
1> geocoder:start().
{ok,<0.33.0>}
2> geocoder:getLocation("1600 Amphitheatre Parkway, Mountain View, CA").
[{<<"lat">>,37.4211444},{<<"lng">>,-122.0853032}]
3> 

You can also take a look to the previous article to see how to put marker on the map with this location coordinates. Follow me on , as I am going to publish more Erlang related articles soon. You can also download the sources from github and report bugs too or you can comment here.

Posted by:
Enjoyed this post? Share and Leave a comment below, thanks! :)