Using Mule to get Geo Code for address

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
  1. 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.
  2. 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

14 comments:

Louis Martin said...

Nice Post about geocoding.

Geocoding API

Unknown said...


I am really enjoying reading your well-written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work...

Mulesoft online course


Unknown said...

the blog is good and Interactive it is about mulesoft it is useful for students and Mulesoft Developers for more updates on Mulesoft

https://onlineitguru.com/mulesoft-online-training.html

Morgan said...

Hello There,

Thank you! Thank you! Thank you! Your blog was a total game changer!


Mock is a feature provided by MUnit to mock the behavior of the message processor. Basically, MUnit replaces the actual behavior of message processor with the behavior defined by the user.
There are various scenarios where we can use the Mock Message processor. Imagine we have completed the development of a Mule application and need to test it,


Appreciate your effort for making such useful blogs and helping the community.
Regards,
Morgan

Unknown said...

Hi There,

Allow me to show my gratitude bloggers. You guys are like unicorns. Never seen but always spreading magic. Your content is yummy.So satisfied.

Previously, I wrote a blog post about IT Engineering here at MuleSoft and how we use MuleSoft products at MuleSoft. Inter-team communication is critical for us to complete projects and we decided to implement a Centre for Enablement (C4E) approach internally. The MuleSoft customer success team offered evolved approach to developing MuleSoft applications for IT.
Excellent tutorials - very easy to understand with all the details. I hope you will continue to provide more such tutorials.

Kind Regards,

Unknown said...

Halo,


Allow me to show my gratitude bloggers. You guys are like unicorns. Never seen but always spreading magic. Your content is yummy. So satisfied.


What is the difference between web APIs and web services? Let’s start off with an analogy: all tigers are cats but not all cats are tigers. In a similar way, all web services are APIs, but not all APIs are web services. Web APIs and web services are often confused with each other; however, web APIs are an evolution of web services. Both facilitate information transfer, but web APIs are more dynamic than web services are.




Thanks a lot. This was a perfect step-by-step guide. Don’t think it could have been done better.


Kind Regards,

Unknown said...

Hello Buddy,


Love it absolutely! So crystalline. No mumbo jumbo. No non-sense. Straight and simple. You guys need a standing ovation for your good work.
I started using Mulesoft Training USA blog for my training practice.
Recently, the RAML team decided it was time for an updated server infrastructure. The original site used a web-based Content Management System (CMS) that required a lot of costly server resources. Each client request to the CMS invoked scripts that rendered the pages from outside sources, such as the database and theme;
This led to significant processing time before providing what was, in most cases, a static page. Of course, we ran a caching layer in front of the web server to speed up some requests,


It was cool to see your article pop up in my google search for the process yesterday. Great Guide.
Keep up the good work!


Thanks a heaps,
Morgan lee

MuleSoft Online Training said...

Thanks for delivering a good stuff.....
MuleSoft Online Training
MuleSoft Training in Hyderabad

Priya said...

This blog is helpful for everyone. Thanks for sharing, keep on sharing this type of articles
Mulesoft Online Training
Mulesoft Training in Hyderabad

Johan said...

This is most informative and also this post most user friendly and super navigation to all posts. Thank you so much for giving this information to me.mulesoft training in bangalore



svrtechnologies said...

Thanks for Posting such an useful info....

mulesoft 4 training

Bhanu Sree said...

Thanks for delivering good stuff, Explanation is good, Nice Article.
Mulesoft Training in Hyderabad
Mulesoft Online Training

bhanu sreee said...

This information is really awesome thanks for sharing most valuable information.
Mulesoft Training Hyderabad
Best Mulesoft Online Training
Best Mulesoft Training
Mulesoft Course Online
Mulesoft Training and Certification
Mulesoft Architecture Certification
Mulesoft Course
Mulesoft Developer Certification
Mulesoft Self Learning
Mulesoft Online Training in India
Mule 4 Online Training
Mulesoft Online Training india
Mulesoft Online Training in Hyderabad
Mulesoft Online Training Hyderabad

bhanu sreee said...


Thank you for sharing wonderful information with us to get some idea about it.
Mulesoft Online Training
Mulesoft Online Training in Hyderabad