Skip to content

TotalDistance as Int #2

@Dave181295

Description

@Dave181295

I'm stuck for hours trying to get the total distance driving
here is my code :

function getDistanceBetweenPoints(userLat, userLng, resLat, resLng) {
    var params = {
        origin: { userLat, userLng },
        destination: { resLat, resLng },
        key: "AIzaSyDeKxeg2T-aM2oHSdwk-iIffE_YFLDYX6k",
        units: 'metric'
    };
    
    return new Promise((resolve, reject) => {

        map.getDistance(params, (err, data) => {
            if (err) {
                reject(err);
            }
		 
            resolve(data);
        });
    });
}
getDistance: function(params, cb) {
		// validate inputs to npm module
		var validateErr = validateInput(params, cb);
		if (validateErr) {
			return cb(validateErr);
		}

		// build query URL
		var url = "https://maps.googleapis.com/maps/api/directions/json?" + qs.stringify(params);

			// make request to google server
			request(url, function(err, res, body) {

			var jsonRes = JSON.parse(body)
			for(i = 0; i < jsonRes.routes[0].legs.length; i++){
		   distance += parseFloat(jsonRes.routes[0].legs[i].distance.value);
		}
				return cb(err, distance); 
			});

	},

what's wrong ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions