如何获取用户位置与其目的地点之间的距离(以公里为单位)

时间:2019-07-07 13:06:53

标签: node.js package.json

我想在通过Google助手与用户交谈时预订一辆救护车。我必须获取接机点与目的地点之间的距离,才能按公里数计算票价。

我在经度和纬度上都有用户的位置。并将目的地位置作为地址(位于Kalyan Bhoiwada的富通医院)。如何计算距离?

'use strict';

const functions = require('firebase-functions');
const { dialogflow } = require('actions-on-google');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const {Permission,Place} = require('actions-on-google');
const app = dialogflow();


app.intent('Default Welcome Intent', conv => {
  conv.close(`Welcome to my agent!`);
  conv.ask('which type of ambulance would you like to book?');
});

app.intent('Default Fallback Intent', conv=> {
  conv.close('sorry i did not understand');
});

app.intent('typeofambulance',conv => {
  conv.ask('ac or non-ac?');
});

app.intent('ac', conv => {
  conv.ask('do you want to book it from your current location?');
});

app.intent('location', (conv) => {

    conv.data.requestedPermission = 'DEVICE_PRECISE_LOCATION';
    return conv.ask(new Permission({
    context: 'to locate you',
    permissions: conv.data.requestedPermission,
    }));

});
app.intent('receive', (conv, params, permissionGranted) => {
  //conv.ask('rr');
    if (permissionGranted) {
     // conv.ask('entered');
        const {
        requestedPermission
    } = conv.data;
      const {
        coordinates
    } = conv.device.location;

     conv.ask(`You are at latitude ${coordinates.latitude} and longitude 
  ${coordinates.longitude}` );
     conv.ask('destination please!');
  } else {
   return conv.close('Sorry, permission denied.');
 }
});

app.intent('destination', conv => {
     conv.ask(new Place({
    prompt: 'Destination point?',
    context: 'To find a place to pick you up',
  }));
});

app.intent('actions.intent.PLACE', (conv, input, place, status) => {

 if (place) {
    conv.ask(` Ah, I see. You want to get dropped at 
 ${place.formattedAddress}`);
     const ll=place.formattedAddress;

 }

  else {
    // Possibly do something with status
    conv.ask(`Sorry, I couldn't find where you want to get picked up`);
  }

});

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

1 个答案:

答案 0 :(得分:0)

Distance calculation is possible given lat/lng,但是问题在于,它并不能真正给您一辆救护车从一个人开车到另一个人的距离,它只会为您提供直线距离。

您真正需要的是计算(我假设)路线距离。您可以使用Google Maps "Direction" API进行计算(因为您显然已经在使用Google的东西)。 这将需要有效的Google Maps API密钥!幸运的是,它具有already been answered on Stack Overflow

基本思想是使用路线服务获取路线,然后在结果对象中查看起点和终点之间的距离(请注意,此代码是从其他答案中劫持的):

const directionsService = new DirectionsService;
const origin = "start address";
const destination = "end address";
const waypoints = addresses.map(stop => ({location: stop}));

directionsService.route({
    origin,
    waypoints,
    destination,
    travelMode: TravelMode.DRIVING,
}, (response, status) => {
    if(status === DirectionsStatus.OK) {
        let totalDistance = 0;
        const legs = directionsResult.routes[0].legs;
        for(let i=0; i<legs.length; ++i) {
            totalDistance += legs[i].distance.value;
        }
        console.log(totalDistance);

    } else {
        return reject(new Error(status));
    }
});