export class TransformLngLat {
constructor() {
this.PI = 3.1415926535897932384626;
this.a = 6378245.0;
this.ee = 0.00669342162296594323;
}
wgs84ToGcj02(coordinate = []) {
const [lng, lat] = this.createStringToNumber(coordinate);
if (!lng || !lat || this.verifyOutOfChina(lng, lat)) return coordinate;
let dlat = this.createTransFormLat(lng - 105.0, lat - 35.0);
let dlng = this.createTransFormLng(lng - 105.0, lat - 35.0);
const radlat = lat / 180.0 * this.PI;
let magic = Math.sin(radlat);
magic = 1 - this.ee * magic * magic;
const sqrtmagic = Math.sqrt(magic);
dlat = (dlat * 180.0) / ((this.a * (1 - this.ee)) / (magic * sqrtmagic) * this.PI);
dlng = (dlng * 180.0) / (this.a / sqrtmagic * Math.cos(radlat) * this.PI);
const mglat = lat + dlat;
const mglng = lng + dlng;
return [mglng, mglat];
}
gcj02ToWgs84(coordinate = []) {
const [lng, lat] = this.createStringToNumber(coordinate);
if (!lng || !lat || this.verifyOutOfChina(lng, lat)) return coordinate;
let dlat = this.createTransFormLat(lng - 105.0, lat - 35.0);
let dlng = this.createTransFormLng(lng - 105.0, lat - 35.0);
const radlat = lat / 180.0 * this.PI;
let magic = Math.sin(radlat);
magic = 1 - this.ee * magic * magic;
const sqrtmagic = Math.sqrt(magic);
dlat = (dlat * 180.0) / ((this.a * (1 - this.ee)) / (magic * sqrtmagic) * this.PI);
dlng = (dlng * 180.0) / (this.a / sqrtmagic * Math.cos(radlat) * this.PI);
const mglat = lat + dlat;
const mglng = lng + dlng;
return [lng * 2 - mglng, lat * 2 - mglat];
}
verifyOutOfChina(lng, lat) {
return (lng < 72.004 || lng > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
}
createTransFormLng(lng, lat) {
let ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * this.PI) + 20.0 * Math.sin(2.0 * lng * this.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lng * this.PI) + 40.0 * Math.sin(lng / 3.0 * this.PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(lng / 12.0 * this.PI) + 300.0 * Math.sin(lng / 30.0 * this.PI)) * 2.0 / 3.0;
return ret;
}
createTransFormLat(lng, lat) {
let ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * this.PI) + 20.0 * Math.sin(2.0 * lng * this.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lat * this.PI) + 40.0 * Math.sin(lat / 3.0 * this.PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(lat / 12.0 * this.PI) + 320 * Math.sin(lat * this.PI / 30.0)) * 2.0 / 3.0;
return ret;
}
createStringToNumber(coordinates = []) {
if (!Array.isArray(coordinates) || coordinates.length === 0) return [];
return coordinates.map((v) => Array.isArray(v) ? this.createStringToNumber(v) : Number(v));
}
}
export function createSingleLnglat(coordinate = [], coordinateType = "gcj02") {
const transformLngLat = new TransformLngLat();
const transformFunc = coordinateType === "gcj02" ? "gcj02ToWgs84" : "wgs84ToGcj02";
return transformLngLat[transformFunc](coordinate);
}
export function createMultipleLnglat(coordinates = [], coordinateType = "gcj02") {
return coordinates.map(v => createSingleLnglat(v));
}