1

谷歌地图 v3 方向服务返回的纬度/经度点有时比我想要的要长。这是我可能会打的电话:

var request = {
    origin: start_position, 
    destination: end_position,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
};

directionsService.route(request, function(response, status) {
    if (status === google.maps.DirectionsStatus.OK) {
        // do something with response, e.g. set the overview_path to a polyline;
    }
});

生成的响应可能会为一个点返回以下值:

response.routes[0].legs[0].start_location.Xa = 35.077000000000005    
response.routes[0].legs[0].start_location.Ya = -85.08854000000001

我真的想降低精度。例如,我更喜欢精确到小数点后五位的值,如下所示:

Xa = 35.07700    
Ya = -85.08854

有没有简单的方法来实现这一点?我查看了文档,但没有看到任何设置方法的提及。

感谢您提供的任何见解或帮助!

4

2 回答 2

3

首先,您不应该使用 .Xa 和 .Ya,因为它们是缩小的名称,可能会随着 API 的下一个版本而改变。相反,您应该使用记录在案的方法 lat() 和 lng()。所以:

response.routes[0].legs[0].start_location.lat()= 35.077000000000005    
response.routes[0].legs[0].start_location.lng() = -85.08854000000001

如果你想对小数进行四舍五入,那么你可以使用标准的 javascript 方法 .toFixed(),即:

var lat = response.routes[0].legs[0].start_location.lat().toFixed(5);// 35.07700    
var lon = response.routes[0].legs[0].start_location.lng().toFixed(5);// -85.08854
于 2012-08-31T04:36:54.790 回答
1

Javascript 使用双精度浮点数来表示数据。如果您将该数据保存为字符串,则使用 .toFixed(); 输出它可能是有意义的。但这将是您的代码,而不是 Google 的代码。

于 2012-08-31T13:08:43.750 回答