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);
}
}
本文详细介绍了如何通过GoogleElevationAPI获取特定地点的海拔信息,并提供了一个使用Java语言实现的示例代码。在使用API前,用户需先注册成为GoogleDeveloper并获取APIkey。API调用次数限制为每日2500次,且每次请求的字节数不能超过2000字符。示例代码展示了如何构建API请求,解析返回的XML响应,并提取关键信息。
549

被折叠的 条评论
为什么被折叠?



