通过百度地图API和高德地图API进行反坐标逆向地理位置

本文分别通过百度地图API和高德地图API,通过WGS84的坐标返回详细的地理位置。

一、通过百度地图API进行反坐标逆向地理位置.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

private JsonNode getAddressBaidu(double lng, double lat) {
        String location = lat + "," + lng;
        if (lat == 1.0E-6 || lat == 0) {
            return null;
        }
        String key="你的key";
        BufferedReader in = null;
        URL tirc = null;
        try {
            tirc = new URL("http://api.map.baidu.com/reverse_geocoding/v3/?ak="+key+"=" + location + "&&output=json&coordtype=wgs84ll");

        } catch (MalformedURLException e1) {
            log.error("反查坐标时出错" + location);
            return null;
        }
        try {
            URLConnection connection = tirc.openConnection();
            connection.setDoOutput(true);
            in = new BufferedReader(new InputStreamReader(tirc.openStream(), "UTF-8"));
            String res;
            StringBuilder sb = new StringBuilder("");
            while ((res = in.readLine()) != null) {
                sb.append(res.trim());
            }
            String str = sb.toString();
            ObjectMapper mapper = new ObjectMapper();
            if (StringUtils.isNotEmpty(str)) {
                try {
                    JsonNode jsonNode = mapper.readTree(str);
                    jsonNode.findValue("status").toString();
                    JsonNode resultNode = jsonNode.findValue("result");
                    JsonNode locationNode = resultNode.findValue("formatted_address");

                    return locationNode;
                } catch (Exception e) {
                    log.error("反查坐标时出错" + location);
                    return null;
                }
            }
        } catch (UnsupportedEncodingException e) {
            //e.printStackTrace();
            log.error("反查坐标时出错" + location);
            return null;
        } catch (IOException e) {
//            e.printStackTrace();
            log.error("反查坐标时出错" + location);
            return null;
        }
        return null;
    }

这段代码的主要功能是使用百度地图API进行反Geo编码,将经纬度坐标转换为地址信息。代码分析如下:

1. 定义方法getAddressBaidu,接收经度lng和纬度lat作为参数。

2. 构造请求URL,URL包含ak参数(apikey)及location参数(经纬度坐标)。

3. 打开URL连接,获取输入流。

4. 从输入流中读取数据,拼接成字符串。

5. 使用Jackson库的ObjectMapper解析该字符串为JsonNode对象。

6. 从JsonNode对象中提取formatted_address字段,得到地址信息。

7.  catches处理网络异常和解析异常。如果出现异常,则返回null。

该代码的主要逻辑就是构造请求,获取响应,解析JSON响应,并从中提取地址信息。如果出现异常,则无法获取地址信息,会返回null。该程序利用百度地图提供的反Geo编码API实现了经纬度坐标到地址的转换。

二、通过高德地图API进行反坐标逆向地理位置.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
     * 高德地图逆地理
     *
     * @param lng
     * @param lat
     * @return
     */
public JsonNode getAddressAmap(double lng, double lat) {
        String key = "你的key";
        String location = lat + "," + lng;
        if (lat == 1.0E-6 || lat == 0) {
            return null;
        }
        BufferedReader in = null;
        URL tirc = null;
        try {
            tirc = new URL("https://restapi.amap.com/v3/geocode/regeo?location="
                    + location + "&poitype=&key="
                    + key + "&radius=1000&extensions=all&batch=false&roadlevel=0");
        } catch (MalformedURLException e1) {
            log.error("反查坐标时出错" + location);
            return null;
        }
        try {
            URLConnection connection = tirc.openConnection();
            connection.setDoOutput(true);
            in = new BufferedReader(new InputStreamReader(tirc.openStream(), "UTF-8"));
            String res;
            StringBuilder sb = new StringBuilder("");
            while ((res = in.readLine()) != null) {
                sb.append(res.trim());
            }
            String str = sb.toString();            
            ObjectMapper mapper = new ObjectMapper();
            if (StringUtils.isNotEmpty(str)) {
                try {
                    JsonNode jsonNode = mapper.readTree(str);
                    jsonNode.findValue("status").toString();
                    JsonNode resultNode = jsonNode.findValue("regeocode");
                    JsonNode locationNode = resultNode.findValue("formatted_address");
                    log.info("反查坐标位置是"+locationNode.asText()+",坐标"+location);
                    return locationNode;
                } catch (Exception e) {
                    log.error("反查坐标时出错" + location);
                    return null;
                }
            }
        } catch (UnsupportedEncodingException e) {
            //e.printStackTrace();
            log.error("反查坐标时出错" + location);
            return null;
        } catch (IOException e) {
//            e.printStackTrace();
            logger.error("反查坐标时出错" + location);
            return null;
        }
        return null;

    }

这段代码的主要功能是使用高德地图API进行反Geo编码,将经纬度坐标转换为地址信息。代码分析如下:

1. 定义方法getAddressAmap,接收经度lng和纬度lat作为参数。

2. 构造请求URL,URL包含ak参数(apikey)及location参数(经纬度坐标)。

3. 打开URL连接,获取输入流。

4. 从输入流中读取数据,拼接成字符串。

5. 使用Jackson库的ObjectMapper解析该字符串为JsonNode对象。

6. 从JsonNode对象中提取formatted_address字段,得到地址信息。 

7. catches处理网络异常和解析异常。如果出现异常,则返回null。

该代码的主要逻辑与使用百度地图API的代码示例非常相似,也是构造请求,获取响应,解析JSON,并提取地址信息。

猜你喜欢

转载自blog.csdn.net/heweiyabeijing/article/details/130427920