Microsoft is providing Bing maps API. To access this API, you need to register the application and get the key. See official documentation here.

Program Flow

  1. Form Get request to Bing Maps API server
  2. Get Response 
  3. Convert Response to Java Pojo Classes
  4. Use those Pojo classes for your own logic
Find the project at below link and execute BingMapDistance.java file.

Get the key 

Register your application here, and get the key.

Form URL

http://dev.virtualearth.net/REST/v1/Routes?wp.1=Cambridge, UK&wp.2=Church St, Newcastle-under-Lyme, UK&key=Your-Applicattion-Key

Parameters

  1. wp.1 - Give starting location address
  2. wp.2 - Give ending location address
  3. key - Give your application key
  4. o - Output format (xml, json)

Java Code

String from = "lynnwood";
String to ="seattle";
String key = "Paste your application key here";
URL url = new URL("http://dev.virtualearth.net/REST/V1/Routes/Driving?o=json&wp.0="+from+"&wp.1="+to+"&avoid=minimizeTolls&key="+key);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
String line, outputString = "";
BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
    outputString += line;
}
System.out.println("*********** JSON Response ***********");
System.out.println(outputString);
System.out.println("*********** POJO Classes ***********");
DistancePojo capRes = new Gson().fromJson(outputString, DistancePojo.class);
System.out.println(capRes);
System.out.println("*********** Distance ***********");
System.out.println(capRes.getResourceSets()[0].getResources()[0].getTravelDistance() + " km");

0 comments:

Blogroll

Follow this blog by Email

Popular Posts