英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论