I have a value in km and I wish to convert that to the browser's locale specific unit of measurement (km or miles).
I'm using google maps API v3 directionsService, specifically the last example on this page.
function computeTotalDistance(result) {
var total = 0;
var myroute = result.routes[0];
for (i = 0; i < myroute.legs.length; i++) {
total += myroute.legs[i].distance.value;
}
total = total / 1000.
document.getElementById("total").innerHTML = total + " km";
}
How would I determine the browser's locale unit of measurement so I can perform a conversion if necessary?
I guess the easiest way is to check window.navigator.language
and check whether the user has chosen "en-US"
or "my"
, as most countries have introduces the metric system (see this map). You could also use GeoIP, but all in all you should provide the user an option to change the system if he prefers the other one.
The browser itself doesn't store any measurement units preferences.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With