I am getting driving distance from google map api v3 using this code.
distance11 = response.routes[0].legs[0].distance.text;
The code is working fine, but the output is coming in the format 300km. I need only the number 300. Please help me find a solution.
I already tried putting distance.value and simply distance but it didn't work.
Edit:.....
Also please tell me how to remove string from a variable using javascript. For instance getting only 300 from 300km.
Thanks!
For this particular case, you can try
parseInt(distance11);
parseInt
automatically removes the trailing string after the number. But if you know already that distance11
ends with "km"
, you can also do
distance11.slice(0, -2);
See the documentation , the distance service returns both the text (which you are trying to use) and a number in meters.
If you want other units than meters, you can convert it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.