The Google Elevation API provides an interface for getting the location's altitude based on latitude and longitude. Developers can call the API to get elevation information for a location. before using the Google Elevation API, users first need to register as Google Developer to get the API key.
Developers can view the API's call history, currently the elevation API is limited to 2,500 times per day, each time the API accesses fewer than 2000 bytes.
The corresponding Java code is as follows:
Package Elevation;import Java.io.*;import Java.net.*;import javax.xml.parsers.parserconfigurationexception;import org.xml.sax.saxexception;/** * * @author yfeng14 */public class Elevation {/** * @param args the command line Argu ments */public static void main (string[] args) {//TODO code application logic here StringBuffer do Cument = new StringBuffer (); try {//elevation API URLs is restricted to approximately characters url url = new URL ("https: Maps.googleapis.com/maps/api/elevation/xml?locations=39.7391536,-104.9847034|39.7391536,-104.9847034&key =yourkey ");//remote URL URLConnection conn = url.openconnection (); BufferedReader reader = new BufferedReader (New InputStreamReader (Conn.getinputstream ())); String line = null; while (line = Reader.readline ()) = null) {if (Line.startswith ("<elevation>")) { line = Line.replaceall ("<eleVation> "," "); line = Line.replaceall ("</elevation>", ""); Document.append (line + ""); System.out.println (line); }} reader.close (); } catch (Malformedurlexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } String XML = document.tostring ();//return value System.out.print (XML); }}
Get altitude using the Google Elevation API (Java Edition)