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.
Find the project at below link and execute ElevationFinder.java
Program Flow
- Form Get request to Google server
- Get Response
- Convert Response to Java Pojo Classes
- 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
- 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.
- 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
- 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.
- 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"); }
¿Como puedo capturar la latitud y longitud dinamicamente?
ReplyDeleteHow store latitude and longitude value in oracle 10g database.
ReplyDeleteI was surfing the Internet for information and came across your blog. I am impressed by the information you have on this blog. It shows how well you understand this subject. mapsjar.com
ReplyDeleteThat is really nice to hear. thank you for the update and good luck. maps.lol/trader-joes-us-or-14519
ReplyDelete