簡體   English   中英

Node.js api - 我們可以根據谷歌地圖獲得兩個緯度之間的准確距離嗎?

[英]Node.js api - Can we get accuracy distance between two latlong as per google map giving result in distance?

在我們的移動應用程序中具有基於位置的功能。 所以我實現了 npm geodist 來獲取兩個坐標之間的距離(由半正弦公式使用)。 geodist 距離結果與谷歌地圖結果不同。 我可以按照提供的相同谷歌地圖在 npm geodist 中獲得距離結果嗎? 請任何人對此提供幫助。 提前致謝

var geodist = require('geodist')
var distance = geodist({lat: 11.0145, lon: 76.9864}, {lat: 11.0167, lon: 76.9774}, {exact: true, unit: 'km'})

我在精度模式下得到 1.01 公里的結果。

但是我在谷歌地圖中測試了兩個地方之間的相同 - 'Ukkadam Bus Shed, Ukkadam, Coimbatore, Tamil Nadu 641008' 和'Noble Business Centre, Avinashi Rd, PN Palayam, Coimbatore, Tamil Nadu 641037' - 它給出的結果5.8 公里

我就是這樣做的。 首先,獲得需要距離值的結果。

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
        });
    } 

這就是連接到 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);
        });
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM