使用纬度和经度的距离 = NaN(在某些情况下)
Distance using Latitude and Longitude = NaN (in some cases)
两点:
{ lat: -6.346640110015869, lng: -39.293800354003906 }
{ lat: 66.450861, lng: 143.261551 }
使用流行的方法:
getDistanceBetweenPoints(location1: ILocation, location2: ILocation): number {
const lat1: number = location1.lat,
lng1: number = location1.lng,
lat2: number = location2.lat,
lng2: number = location2.lng,
R = 6378137,
dLat: number = this.toRadians(lat2 - lat1),
dLong: number = this.toRadians(lng2 - lng1),
const a: number =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(this.toRadians(lat1)) *
Math.cos(this.toRadians(lat1)) *
Math.sin(dLong / 2) *
Math.sin(dLong / 2);
// temp helpers
const xx = Math.sqrt(1 - a); ----> a = 1.339 => sqrt(-number) = NaN
const yy = Math.sqrt(a);
const zz = Math.atan2(xx, yy);
const c: number = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance: number = (R * c) / 1000;
我在调试模式下发现问题:
a > 1
Math.sqrt(1 - a) -> sqrt(<0) = NaN
意思是,a不能>1。
但是如何解决呢?
map screen
我希望这会奏效(基于维基百科article):
const RADIUS_OF_EARTH = 6378137;
const from = { lat: -6.346640110015869, lng: -39.293800354003906 };
const to = { lat: 66.450861, lng: 143.261551 };
const distance = (from, to) => {
const latF = from.lat / 180 * Math.PI;
const lngF = from.lng / 180 * Math.PI;
const latT = to.lat / 180 * Math.PI;
const lngT = to.lng / 180 * Math.PI;
const latD = Math.abs(latF - latT);
const lngD = Math.abs(lngF - lngT);
const latH = Math.pow(Math.sin(latD / 2), 2);
const lngH = Math.pow(Math.sin(lngD / 2), 2);
const delta = 2 * Math.asin(Math.sqrt(latH + Math.cos(latF) * Math.cos(latT) * lngH));
return RADIUS_OF_EARTH * delta;
};
console.log(distance(from, to));
两点:
{ lat: -6.346640110015869, lng: -39.293800354003906 }
{ lat: 66.450861, lng: 143.261551 }
使用流行的方法:
getDistanceBetweenPoints(location1: ILocation, location2: ILocation): number {
const lat1: number = location1.lat,
lng1: number = location1.lng,
lat2: number = location2.lat,
lng2: number = location2.lng,
R = 6378137,
dLat: number = this.toRadians(lat2 - lat1),
dLong: number = this.toRadians(lng2 - lng1),
const a: number =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(this.toRadians(lat1)) *
Math.cos(this.toRadians(lat1)) *
Math.sin(dLong / 2) *
Math.sin(dLong / 2);
// temp helpers
const xx = Math.sqrt(1 - a); ----> a = 1.339 => sqrt(-number) = NaN
const yy = Math.sqrt(a);
const zz = Math.atan2(xx, yy);
const c: number = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance: number = (R * c) / 1000;
我在调试模式下发现问题:
a > 1
Math.sqrt(1 - a) -> sqrt(<0) = NaN
意思是,a不能>1。 但是如何解决呢?
map screen
我希望这会奏效(基于维基百科article):
const RADIUS_OF_EARTH = 6378137;
const from = { lat: -6.346640110015869, lng: -39.293800354003906 };
const to = { lat: 66.450861, lng: 143.261551 };
const distance = (from, to) => {
const latF = from.lat / 180 * Math.PI;
const lngF = from.lng / 180 * Math.PI;
const latT = to.lat / 180 * Math.PI;
const lngT = to.lng / 180 * Math.PI;
const latD = Math.abs(latF - latT);
const lngD = Math.abs(lngF - lngT);
const latH = Math.pow(Math.sin(latD / 2), 2);
const lngH = Math.pow(Math.sin(lngD / 2), 2);
const delta = 2 * Math.asin(Math.sqrt(latH + Math.cos(latF) * Math.cos(latT) * lngH));
return RADIUS_OF_EARTH * delta;
};
console.log(distance(from, to));