For last few days i am playing around with Mule, its really cool integration framework. You can use it to build integration applications by using small amount of code. I had this requirement where given address i wanted to get the Geo Coding information for that address, I wanted to use Google Maps API for getting GeoCode information for the address.
The way Google Maps API works is it has a REST API, it takes address as query string and it returns JSON structure. Ex. If you copy paste this URL in the browser
http://maps.googleapis.com/maps/api/geocode/json?address=3055 Oak Road,Walnut Creek,CA,94597&sensor=false
You will get a JSON structure with longitude and latitude embedded in lot of other information
{
"results" : [
{
"address_components" : [
{
"long_name" : "3055",
"short_name" : "3055",
"types" : [ "street_number" ]
},
{
"long_name" : "Oak Road",
"short_name" : "Oak Rd",
"types" : [ "route" ]
},
{
"long_name" : "Walnut Creek",
"short_name" : "Walnut Creek",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Contra Costa County",
"short_name" : "Contra Costa County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94597",
"short_name" : "94597",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "3055 Oak Road, Walnut Creek, CA 94597, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 37.9302338,
"lng" : -122.0582288
},
"southwest" : {
"lat" : 37.9302299,
"lng" : -122.0582466
}
},
"location" : {
"lat" : 37.9302299,
"lng" : -122.0582466
},
"location_type" : "RANGE_INTERPOLATED",
"viewport" : {
"northeast" : {
"lat" : 37.93158083029149,
"lng" : -122.0568887197085
},
"southwest" : {
"lat" : 37.9288828697085,
"lng" : -122.0595866802915
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
Out of all the data returned by Google MAPS api i am only interested in the Lat and Long and i want to return it in GeoJSON format like this
[
-122.0582466,
37.9302299
]
Which is
[lng,lat]
format, some open source JavaScript Mapping frameworks understand the GeoJSON format.
I built a simple Mule integration application which takes STREETLINE1, CITY, STATE, ZIPCODE as argument and returns geo location in GeoJSON format like this
[lng,lat]
. I followed these steps to build the application
-
First i built a Mule Flow which looks like this
In this flow i have a composite input source that could take input on either VM transport or HTTP transport and then uses the values submitted by user to make a request to Google Maps API. Once the response is returned it uses DataMapper to convert the big response that Google Maps API returns into simple GeoJSON response.
- This is how the Data Mapping file looks like
If you look at the script of this file it looks like this
After deploying this application i can make the POST call to http://localhost:9081/geocode URL and see the location in geojson format like this