sql 根据经纬度计算两点的距离

1.sql

round(2 * Asin(Sqrt(power(Sin((出发点纬度参数- 目的地纬度参数) * 3.1415926 / 180 / 2),2) + Cos(出发点纬度参数 * 3.1415926 / 180) * Cos(目的地纬度参数* 3.1415926 / 180) * power(Sin((出发点经度参数 - 目的地经度参数) * 3.1415926 / 180 / 2),2))) * 6378.137,1) AS distance

2.java 腾讯地图经纬度获取省市区

package com.kinloou.utils;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class LocationUtils {

    // key
    private static final String KEY = "5DFBZ-NLNWS-2MZOF-6GKJQ-IENQZ-5VFHX";

    /**
     * @Description: 通过经纬度获取位置
     * @Param: [lng, lat]
     * @return: java.lang.String
     * @Author: kinloou
     */
    public static Map<String, Object> getLocation(String lng, String lat) {

        Map<String, Object> resultMap = new HashMap<String, Object>();

        // 参数解释:lng:经度,lat:维度。KEY:腾讯地图key,get_poi:返回状态。1返回,0不返回
        String urlString = "http://apis.map.qq.com/ws/geocoder/v1/?location=" + lat + "," + lng + "&key=" + KEY + "&get_poi=1";
        String result = "";
        try {
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            // 腾讯地图使用GET
            conn.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            // 获取地址解析结果
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
        } catch (Exception e) {
            e.getMessage();
        }

        // 转JSON格式
        JSONObject jsonObject = JSONObject.parseObject(result).getJSONObject("result");
        // 获取地址(行政区划信息) 包含有国籍,省份,城市
        JSONObject adInfo = jsonObject.getJSONObject("ad_info");
        resultMap.put("nation", adInfo.get("nation"));
        resultMap.put("nationCode", adInfo.get("nation_code"));
        resultMap.put("province", adInfo.get("province"));
        resultMap.put("provinceCode", adInfo.get("adcode"));
        resultMap.put("city", adInfo.get("city"));
        resultMap.put("cityCode", adInfo.get("city_code"));
        return resultMap;
    }

    public static void main(String[] args) {

        // 测试  22.640294,114.011653
        String lng = "114.011653";//经度
        String lat = "22.640294";//维度
        Map<String, Object> map = getLocation(lng, lat);
        System.out.println(map);
        System.out.println("国   籍:" + map.get("nation"));
        System.out.println("国家代码:" + map.get("nationCode"));
        System.out.println("省   份:" + map.get("province"));
        System.out.println("省份代码:" + map.get("provinceCode"));
        System.out.println("城   市:" + map.get("city"));
        System.out.println("城市代码:" + map.get("cityCode"));
    }
}

 

posted @ 2021-11-07 10:54  keyboard达人  阅读(823)  评论(0编辑  收藏  举报