Parse.com Cloudcode,计算两个位置之间的行驶距离
Parse.com Cloudcode, calculate driving distance between two locations
我有一个beforeSave函数,class的一个字段是一个GeoPoint,我想找到之前可用的数据和即将更新的数据之间的行驶距离。我知道我需要使用 Google 地图等服务来查找行驶距离。但我不明白如何在云代码中使用第三方 js 或使用来自云代码的 https 请求调用 Google 方向服务。我该怎么做。谢谢
解决方案是使用来自 google 个项目的 api 键调用 google 地图的 REST 服务。
我的代码是这样的。
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();
});
我有一个beforeSave函数,class的一个字段是一个GeoPoint,我想找到之前可用的数据和即将更新的数据之间的行驶距离。我知道我需要使用 Google 地图等服务来查找行驶距离。但我不明白如何在云代码中使用第三方 js 或使用来自云代码的 https 请求调用 Google 方向服务。我该怎么做。谢谢
解决方案是使用来自 google 个项目的 api 键调用 google 地图的 REST 服务。
我的代码是这样的。
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();
});