Java百度地图高德地图ArcGIS地图逆地理编码

Posted shuzu渊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java百度地图高德地图ArcGIS地图逆地理编码相关的知识,希望对你有一定的参考价值。

百度地图和高德地图用户获取国内地址,区别在于高德地图的精确度比百度地图要高一点,而高德地图不支持国外地址,根据需求选择,ArcGIS地图获取国外地址

1、根据需求选择地图,申请自己的key

高德地图:https://lbs.amap.com/api/webservice/guide/api/georegeo

申请完key后,在我的应用中添加一个应用

 

 

 

 

百度地图:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding

 

申请完AK后,创建应用

 

 

 

 

 

 

 

 

 

2、创建GetAdreesByLatAndLong类用于调用API,进行逆地理编码

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

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class GetAdreesByLatAndLong {

    /**
     * <p>
     * Title: GetLocationMsg
     * </p>
     * <p>
     * Description:高德地图API
     * </p>
     *
     * @param longitude
     * @param latitude
     * @return
     */
    public static String GetLocationMsg(double longitude, double latitude) {

        String message = "";

        String address = "";

        // 高德地图逆地理编码API

        String url = String.format(
                "https://restapi.amap.com/v3/geocode/regeo?output=JSON&key=你的key&radius=1000&extensions=all&batch=false&roadlevel=0&location=%s,%s",
                longitude, latitude);

        URL myURL = null;

        URLConnection httpsConn = null;

        try {

            myURL = new URL(url);

        } catch (MalformedURLException e) {

            e.printStackTrace();

        }

        try {

            httpsConn = (URLConnection) myURL.openConnection();
            httpsConn.setConnectTimeout(100000);
            if (httpsConn != null) {

                InputStreamReader insr = new InputStreamReader(

                        httpsConn.getInputStream(), "UTF-8");

                BufferedReader br = new BufferedReader(insr);

                String data = null;

                while ((data = br.readLine()) != null) {

                    message = message + data;
                }

                JsonParser jp = new JsonParser();
                //将json字符串转化成json对象
                JsonObject jo = jp.parse(message).getAsJsonObject();

                String status = jo.get("status").getAsString();

                String addressJsonEle = jo.get("regeocode").getAsJsonObject().get("formatted_address").toString();

                if (addressJsonEle.equals("[]")) {
                    address = null;

                } else {


                    if (jo.get("regeocode").getAsJsonObject().get("pois").getAsJsonArray().size() <= 0) {
                        String detail = jo.get("regeocode").getAsJsonObject().get("addressComponent").getAsJsonObject().get("streetNumber").getAsJsonObject().get("street").getAsString() + jo.get("regeocode").getAsJsonObject().get("addressComponent").getAsJsonObject().get("streetNumber").getAsJsonObject().get("number").getAsString();

                        if (status.equals("1") && !addressJsonEle.equals("[]")) {
                            address = addressJsonEle + " " + detail;

                        }

                    } else {
                        String detail = jo.get("regeocode").getAsJsonObject().get("pois").getAsJsonArray().get(0).getAsJsonObject().get("name").getAsString();

                        String detailDistance = jo.get("regeocode").getAsJsonObject().get("pois").getAsJsonArray().get(0).getAsJsonObject().get("distance").getAsString();


                        if (status.equals("1") && !addressJsonEle.equals("[]")) {
                            address = addressJsonEle + " " + detail + " " + detailDistance.substring(0, detailDistance.lastIndexOf(".")) + "米";

                        }
                    }
                }

                insr.close();

            }

        } catch (

                IOException e) {

            e.printStackTrace();

        }

        return address;

    }

    /**
     * <p>
     * Title: GetLocationMsgs
     * </p>
     * <p>
     * Description:百度地图aPI
     * </p>
     *
     * @param longitude
     * @param latitude
     * @return
     */
    public static String GetLocationMsgs(double latitude, double longitude) {

        String message = "";

        String address = "";

        // 百度地图逆地理编码API
        String url = String.format(
                "http://api.map.baidu.com/reverse_geocoding/v3/?ak=你的ak&extensions_poi=1&radius=1000&output=json&coordtype=bd09ll&location=%s,%s",
                latitude, longitude);

        URL myURL = null;

        URLConnection httpsConn = null;

        try {

            myURL = new URL(url);

        } catch (MalformedURLException e) {

            e.printStackTrace();

        }

        try {

            httpsConn = (URLConnection) myURL.openConnection();

            if (httpsConn != null) {

                InputStreamReader insr = new InputStreamReader(

                        httpsConn.getInputStream(), "UTF-8");

                BufferedReader br = new BufferedReader(insr);

                String data = null;

                while ((data = br.readLine()) != null) {

                    message = message + data;

                }

                JsonParser jp = new JsonParser();
                //将json字符串转化成json对象
                JsonObject jo = jp.parse(message).getAsJsonObject();

                String status = jo.get("status").getAsString();

                if (jo.get("result").getAsJsonObject().get("pois").getAsJsonArray().size() <= 0) {
                    String adds = jo.get("result").getAsJsonObject().get("formatted_address").getAsString();
                    address = adds;

                } else {
                    JsonElement addressJsonEle = jo.get("result").getAsJsonObject().get("addressComponent");

                    String adds = jo.get("result").getAsJsonObject().get("formatted_address").getAsString();

                    String details = jo.get("result").getAsJsonObject().get("pois").getAsJsonArray().get(0).getAsJsonObject().get("name").getAsString() + " " +
                            jo.get("result").getAsJsonObject().get("pois").getAsJsonArray().get(0).getAsJsonObject().get("distance").getAsString() + "米";

                    if (status.equals("0")) {
                        address = adds + " " + details;
                    }
                }
                
                insr.close();

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

        return address;

    }

    /**
     * <p>
     * Title: GetLocationMsgForForeign
     * </p>
     * <p>
     * Description: ArcGIS地图
     * </p>
     *
     * @param longitude
     * @param latitude
     * @return
     */
    public static String GetLocationMsgForForeign(double longitude, double latitude) {

        String message = "";

        String address = "";

        // 高德地图逆地理编码API

        String url = String.format(
                "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/reverseGeocode?f=pjson&langCode=EN&featureTypes=&location=%s,%s",
                longitude, latitude);

        URL myURL = null;

        URLConnection httpsConn = null;

        try {

            myURL = new URL(url);

        } catch (MalformedURLException e) {

            e.printStackTrace();

        }

        try {

            httpsConn = (URLConnection) myURL.openConnection();
            httpsConn.setConnectTimeout(100000);
            if (httpsConn != null) {

                InputStreamReader insr = new InputStreamReader(

                        httpsConn.getInputStream(), "UTF-8");

                BufferedReader br = new BufferedReader(insr);

                String data = null;

                while ((data = br.readLine()) != null) {

                    message = message + data;

                }

                JsonParser jp = new JsonParser();
                //将json字符串转化成json对象
                JsonObject jo = jp.parse(message).getAsJsonObject();

                if (!jo.has("error")) {
                    address = jo.get("address").getAsJsonObject().get("LongLabel").getAsString();

                }
                    
            
                insr.close();

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

        return address;

    }

    public final static void main(String[] args) {
        // 百度经纬度和高德地图经纬度,位置相反
        //114.196517, 22.380436
        String gaodeResult = GetLocationMsg(114.065240, 25.189741);
        String baiduResult = GetLocationMsgs(25.189741, 114.065240);
        String result = GetLocationMsgForForeign(114.065240, 25.189741);
        System.out.println("高德地址===" + gaodeResult);
        System.out.println("百度地址===" + baiduResult);
        System.out.println("国际地址===" + result);

    }

}

 

以上是关于Java百度地图高德地图ArcGIS地图逆地理编码的主要内容,如果未能解决你的问题,请参考以下文章

高德地图:地理/逆地理编码

Android高德通过经纬度获取地理位置

高德地图javascriptAPI基本使用心得(下)

Flutter 调用高德地图APP实现位置搜索路线规划逆地理编码

Flutter 调用高德地图APP实现位置搜索路线规划逆地理编码

在地图API中如何用经纬度来制定路线?