微信GPS坐标(腾讯坐标)上报,调用百度api得到具体地址

源码照搬可用,记得改上你的秘钥

package com.sxtx.weixin.controller;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;



/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: renkaijiang
 * Date: 2018-07-27
 * Time: 14:27
 */
public class BaiduUtil {
    
    public static JSONObject getLocationInfo(String lat, String lng) {
        Model str = map_tx2bd(Double.parseDouble(lat), Double.parseDouble(lng));

        String url = "http://api.map.baidu.com/geocoder/v2/?location=" + str.lat

                + "," + str.lng

                + "&output=json&ak=你的秘钥&pois=0";

        JSONObject obj = JSONObject.parseObject(HttpUtil.get(url));

        System.out.println("\n"+"经纬度查询地址为:" + url);

        System.out.println("\n"+"经纬度坐标返回结果为:" + obj.toString());

        return obj;



    }



    public static String getAddress(String lng, String lat) {  //lng经度,lat纬度

        JSONObject obj = getLocationInfo(lat, lng).getJSONObject("result");

        StringBuilder address = new StringBuilder();

        String province = obj.getJSONObject("addressComponent").getString(

                "province");

        String city = obj.getJSONObject("addressComponent").getString("city");

        if (province != null && city != null && province.equals(city)) { 	//直辖市的省份名称和城市名称一样,判断重复时去掉

            address.append(

                    obj.getJSONObject("addressComponent").getString("city"))

                    .append(obj.getJSONObject("addressComponent").getString(

                            "district"))

                    .append(obj.getJSONObject("addressComponent").getString(

                            "street"))

                    .append(obj.getString("sematic_description"));

        } else {

            address.append(

                    obj.getJSONObject("addressComponent").getString("province"))

                    .append(obj.getJSONObject("addressComponent").getString(

                            "city"))

                    .append(obj.getJSONObject("addressComponent").getString(

                            "district"))

                    .append(obj.getJSONObject("addressComponent").getString(

                            "street"))

                    .append(obj.getString("sematic_description"));

        }



        return address.toString();

    }

    /**
     * 坐标转换,腾讯地图转换成百度地图坐标
     * @param lat 腾讯纬度
     * @param lon 腾讯经度
     * @return 返回结果:经度,纬度
     */
    public static Model map_tx2bd(double lat, double lon){
        double bd_lat;
        double bd_lon;
        double x_pi=3.14159265358979324;
        double x = lon, y = lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        bd_lon = z * Math.cos(theta) + 0.0065;
        bd_lat = z * Math.sin(theta) + 0.006;

        System.out.println("bd_lat:"+bd_lat);
        System.out.println("bd_lon:"+bd_lon);
        Model model = new Model();
        model.setLat(String.valueOf(bd_lat));
        model.setLng(String.valueOf(bd_lon));
        return model;
    }


    public static void main(String[] args) {




        System.out.println("\n"+"经纬度具体地址为:"

                + BaiduUtil.getAddress("106.571245", "29.597566"));



}

}
class Model {
    String lat;
    String lng;

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getLng() {
        return lng;
    }

    public void setLng(String lng) {
        this.lng = lng;
    }
}

 

你可能感兴趣的:(第三方api)