In our mobile application has location based features. So i had implemented npm geodist for getting distance (used by haversine formula) between two coordinates. The geodist distance result is different with google map result. Can i get the distance result in npm geodist as per same google map has provided. Please anybody help regarding this. Thanks in advance
var geodist = require('geodist')
var distance = geodist({lat: 11.0145, lon: 76.9864}, {lat: 11.0167, lon: 76.9774}, {exact: true, unit: 'km'})
I am getting result in 1.01 km in accuracy mode.
But the same i had tested in google map between two places from - 'Ukkadam Bus Shed, Ukkadam, Coimbatore, Tamil Nadu 641008' and to 'Noble Business Centre, Avinashi Rd, PN Palayam, Coimbatore, Tamil Nadu 641037' - its giving result 5.8 km
That's how I did it. First, getting results that requiring distance values.
public static getDetails = async (req: Request, res: Response) => {
const id = req.params.id;
// Our application's user current position received from browser
const latitude = req.query.lat;
const longitude = req.query.long;
await new sql.ConnectionPool(CommonConstants.connectionString).connect().then(pool => {
return pool.request()
.input('Id', sql.Int, id)
.execute('GetSupplierDetails')
}).then(result => {
sql.close();
const rows = result.recordset.map(async (supplier) => {
const data = { origin: [latitude, longitude], destination: [supplier.Latitude, supplier.Longitude] }; // Setting coordinates here
const distance = await GetDistance(data) || 0;
Object.defineProperty(supplier, 'Distance', {
enumerable: true,
configurable: true,
writable: true,
value: distance
});
return supplier;
});
Promise.all(rows).then((values) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.status(200).json(values);
});
}).catch(err => {
res.status(500).send({ message: err })
sql.close();
});
sql.on('error', err => {
// ... error handler
});
}
And that is the function that connects to Google Distance Matrix API.
import { Constants } from "./constants";
const https = require('https');
export async function GetDistance(coords) {
const { origin, destination } = coords;
return new Promise((resolve, reject) => {
https.get(`${Constants.GoogleMapsUrl}?origins=${origin[0]},${origin[1]}
&destinations=${destination[0]},${destination[1]}
&key=${Constants.GoogleMapsApiKey}`, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
const distance = JSON.parse(data);
resolve(distance.rows[0].elements[0].distance.value);
});
}).on("error", (err) => {
reject("Error: " + err.message);
});
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.