Google Elevation API提供了根据某地经纬度获取该地点海拔高度的接口。开发者可以调用该API获取地点的海拔信息。使用Google Elevation API之前,用户首先需要注册为Google Developer,获取API key。
开发者可以查看API的调用记录,目前Elevation API的限制次数为2500次/每天,每次API访问的字节数小于2000字符。
相应的Java代码如下所示:
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 arguments
*/
public static void main(String[] args) {
// TODO code application logic here
StringBuffer document = new StringBuffer();
try {
//Elevation API URLs are restricted to approximately 2000 characters
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();//返回值
System.out.print(xml);
}
}