0

I want to book an ambulance while having conversation with user via google assistant.I have to get the distance between the pickup point and destination point to calculate fare as per the kms.

I have the user's location in longitudes and latitudes. and the destination location as a address(Fortis Hospital at Bhoiwada, Kalyan). How do I calculate the distance?

'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 Answers1

0

Distance calculation is possible given lat/lng, but the problem is that it won't really give you the distance it will take an ambulance to drive from one to the other, it will only get you the straight-line distance.

What you really need is to calculate (I am assuming) a route distance. You can calculate this using the Google Maps "Direction" API (since you're clearly already using Google things). This will require a valid Google Maps API key! Luckily, this has already been answered on Stack Overflow.

The basic idea is to use the directions service to get the route, then look at then result object for the distance between the start and end points (note that this code is hijacked from that other answer):

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));
    }
});
Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
  • thank you.. Actually I'm working with this platform for the first time. Can you please guide me further? I have written the code in the fullfillment (in inline editor) of dialogflow. I'm confused as in where do I put this link (https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places). And Also how do I access the user location which is stored in coordinates.longitudes in another intent to make distance calculation. It would be a great help for me. – RUTUJA RANDIVE Jul 08 '19 at 15:59