|
@@ -1,57 +1,69 @@
|
|
|
import AMapLoader from '@amap/amap-jsapi-loader';
|
|
|
import $config from "@/config/index.js"
|
|
|
+import gcoord from 'gcoord';
|
|
|
|
|
|
export const getCoord = (parmas) => {
|
|
|
return new Promise((reslove, reject) => {
|
|
|
- const { name, code, type } = parmas;
|
|
|
- window._AMapSecurityConfig = {
|
|
|
- securityJsCode: $config.secretKey,
|
|
|
- };
|
|
|
- // 配置高德API的key
|
|
|
- AMapLoader.load({
|
|
|
- "key": $config.key,
|
|
|
- "version": "2.0",
|
|
|
- "plugins": []
|
|
|
- })
|
|
|
- .then((AMap) => {
|
|
|
- AMap.plugin('AMap.Geocoder', function() {
|
|
|
- var geocoder = new AMap.Geocoder({
|
|
|
- city: code // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
|
|
|
- });
|
|
|
-
|
|
|
- var addresses = [name];
|
|
|
- // 使用高德逆编码,将天地图转换成高德地图
|
|
|
- geocoder.getLocation(addresses, function(status, result) {
|
|
|
- if (status === 'complete' && result.info === 'OK') {
|
|
|
- const val = result.geocodes[0];
|
|
|
- reslove(val.location)
|
|
|
- }else{
|
|
|
- reject()
|
|
|
- }
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- // var gps = [longitude, latitude]; //需要转换的gps类型的坐标
|
|
|
- // // var gps = [116.3, 39.9]; //需要转换的gps类型的坐标
|
|
|
- // // 初始化地图实例
|
|
|
- // console.log('AMap =', AMap, gps)
|
|
|
- // // window.map = new AMap.Map('container', {
|
|
|
- // // zoom: 10, // 设置地图显示的缩放级别
|
|
|
- // // center: [116.397428, 39.90923] // 设置地图中心点坐标
|
|
|
- // // });
|
|
|
- // //参数说明:需要转换的坐标或者坐标组,需要转换的坐标类型,转换成功后的回调函数
|
|
|
- // AMap.convertFrom(gps, "gps", function(status, result) {
|
|
|
- // console.log('status, result =', status, result)
|
|
|
- // //status:complete 表示查询成功,no_data 为查询无结果,error 代表查询错误
|
|
|
- // //查询成功时,result.locations 即为转换后的高德坐标系
|
|
|
- // if (status === "complete" && result.info === "ok") {
|
|
|
- // var lnglats = result.locations; //转换后的高德坐标 Array.<LngLat>
|
|
|
- // console.log(lnglats);
|
|
|
- // }
|
|
|
- // });
|
|
|
- })
|
|
|
- .catch(e => {
|
|
|
- reject()
|
|
|
- });
|
|
|
+ const { name, code, type, longitude, latitude } = parmas;
|
|
|
+ console.log("name, code, type = ", longitude, latitude)
|
|
|
+ switch (type) {
|
|
|
+ case 'gaode':
|
|
|
+ // const [gcjLat, gcjLon] = wgs84ToGcj02(latitude, longitude);
|
|
|
+ // console.log("[gcjLat, gcjLon] = ", gcjLat, gcjLon)
|
|
|
+
|
|
|
+ const convertedCoords = gcoord.transform(
|
|
|
+ [longitude, latitude],
|
|
|
+ gcoord.WGS84, // 输入坐标系
|
|
|
+ gcoord.GCJ02 // 输出坐标系
|
|
|
+ );
|
|
|
+ console.log('convertedCoords = ', convertedCoords)
|
|
|
+ const data = {
|
|
|
+ lng: convertedCoords[0],
|
|
|
+ lat: convertedCoords[1]
|
|
|
+ }
|
|
|
+ reslove(data)
|
|
|
+
|
|
|
+ }
|
|
|
})
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+function wgs84ToGcj02(lat, lon) {
|
|
|
+ const a = 6378245.0;
|
|
|
+ const ee = 0.00669342162296594323;
|
|
|
+
|
|
|
+ function transform(lat, lon) {
|
|
|
+ let dLat = transformLat(lon - 105.0, lat - 35.0);
|
|
|
+ let dLon = transformLon(lon - 105.0, lat - 35.0);
|
|
|
+ const radLat = lat / 180.0 * Math.PI;
|
|
|
+ let magic = Math.sin(radLat);
|
|
|
+ magic = 1 - ee * magic * magic;
|
|
|
+ const sqrtMagic = Math.sqrt(magic);
|
|
|
+ dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * Math.PI);
|
|
|
+ dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * Math.PI);
|
|
|
+ return [lat + dLat, lon + dLon];
|
|
|
+ }
|
|
|
+
|
|
|
+ function transformLat(x, y) {
|
|
|
+ let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y;
|
|
|
+ ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
|
|
|
+ ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0;
|
|
|
+ ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0;
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ function transformLon(x, y) {
|
|
|
+ let 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 * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
|
|
|
+ ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0;
|
|
|
+ ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0;
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (lon < 72.004 || lon > 137.8347 || lat < 0.8293 || lat > 55.8271) {
|
|
|
+ return [lat, lon]; // 不在国内,不转换
|
|
|
+ }
|
|
|
+
|
|
|
+ return transform(lat, lon);
|
|
|
}
|