Google is providing Elevation Web Service for finding elevation of places. You can find the documentation of that REST service here. In this tutorial I am going to explain how to use that service using java.

Program Flow

  1. Form Get request to Google 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 ElevationFinder.java

Get URL

https://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536,-104.9847034|36.455556,-116.866667

Parameters

  1. locations (required) defines the location(s) on the earth from which to return elevation data. This parameter takes either a single location as a comma-separated {latitude,longitude} pair (e.g. "40.714728,-73.998672") or multiple latitude/longitude pairs passed as an array or as an encoded polyline. 
  2. path (required) defines a path on the earth for which to return elevation data. This parameter defines a set of two or more ordered {latitude,longitude} pairs defining a path along the surface of the earth. This parameter must be used in conjunction with the samples parameter described below. For more information
  3. samples (required) specifies the number of sample points along a path for which to return elevation data. The samples parameter divides the given path into an ordered set of equidistant points along the path.
  4. key  Your application's API key. This key identifies your application for purposes of quota management
Find documentation here to know more about parameter usage

Java Code

I have developed all required pojo classes. Just download the project for quick execution. Find main code to get elevation information from Google REST web service.

URL url = new URL("https://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536,-104.9847034|36.455556,-116.866667");
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(outputString);
ElevatorPojo ep = (ElevatorPojo)JsonGenerator.generateTOfromJson(outputString, ElevatorPojo.class);

for(Results res: ep.getResults()) {
    System.out.println("location starts");
    System.out.println("location = "+res.getLocation().getLat()+", "+res.getLocation().getLng());
    System.out.println("location = "+res.getElevation());
    System.out.println("location ends");
}

2 comments:

  1. ¿Como puedo capturar la latitud y longitud dinamicamente?

    ReplyDelete
  2. How store latitude and longitude value in oracle 10g database.

    ReplyDelete

Blogroll

Follow this blog by Email

Popular Posts