admin管理员组

文章数量:1355068

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 puteTotalDistance(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 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 puteTotalDistance(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?

Share Improve this question edited Sep 8, 2013 at 13:29 gpcola asked Sep 8, 2013 at 12:53 gpcolagpcola 1,0095 gold badges16 silver badges27 bronze badges 1
  • 1 I think you should work on the browser data (location) and create an array for each country. – Mr.Web Commented Sep 8, 2013 at 13:01
Add a ment  | 

2 Answers 2

Reset to default 7

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.

As the maps API defaults to the UnitSystem of the unit system of the origin's country I'm going to use the following unless anyone has a better solution:

var metric = myroute.legs[0].distance.text.indexOf(" km") != -1?true:false;

I know it's not particularly elegant but seems to be reliable and it doesn't require any data I don't already have at my disposal.

I tried looking for a way to get the actual UnitSystem that the directionsService used but without success.

From API documentation:

Unit Systems

By default, directions are calculated and displayed using the unit system of the origin's country or region. (Note: origins expressed using latitude/longitude coordinates rather than addresses always default to metric units.) For example, a route from "Chicago, IL" to "Toronto, ONT" will display results in miles, while the reverse route will display results in kilometers. You may override this unit system by setting one explicitly within the request using one of the following UnitSystem values:

UnitSystem.METRIC specifies usage of the metric system. Distances are shown using kilometers. UnitSystem.IMPERIAL specifies usage of the Imperial (English) system. Distances are shown using miles. Note: this unit system setting only affects the text displayed to the user. The directions result also contains distance values, not shown to the user, which are always expressed in meters.

本文标签: javascriptHow to get browser39s locale measurement unitsStack Overflow