如何从Google地图的折线(Polyline)中获取适当的整数距离值?

huangapple go评论78阅读模式
英文:

How to get proper integer value of distance from Polyline in Google Map?

问题

好的,以下是您要求的翻译内容:

我从折线中获取距离的方法如下:

distance = polylineData.getLeg().distance.toString();

我得到的字符串可能是"44.5 km"或者"123 km"。然后,我将其解析为双精度浮点数,如下:

distance = distance.replaceAll("[^-?0-9]+", " ");
distance = distance.replaceAll(" ", ".");
distance = distance.substring(0, distance.length() - 1);
double dist = new Double(Double.parseDouble(distance));

当距离大于等于1000 km时,我得到的字符串是"1.378 km",经过解析后得到"1.378",但我需要得到"1378",我无法找到解决方法。

英文:

Ok, I'm getting distance from polyline like this:

distance = polylineData.getLeg().distance.toString();

I get String like "44.5 km" or "123 km". After that I parse it to Double like this:

distance = distance.replaceAll("[^-?0-9]+", " ");
distance = distance.replaceAll(" ", ".");
distance = distance.substring(0, distance.length() - 1);
double dist = new Double(Double.parseDouble(distance));

I get Double like "44.5" or "123.0" and everything is fine. But,
if distance is 1000 km and above I get String like "1.378 km" and after parsing I get "1.378", but I need "1378" and I cannot find the solution to fix it.

答案1

得分: 0

使用inMeters字段替代调用toString()方法:

double distanceInMeters = polylineData.getLeg().distance.inMeters;

如果需要以千米为单位的距离,请除以1000.0。

英文:

Use the inMeters field instead of calling toString():

double distanceInMeters = polylineData.getLeg().distance.inMeters;

If you need the distance in km, divide by 1000.0

huangapple
  • 本文由 发表于 2020年8月20日 23:33:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63508483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定