oak-common-aspect/lib/map/amap.js

72 lines
2.4 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
const AmapSDK_1 = tslib_1.__importDefault(require("oak-external-sdk/lib/AmapSDK"));
const AMap_1 = require("oak-external-sdk/lib/types/AMap");
const geo_1 = require("oak-domain/lib/utils/geo");
class AMap {
keys;
index;
constructor(keys) {
(0, assert_1.default)(keys.length > 0);
this.keys = keys.map(ele => ({
...ele,
count: AMap_1.WebServiceKeyWeight[ele.type],
}));
this.index = 0;
}
getKey() {
while (true) {
const key = this.keys[this.index];
if (key.count > 0) {
key.count--;
return key.key;
}
key.count = AMap_1.WebServiceKeyWeight[key.type];
this.index = (this.index + 1) % this.keys.length;
}
}
async regeo(params) {
const key = this.getKey();
const instance = AmapSDK_1.default.getInstance(key);
/**
* https://lbs.amap.com/api/webservice/guide/api/georegeo
*/
const gcj02Tudes = (0, geo_1.wgs84togcj02)([params.longitude, params.latitude]);
const { regeocode } = await instance.regeo({
longitude: gcj02Tudes[0],
latitude: gcj02Tudes[1],
});
return {
poiName: regeocode.formatted_address,
areaId: regeocode.addressComponent.adcode, // towncode的编码不是很规范
};
}
;
async geo(params) {
const key = this.getKey();
const instance = AmapSDK_1.default.getInstance(key);
/**
* https://lbs.amap.com/api/webservice/guide/api/georegeo
*/
const { name, areaId } = params;
const { geocodes } = await instance.geocode({
address: name,
city: areaId,
});
return geocodes.map((ele) => {
const { location } = ele;
const tudes = location.split(',').map((ele) => parseFloat(ele));
const wgs84Tudes = (0, geo_1.gcj02towgs84)(tudes);
return {
poiName: ele.formatted_address,
areaId: ele.adcode,
longitude: wgs84Tudes[0],
latitude: wgs84Tudes[1],
};
});
}
}
exports.default = AMap;