Commit 3a56496c by wnb0128

车家智能数据服务 用户经纬度转换--think.wang

1 parent ee5b9ba6
package com.glsx.smart.config;
import com.glsx.smart.pojo.pb.InstanceFence.Fence;
import com.glsx.smart.utils.GCJ2WGS;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import lombok.extern.slf4j.Slf4j;
......@@ -8,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
/**
* @author think.wang
......@@ -35,14 +37,18 @@ public class DeviceAddressProducer {
*/
public void pushUserDeviceData(String deviceId, String lngAndLat, Integer value) {
//kafka 发送消息数据封装
String[] strArr;
HashMap<String, Double> hm;
if(value == Fence.OperationType.DEL_VALUE
|| value == Fence.OperationType.GET_VALUE){ //删除和获取不需要经纬度数据
strArr = new String[]{"0", "0"};
hm = new HashMap<>(2);
hm.put("lat", Double.valueOf(0));
hm.put("lon", Double.valueOf(0));
} else{
strArr = lngAndLat.split(":");
GCJ2WGS wg = new GCJ2WGS();
String[] lngLat = lngAndLat.split(":");
hm = wg.delta(Double.valueOf(lngLat[0]), Double.valueOf(lngLat[1]));
}
Fence.GpsLoc gpsLoc = this.buildGpsLoc(strArr[0], strArr[1]);
Fence.GpsLoc gpsLoc = this.buildGpsLoc(hm.get("lon"), hm.get("lat"));
Fence.FenceDeviceFamilyInfo deviceFamilyInfo = this.buildFenceDevice(deviceId, gpsLoc);
Fence.OptFamilyFenceInfo familyFenceInfo = this.buildFamilyFence(value, deviceFamilyInfo);
try {
......@@ -57,16 +63,14 @@ public class DeviceAddressProducer {
/**
* 围栏设备经纬度数据封装
* @param lngStr
* @param latStr
* @param lng
* @param lat
* @return
*/
private Fence.GpsLoc buildGpsLoc(String lngStr, String latStr){
double lng = Double.valueOf(lngStr) * 1000000;
double lat = Double.valueOf(latStr) * 1000000;
private Fence.GpsLoc buildGpsLoc(double lng, double lat){
Fence.GpsLoc.Builder gpsLocBuilder = Fence.GpsLoc.newBuilder();
gpsLocBuilder.setLng((int)lng);
gpsLocBuilder.setLat((int)lat);
gpsLocBuilder.setLng((int)(lng * 1000000));
gpsLocBuilder.setLat((int)(lat * 1000000));
return gpsLocBuilder.build();
}
......
package com.glsx.smart.utils;
import org.apache.log4j.Logger;
import java.math.BigDecimal;
import java.util.HashMap;
/**
* 高德点转原始经纬度
*
* @author zc
* @version 1.0
* @history 2017/4/10 create by zc
*/
public class GCJ2WGS {
private static Logger logger = Logger.getLogger(GCJ2WGS.class);
public static void main(String[] args) {
GCJ2WGS wg = new GCJ2WGS();
HashMap<String, Double> hm = wg.delta(30.815040, 104.082125);
System.out.println(hm);
}
//圆周率 GCJ_02_To_WGS_84
double PI = 3.14159265358979324;
/**
* @param lat ,lon 需要转换的经纬度
* @return 转换为真实GPS坐标后的经纬度
* @throws <异常类型> {@inheritDoc} 异常描述
* @author 作者:
* 方法描述:方法可以将高德地图SDK获取到的GPS经纬度转换为真实的经纬度
*/
public HashMap<String, Double> delta(double lat, double lon) {
logger.info("delta before:"+"lat="+lat+" lon="+lon);
double a = 6378245.0;
double ee = 0.00669342162296594323;
double dLat = this.transformLat(lon - 105.0, lat - 35.0);
double dLon = this.transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * this.PI;
double magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);
HashMap<String, Double> hm = new HashMap<String, Double>();
hm.put("lat", formatDouble((lat - dLat),6));
hm.put("lon", formatDouble((lon - dLon),6));
logger.info("delta after:"+"lat="+hm.get("lat")+" lon="+hm.get("lon"));
return hm;
}
//转换经度
public double transformLon(double x, double y) {
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
return ret;
}
//转换纬度
public double transformLat(double x, double y) {
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
return ret;
}
/**
* 格式化数字
* @param x 待格式数字
* @param num 小数点位数
*/
public static Double formatDouble(double x, int num) {
BigDecimal b = new BigDecimal(x);
return b.setScale(num, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!