如何在谷歌地图中使用函数返回距离 [英] how to returning distance with function in google maps

查看:159
本文介绍了如何在谷歌地图中使用函数返回距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从此链接获得脚本这里,我想让函数返回距离,这是我的实际脚本:

  var calcRoute =函数(原点,目的地){
var dist;
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
var request = {
origin:origin,
destination:destination,
travelMode:google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request,function(response,status){
if(status == google.maps.DirectionsStatus.OK){
directionsDisplay.setDirections(response);
dist = response.routes [0] .legs [0] .distance.value / 1000;
}
});
return dist;
};
$ scope.resto = calcRoute( - 7.048357,110.418877, - 7.048443,110.441022);

有两个参数我放入函数中,我想函数返回距离但 p>

return dist;



值来自

dist = response.routes [0] .legs [0] .distance.value / 1000



我在View中使用 angularjs resto 不显示距离,任何人都可以帮助我,请 直到 directionsService.route c>函数被执行,函数 calcRoute 已经执行并返回 dist ,这将是未定义的。



您将在 directionsService.route



您可以将另一个参数(回调函数)添加到 calcRoute 函数中。现在,一旦 directionsService.route 得到响应,您可以将该值传递给这个新的回调函数。


$ b 试试这个。

  var calcRoute = function(origin,destination ,cb){
var dist;
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
var request = {
origin:origin,
destination:destination,
travelMode:google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request,function(response,status){
if(status == google.maps.DirectionsStatus.OK){
directionsDisplay.setDirections(response);
cb(null,response.routes [0] .legs [0] .distance.value / 1000);
}
else {
cb('pass error information');
}
});
};
calcRoute( - 7.048357,110.418877, - 7.048443,110.441022,function(err,dist){
if(!err){
$ scope.resto = dist;
}
});


i've script from this link here and i want to make the function returning distance, this's my actually script :

var calcRoute = function(origin,destination) {
    var dist;
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    directionsDisplay = new google.maps.DirectionsRenderer();
  var request = {
    origin:origin,
    destination:destination,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
      dist = response.routes[0].legs[0].distance.value / 1000;
    }
  });
    return dist;
};
$scope.resto = calcRoute("-7.048357, 110.418877","-7.048443, 110.441022");

there are two parameters that I put in function, and i want the function returning the distance but

return dist;

in the script not returning the value from

dist = response.routes[0].legs[0].distance.value / 1000

i'm using angularjs and the resto in my View not showing the distance, anyone help me please, there's something wrong with the script or anything else ?

解决方案

Until the directionsService.route function is executed the function calcRoute has already executed and returned dist which will be undefined.

You will get the value inside the callback function of directionsService.route

You can add another parameter (a callback function) to calcRoute function. Now once directionsService.route gets the response you can pass the value to this new callback function.

Try this.

var calcRoute = function(origin,destination,cb) {
    var dist;
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    directionsDisplay = new google.maps.DirectionsRenderer();
  var request = {
    origin:origin,
    destination:destination,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
      cb(null, response.routes[0].legs[0].distance.value / 1000);
    }
    else {
      cb('pass error information');
    }
  });
};
calcRoute("-7.048357, 110.418877","-7.048443, 110.441022", function (err, dist) {
    if (!err) {        
      $scope.resto = dist;
    }
});

这篇关于如何在谷歌地图中使用函数返回距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆