在js中从Firestore获取附近的位置

时间:2019-10-29 10:45:59

标签: javascript node.js express google-cloud-firestore

我想从我的Firebase Firestore数据库中获取快递中的附近位置。 我创建了如下距离函数:

function distance(location1, location2) {
    const radius = 6371; // Earth's radius in kilometers
    const latDelta = degreesToRadians(location2.latitude - location1.latitude);
    const lonDelta = degreesToRadians(location2.longitude - location1.longitude);

    const a = (Math.sin(latDelta / 2) * Math.sin(latDelta / 2)) +
        (Math.cos(degreesToRadians(location1.latitude)) * Math.cos(degreesToRadians(location2.latitude)) *
            Math.sin(lonDelta / 2) * Math.sin(lonDelta / 2));

    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    return radius * c;
}

然后我以快递方式创建了端点:

app.post('/locatii', (req, res) => {
    let places = [];
    const locatii = db.collection('locatii');
    const locatie1 = {
        lat: req.body.lat,
        lon: req.body.lon
    };
    locatii.get().then(snap => {
        //add the nearby location in the array
    )
}];

我想知道如何实现距离函数以提取数据。谢谢!

0 个答案:

没有答案