Search code examples
javascriptgoogle-maps-api-3parse-platformparse-cloud-codegoogle-directions-api

Parse.com Cloudcode, calculate driving distance between two locations


I have a beforeSave function, and one field of the class is a GeoPoint, I want to find the driving distance between the previously available data and the about to be updated data. I understand I need to use some service like Google maps to find the driving distance. But I do not understand how to use a third party js inside the cloud code or call Google directions service with a https request from cloudcode. How do I go about this. Thanks.


Solution

  • The solution was to call the google maps' REST services with a api key from google projects.

    This is how my code went.

        var url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins="+oldLatitude+","+oldLongitude
    
            url = url+"&destinations="+newLatitude+","+newLongitude+"&key=YOUR_API_KEY";
    
    
    
       Parse.Cloud.httpRequest({
          url: url,
    
        }).then(function(httpResponse) {
    
                    var distance = (JSON.parse(httpResponse.text)).rows[0].elements[0].distance.value;
    
    
    
    
           response.success();
        }, function(err) {
          console.log(err);
    
                    response.success();
        });