Jan 22, 2008

Fun with Map: Simple Geocoding with Yahoo Map API

Yahoo Map API has a simple REST interface which works well with Mathematica.
Here is an example to show a simple Geocoding function.

Problem: Give an address, return (longitude, latitude)

The basic query format:
http://local.yahooapis.com/MapsService/V1/geocode?appid=YahooDemo
&street=?&city=?&state=?


Let's try it with the address of Wolfram Research Inc.

geocoding[{"100 Trade Center Drive", "Champaign", "IL"}]
{-88.244868, 40.097855}

Is the result right? Let's check it out with Map Image API.


image[geocoding[{"100 Trade Center Drive", "Champaign", "IL"}]]



Notice:
1. I use "YahooDemo" as AppID, if you want to use this function frequently, please apply your own ID, it is free.
2. The Geocoding API is limited to 5, 000 queries per IP per day, Google Map has the similar limitation, so if you need geocoding a large amount of addresses, please use offline geocoding application.


reference: Yahoo Map API, Map Image API, Geocoding API

update:
Google version: see 1th comment , Thanks very much.

2 comments:

Ragfield said...

With Google:

hexEncode[s_String] := StringJoin[
Riffle[IntegerString[ToCharacterCode[s, "UTF-8"], 16, 2], "%", {1, -2, 2}]
]

Geocode[addr_String, apikey_String] := Module[
{url, xml, cases},
url = "http://maps.google.com/maps/geo?q="
<> hexEncode[addr]
<> "&output=xml&key="
<> apikey;
xml = Import[url, "XML"];
cases = Cases[xml, XMLElement["coordinates", _, {x_String}] :> x, \[Infinity]];
Reverse@Take[First@ImportString[First[cases], "CSV"], 2]
];

In[3] := Geocode["100 Trade Centre, Champaign, IL", "API KEY DELETED"]
Out[3]= {40.0979,-88.2457}

Steph said...

Good readding