标签:java google elevation api 地图海拔高度 xml海拔 jason海拔
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 URL url = new URL("https://maps.googleapis.com/maps/api/elevation/xml?locations=39.7391536,-104.9847034|39.7391536,-104.9847034&key=YourKEY");//远程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();//返回值 System.out.print(xml); } }
使用Google Elevation API获取海拔高度(java版)
标签:java google elevation api 地图海拔高度 xml海拔 jason海拔
原文地址:http://blog.csdn.net/vernice/article/details/46591749