Prechádzať zdrojové kódy

添加本地坐标转换;添加请求头

huangrf 5 rokov pred
rodič
commit
57d9dd8957
2 zmenil súbory, kde vykonal 56 pridanie a 1 odobranie
  1. 55 1
      third/amap/amap.go
  2. 1 0
      utils/http_util.go

+ 55 - 1
third/amap/amap.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
 	"github.com/pkg/errors"
+	"math"
 )
 
 const(
@@ -56,7 +57,7 @@ func (a *AMapClient)Regeo(lngLats []string)(*RegeoResult, error){
 }
 
 /**
- * @brief: 坐标转换
+ * @brief: 坐标转换(通过请求高德接口)
  * @param1 lngLats: 经纬度列表
  * @param2 coordType: 坐标类型 gps;mapbar;baidu;autonavi
  * @return1: 坐标转回结果
@@ -86,6 +87,28 @@ func (a *AMapClient)ConvCoord(lngLats []string, coordType string)(*ConvCoordResu
 	return result, nil
 }
 
+/**
+ * @brief: 把gps转成高德地图坐标
+ * @param1 gpsLng: gps 经度
+ * @param2 gpsLat: gps 纬度
+ * @return1: 高德地图经度
+ * @return2: 高德地图纬度
+ */
+func (a *AMapClient)ConvGpsToAMap(gpsLng, gpsLat float64)(float64, float64){
+	if outOfChinal(gpsLng, gpsLat){
+		return gpsLng, gpsLat
+	}
+	dLat := transformLat(gpsLng - 105.0, gpsLat - 35.0)
+	dLon := transformLon(gpsLng - 105.0, gpsLat - 35.0)
+	radLat := gpsLat / 180.0 * pi
+	magic := math.Sin(radLat)
+	magic = 1 - ee * magic * magic
+	sqrtMagic := math.Sqrt(magic)
+	dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi)
+	dLon = (dLon * 180.0) / (a / sqrtMagic * math.Cos(radLat) * pi)
+	return gpsLng + dLon, gpsLat + dLat;
+}
+
 /**
  * @brief: 获取天气
  * @param1 city: 区编码
@@ -174,3 +197,34 @@ func (a *AMapClient)getLocations(lngLats []string)string{
 
 	return location
 }
+
+//////////////////////////工具函数///////////////////////////////
+const(
+	pi = 3.14159265358979324
+	a = 6378245.0
+	ee = 0.00669342162296594323
+)
+
+func outOfChinal(gpsLng, gpsLat float64)bool{
+	if gpsLng < 72.004 || gpsLng > 137.8347{
+		return true;
+	}
+	if gpsLat < 0.8293 || gpsLat > 55.8271{
+		return true;
+	}
+	return false;
+}
+func transformLat(x, y float64) float64 {
+	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*pi) + 20.0*math.Sin(2.0*x*pi)) * 2.0 / 3.0
+	ret += (20.0*math.Sin(y*pi) + 40.0*math.Sin(y/3.0*pi)) * 2.0 / 3.0
+	ret += (160.0*math.Sin(y/12.0*pi) + 320*math.Sin(y*pi/30.0)) * 2.0 / 3.0
+	return ret
+}
+func transformLon(x, y float64) float64 {
+	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*pi) + 20.0*math.Sin(2.0*x*pi)) * 2.0 / 3.0
+	ret += (20.0*math.Sin(x*pi) + 40.0*math.Sin(x/3.0*pi)) * 2.0 / 3.0
+	ret += (150.0*math.Sin(x/12.0*pi) + 300.0*math.Sin(x/30.0*pi)) * 2.0 / 3.0
+	return ret
+}

+ 1 - 0
utils/http_util.go

@@ -35,6 +35,7 @@ func (this *HttpUtil) PostBytes(url string, data []byte) ([]byte, error) {
 		return []byte(""), err
 	}
 	request.Header.Set("Connection", "Keep-Alive")
+	request.Header.Set("Content-Type", "application/json;charset=utf-8")
 
 	return this.getResponse(request)
 }