通过经纬度获取地址值(使用百度地图API)

package com.baidumap;
import net.sf.json.JSONObject;

public class LngAndLatUtil {
    public static String getCity(String lat, String lng) {
        JSONObject obj = getLocationInfo(lat, lng).getJSONObject("result").getJSONObject("addressComponent");
        return obj.getString("city");
      }

      public static JSONObject getLocationInfo(String lat, String lng) {
        String url = "http://api.map.baidu.com/geocoder/v2/?location=" + lat + ","
            + lng + "&output=json&ak=你的ak&pois=0";
        JSONObject obj = JSONObject.fromObject(HttpUtil.getRequest(url));
        return obj;
      }

      public static void main(String[] args) {
        System.out.println(LngAndLatUtil.getLocationInfo("30.00", "114"));
      }
}
可以使用百度地图的Elevation API获取经纬度对应的海拔高度。以下是Java代码示例: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class ElevationAPI { public static void main(String[] args) throws Exception { String ak = "你的百度地图AK"; double lat = 39.915; double lng = 116.404; int coordType = 3; // 坐标类型,3代表WGS84坐标系 String url = "http://api.map.baidu.com/geoconv/v1/?coords=" + lng + "," + lat + "&from=" + coordType + "&to=6&ak=" + ak; String json = httpRequest(url); // 解析返回的json数据 String status = getValueByKey(json, "status"); if ("0".equals(status)) { String x = getValueByKey(json, "x"); String y = getValueByKey(json, "y"); // 获取海拔高度 url = "http://api.map.baidu.com/elevation/v1/?locations=" + y + "," + x + "&coordtype=wgs84&ak=" + ak; json = httpRequest(url); status = getValueByKey(json, "status"); if ("0".equals(status)) { String elevation = getValueByKey(json, "elevation"); System.out.println("海拔高度:" + elevation); } else { System.out.println("获取海拔高度失败,错误码:" + status); } } else { System.out.println("坐标转换失败,错误码:" + status); } } public static String httpRequest(String requestUrl) throws IOException { StringBuilder sb = new StringBuilder(); URL url = new URL(requestUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); InputStream inputStream = connection.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str; while ((str = bufferedReader.readLine()) != null) { sb.append(str); } bufferedReader.close(); inputStreamReader.close(); inputStream.close(); connection.disconnect(); return sb.toString(); } public static String getValueByKey(String json, String key) { String value = ""; int index = json.indexOf(key); if (index > 0) { index += key.length() + 2; boolean flag = true; while (flag) { String tmp = json.substring(index, index + 1); if (!",".equals(tmp) && !"}".equals(tmp)) { value += tmp; index++; } else { flag = false; } } } return value; } } ``` 其中,ak是你的百度地图API Key,lat和lng分别是经纬度,coordType是坐标类型,这里使用的是WGS84坐标系。在代码中,先使用Geoconv API经纬度转换为百度坐标系的x、y坐标,然后再使用Elevation API获取海拔高度。最后输出海拔高度即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值