更新获取位置接口,补充接口文档
This commit is contained in:
@ -1,45 +0,0 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2020
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
|
||||
*/
|
||||
package co.yixiang.utils;
|
||||
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
|
||||
/**
|
||||
* @ClassName LocationUtils
|
||||
* @Author hupeng <610796224@qq.com>
|
||||
* @Date 2020/3/24
|
||||
**/
|
||||
public class LocationUtils {
|
||||
private static double EARTH_RADIUS = 6378.137;
|
||||
|
||||
private static double rad(double d) {
|
||||
return d * Math.PI / 180.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过经纬度获取距离(单位:千米)
|
||||
* @param lat1
|
||||
* @param lng1
|
||||
* @param lat2
|
||||
* @param lng2
|
||||
* @return
|
||||
*/
|
||||
public static double getDistance(double lat1, double lng1, double lat2,
|
||||
double lng2) {
|
||||
double radLat1 = rad(lat1);
|
||||
double radLat2 = rad(lat2);
|
||||
double a = radLat1 - radLat2;
|
||||
double b = rad(lng1) - rad(lng2);
|
||||
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
|
||||
+ Math.cos(radLat1) * Math.cos(radLat2)
|
||||
* Math.pow(Math.sin(b / 2), 2)));
|
||||
s = s * EARTH_RADIUS;
|
||||
s = Math.round(s * 10000d) / 10000d;
|
||||
//s = s*1000;
|
||||
return NumberUtil.round(s,2).doubleValue();
|
||||
//return s;
|
||||
}
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
package co.yixiang.utils.location;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author :LionCity
|
||||
* @date :Created in 2020-09-10 11:39
|
||||
* @description:
|
||||
* @modified By:
|
||||
* @version:
|
||||
*/
|
||||
@Data
|
||||
@ApiModel(value="腾讯地图返回对象")
|
||||
public class GetTencentLocationVO implements Serializable {
|
||||
|
||||
/**
|
||||
* result : {"level":2,"similarity":0.8,"ad_info":{"adcode":"441302"},"reliability":1,"location":{"lng":114.38257,"lat":23.08464},"deviation":1000,"title":"惠城区","address_components":{"province":"广东省","city":"惠州市","street":"","district":"惠城区","street_number":""}}
|
||||
* message : query ok
|
||||
* status : 0
|
||||
*/
|
||||
@ApiModelProperty(value = "地址解析结果")
|
||||
private ResultBean result;
|
||||
@ApiModelProperty(value = "状态说明")
|
||||
private String message;
|
||||
@ApiModelProperty(value = "状态码,0为正常")
|
||||
private int status;
|
||||
|
||||
@Data
|
||||
@ApiModel(value="地址解析结果")
|
||||
public static class ResultBean {
|
||||
/**
|
||||
* level : 2
|
||||
* similarity : 0.8
|
||||
* ad_info : {"adcode":"441302"}
|
||||
* reliability : 1
|
||||
* location : {"lng":114.38257,"lat":23.08464}
|
||||
* deviation : 1000
|
||||
* title : 惠城区
|
||||
* address_components : {"province":"广东省","city":"惠州市","street":"","district":"惠城区","street_number":""}
|
||||
*/
|
||||
|
||||
@ApiModelProperty(value = "解析到的坐标")
|
||||
private LocationBean location;
|
||||
@ApiModelProperty(value = "解析精度级别,分为11个级别,一般>=9即可采用(定位到点,精度较高) 也可根据实际业务需求自行调整")
|
||||
private int level;
|
||||
@ApiModelProperty(value = "即将下线,由reliability代替")
|
||||
private double similarity;
|
||||
|
||||
@ApiModelProperty(value = "行政区划信息")
|
||||
private AdInfoBean ad_info;
|
||||
@ApiModelProperty(value = "可信度参考")
|
||||
private int reliability;
|
||||
@ApiModelProperty(value = "即将下线,由level代替")
|
||||
private int deviation;
|
||||
@ApiModelProperty(value = "区信息")
|
||||
private String title;
|
||||
@ApiModelProperty(value = "行政区划信息")
|
||||
private AddressComponentsBean address_components;
|
||||
|
||||
|
||||
@Data
|
||||
@ApiModel(value="行政区划信息")
|
||||
public static class AdInfoBean {
|
||||
/**
|
||||
* adcode : 441302
|
||||
*/
|
||||
@ApiModelProperty(value = "行政区划代码")
|
||||
private String adcode;
|
||||
|
||||
}
|
||||
@Data
|
||||
@ApiModel(value="解析到的坐标")
|
||||
public static class LocationBean {
|
||||
/**
|
||||
* lng : 114.38257
|
||||
* lat : 23.08464
|
||||
*/
|
||||
|
||||
@ApiModelProperty(value = "经度")
|
||||
private double lng;
|
||||
|
||||
@ApiModelProperty(value = "纬度")
|
||||
private double lat;
|
||||
|
||||
|
||||
}
|
||||
@Data
|
||||
@ApiModel(value="解析后的地址部件")
|
||||
public static class AddressComponentsBean {
|
||||
/**
|
||||
* province : 广东省
|
||||
* city : 惠州市
|
||||
* street :
|
||||
* district : 惠城区
|
||||
* street_number :
|
||||
*/
|
||||
@ApiModelProperty(value = "省")
|
||||
private String province;
|
||||
@ApiModelProperty(value = "市")
|
||||
private String city;
|
||||
@ApiModelProperty(value = "街道,可能为空字串")
|
||||
private String street;
|
||||
@ApiModelProperty(value = "区,可能为空字串")
|
||||
private String district;
|
||||
@ApiModelProperty(value = "门牌,可能为空字串")
|
||||
private String street_number;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package co.yixiang.utils.location;
|
||||
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import co.yixiang.constant.ShopKeyUtils;
|
||||
import co.yixiang.exception.BadRequestException;
|
||||
import co.yixiang.utils.RedisUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
/**
|
||||
* 具体查看 https://lbs.qq.com/service/webService/webServiceGuide/webServiceGeocoder
|
||||
* @author :LionCity
|
||||
* @date :Created in 2020-09-10 11:46
|
||||
* @description:
|
||||
* @modified By:
|
||||
* @version:
|
||||
*/
|
||||
public class LocationUtils {
|
||||
|
||||
private static double EARTH_RADIUS = 6378.137;
|
||||
|
||||
private static double rad(double d) {
|
||||
return d * Math.PI / 180.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 腾讯地图地址解析
|
||||
*/
|
||||
private static final String QQ_MAP_URL = "https://apis.map.qq.com/ws/geocoder/v1/";
|
||||
|
||||
|
||||
/**
|
||||
* 通过经纬度获取距离(单位:千米)
|
||||
*
|
||||
* @param lat1
|
||||
* @param lng1
|
||||
* @param lat2
|
||||
* @param lng2
|
||||
* @return
|
||||
*/
|
||||
public static double getDistance(double lat1, double lng1, double lat2,
|
||||
double lng2) {
|
||||
double radLat1 = rad(lat1);
|
||||
double radLat2 = rad(lat2);
|
||||
double a = radLat1 - radLat2;
|
||||
double b = rad(lng1) - rad(lng2);
|
||||
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
|
||||
+ Math.cos(radLat1) * Math.cos(radLat2)
|
||||
* Math.pow(Math.sin(b / 2), 2)));
|
||||
s = s * EARTH_RADIUS;
|
||||
s = Math.round(s * 10000d) / 10000d;
|
||||
//s = s*1000;
|
||||
return NumberUtil.round(s, 2).doubleValue();
|
||||
//return s;
|
||||
}
|
||||
|
||||
|
||||
public static GetTencentLocationVO getLocation(String addr) {
|
||||
String key = RedisUtil.get(ShopKeyUtils.getTengXunMapKey());
|
||||
if (StrUtil.isBlank(key)) throw new BadRequestException("请先配置腾讯地图key");
|
||||
String url = StrUtil.format("?address={}&key={}", addr, key);
|
||||
String json = HttpUtil.get(QQ_MAP_URL + url);
|
||||
return JSONObject.parseObject(json, GetTencentLocationVO.class);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user