skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, October 18, 2012

Reverse Geocoding Using Bing Rest Api

Posted by Raju Gupta at 12:00 AM – 0 comments
 
For the past several years, the Bing Map API has made search data available for developers to innovate and build various address/ geocode related applications. Using Bing Map API developer can retrieve latitude & longitude (geocode) of an address/ zipCode which is called Geocoding. Vice versa, developer can get address/ zipCode from latitude & longitude (geocode) which is called reverse geocoding.

  1. In this approach I invoked http://dev.virtualearth.net/REST/v1/Locations Bing Api. In order to invoke this API developer needs an authentication key by purchasing a license. There are several other similar apis available in Bing which differ by different parameters.
  2. To get a license for using the Geocoding Webservice, developer needs to register for Bing Maps Developer account. Developer can sign up here: http://www.microsoft.com/maps/product/licensing.aspx . Developer also can use a free version of api without any license fee [free version of api has some limitation which is also documented in the above link]
  3. Now, I am assuming developer will have their own Bing Map authentication key. For this documentation and example I am using an authentication key value: "XXXXXXX".
  4. Create a class ReverseGeoCodeFetcher that would hold all the logic for interacting with reverse geocoding webservices. By following the logic developer can create their own functionality by enhancing the logic.
  5. In ReverseGeoCodeFetcher class I hardcode "latitude", "longitude", "resposeOutputType" value inside main() method. Developer needs to modify all this dynamic value based on their requirement.
  6. I passed "latitude", "longitude", "resposeOutputType" as a parameter to prepareDynamicURL() function to prepare the URL dynamically by appending the parameter after the Bing Map API URL.
  7. After preparing the URL dynamically, I invoked getResultHttpAsStream() method. This method opens calls the Bing URL by opening HttpURLConnection. After receiving a success status code from Bing API, this method populates the response data in a string and return the data to main() method for displaying. In this example I didn't play with result further and just displayed the data using SOP. But developer needs to process the result based on requirement.
  8. In this example, Bing API will return the data in XML format. Bing currently returns the data in XML/ JSON format. Developer can decide the response format type by "output" parameter value "xml/json". In this example "output" parameter value "xml" has been used by passing the "resposeOutputType" value as "xml".





import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.lang.StringUtils;



public class ReverseGeocodeFetcher {
 //Bing service REST-URL
 public static final String bingLocationUrl="http://dev.virtualearth.net/REST/v1/Locations";
 //Authentication key to invoke Bing URL [user need to change this key]
 public static final String bingAuthenticationKey="API Key";
 
 protected static final String EQUAL = "=";
 protected static final String AND = "&";
 protected static final String PARAM_KEY = "key";
 protected static final String PARAM_OUTPUT = "output";

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  ReverseGeocodeFetcher reverseGeocodeFetcher = new ReverseGeocodeFetcher();
  String latitude = "42.399851";
  String longitude = "-71.259792";
  String resposeOutputType = "xml";  
  /*
   * invoking prepareDynamicURL() to prepare URL dynamically by appending 
   * latitude, longitude
   */
  String dynamicURl = reverseGeocodeFetcher.prepareDynamicURL(latitude, longitude, resposeOutputType);
  System.out.println("dynamicURl: " + dynamicURl);
  String result = null;
  if(!StringUtils.isEmpty(dynamicURl)){
   result = reverseGeocodeFetcher.getResultHttpAsStream(dynamicURl);
  }
  System.out.println("result: " + result);
 }
 
 /*
  * This prepareDynamicURL() prepare URL dynamically by appending 
  * street latitude, longitude  as parameter 
  * after the static part of Bing api
  */
 protected String prepareDynamicURL(String latitude, String longitude, 
    String resposeOutputType ) 
 {
  double x= (Double.valueOf(latitude)).doubleValue();
  double tempLatitude = ((Double.valueOf(latitude)).doubleValue());
  double tempLongitude = ((Double.valueOf(longitude)).doubleValue());
  StringBuffer sb = new StringBuffer();
  sb.append(bingLocationUrl).append("/").append(tempLatitude).append(",").append(tempLongitude).append("?");
  //appending bing authentication key
  sb.append(PARAM_KEY).append(EQUAL).append(bingAuthenticationKey).append(AND);
  /*
   * appending response type/ output type value. Bing return response in
   * XML/ JSON/ Text format.   
  */
  sb.append(PARAM_OUTPUT).append(EQUAL).append(resposeOutputType);

  return sb.toString();
 }
 
 /*
  * this getResultHttpAsStream() used to invoke Bing Rest-api
  * using  HttpURLConnection and converting the result in string format
  */
 protected final String getResultHttpAsStream(String url) 
 {
  System.out.println("url = " + url);
     HttpURLConnection conn = null;
     InputStream in = null;
     BufferedReader rd = null;
     StringBuffer sb = new StringBuffer();
  try {
   URL u = new URL(url);
   conn = (HttpURLConnection)u.openConnection();
   
   if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
    //in = conn.getInputStream();
    // Get the response
    rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null)
    {
     sb.append(line);
    }
    rd.close();
   } 
   
  } catch (Throwable e) {
   in = null;
  } finally{
   conn = null;
  }
  return sb.toString();
 }
}


Labels: Bing API Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger