Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get browser's locale measurement units

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?

like image 917
gpcola Avatar asked Feb 15 '23 08:02

gpcola


1 Answers

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.

like image 83
Zeta Avatar answered Feb 18 '23 01:02

Zeta