计算周围经纬度并转换成高德
程序员文章站
2022-03-18 20:39:46
...
计算周围经纬度并转换成高德
使用微信的定位获取当前经纬度后:
public ResultMap getUserAroundDeviceFileByLAL(Map hmap) {
try {
Map map = getPosition(hmap);
Double longitude = Double.parseDouble(map.get("longitude").toString());
Double latitude = Double.parseDouble(map.get("latitude").toString());
double[] a = getRectangle(longitude,latitude,dis);
double minlng = Double.parseDouble(String.format("%.8f", a[0]));
double maxlng = Double.parseDouble(String.format("%.8f", a[2]));
double minlat = Double.parseDouble(String.format("%.8f", a[1]));
double maxlat = Double.parseDouble(String.format("%.8f", a[3]));
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("longitude1", minlng);
columnMap.put("longitude2", maxlng);
columnMap.put("latitude1", minlat);
columnMap.put("latitude2", maxlat);
List<TbDeviceList> devicefieldList = tbDeviceGetewayListMapper.findDeviceFieldByLAL(columnMap);
QueryWrapper<TbUserBind> tbUserBindQueryWrapper = new QueryWrapper();
tbUserBindQueryWrapper.eq("open_id", map.get("openId"));
List<TbUserBind> TbUserBinds = tbUserBindMapper.selectList(tbUserBindQueryWrapper);
// 如果该设备以及被用户绑定,则设标识字段为1
for (TbDeviceList list : devicefieldList){
for (TbUserBind bind : TbUserBinds){
if (list.getDeviceId().equals(bind.getDeviceId())){
list.setIsBind(1);
}
}
}
return ResultMap.success(devicefieldList);
} catch (Exception e) {
return ResultMap.error("获取地图失败!");
}
}
使用到的两个方法
private Map getPosition(Map map) {
String url = "https://restapi.amap.com/v3/assistant/coordinate/convert?" +
"key=" + GaoDeKey + "&locations=" + map.get("longitude") + "," + map.get("latitude") + "&coordsys=gps";
String getResult = HttpUtil
.createGet(url)
.header("Content-Type", "application/form-data")
.execute()
.charset("UTF-8")
.body();
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(getResult);
if (jsonObject.getString("info").equals("ok")) {
String locations = jsonObject.getString("locations");
String[] a = locations.split(",");
map.put("longitude", a[0]);
map.put("latitude", a[1]);
} else {
log.error("转化坐标出现错误:getPosition");
}
return map;
}
public static double[] getRectangle(double lng, double lat, long distance) {
float delta = 111000;
if (lng != 0 && lat != 0) {
double lng1 = lng - distance
/ Math.abs(Math.cos(Math.toRadians(lat)) * delta);
double lng2 = lng + distance
/ Math.abs(Math.cos(Math.toRadians(lat)) * delta);
double lat1 = lat - (distance / delta);
double lat2 = lat + (distance / delta);
return new double[]{lng1, lat1, lng2, lat2};
} else {
double lng1 = lng - distance / delta;
double lng2 = lng + distance / delta;
double lat1 = lat - (distance / delta);
double lat2 = lat + (distance / delta);
return new double[]{lng1, lat1, lng2, lat2};
}
}
<select id="findDeviceFieldByLAL" resultType="com.example.entry.model.TbDeviceList">
SELECT
*
FROM
tb_device_list
WHERE
(longitude between #{longitude1} AND #{longitude2})
AND ( latitude between #{latitude1} AND #{latitude2} )
</select>
推荐阅读