javascript - 将 Google map 高程输出转换为英尺而不是米

标签 javascript google-maps math google-maps-api-3

这只是我的项目中的一小段代码,但它当前显示 map 和海拔栏。标高栏当前显示公制,而不是英制。显然,我已经定义了帝国。有什么想法吗?

     ds.route({
  origin: from,
  destination: to,
  travelMode: google.maps.DirectionsTravelMode.DRIVING,
  unitSystem: google.maps.UnitSystem.IMPERIAL
}, function(result, status) {
  if (status == google.maps.DirectionsStatus.OK) {
      fitBounds = true;
      dr.setDirections(result);
    }
}); 

更新:此信息是否会传递到 Google 的海拔服务?看来如果你在这里定义帝国,它应该贯彻下去。

最佳答案

您的代码返回海拔米。 要将其转换为英制,以下将米转换为英尺。

var writeStats = function(r) {
  var prevElevation = r[0].elevation;
  var climb = 0;
  var drop = 0;
  var max = 0;
  for (var i = 1; i < r.length; i++) {
    var diff = r[i].elevation - prevElevation;
    prevElevation = r[i].elevation;
    if (diff > 0) {
      climb += diff;
      climb = Math.abs(climb / 1) ;//Object to number
      var  climb2 = climb  * 3.2808399;//Metres to feet

    }
    else {
      drop -= diff;
      drop = Math.abs(drop / 1) ;
      var  drop2 = drop  * 3.2808399;

    }

    if (r[i].elevation > max) {
      max = r[i].elevation;
    }
  }
  max = Math.ceil(max);
  $('#climb-drop').text("Climb: " + Math.round(climb2) + " feet Drop: " + Math.round(drop2) + " feet");
  return max;
};

enter image description here

关于javascript - 将 Google map 高程输出转换为英尺而不是米,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18777491/

相关文章:

javascript - 谷歌地图仅针对正纬度/经度值绘制,如果传递负纬度/经度,则重置 NaN

javascript - 设置一个 HTML5 Video 元素来覆盖?

javascript - 使用 fitBounds 时 Google Maps v3 : Enforcing min. 缩放级别

javascript - 如何防止Javascript点击事件发生两次?

php - 根据价格给出 0 到 10 的平均评分

java - Java 中的字符运算

java - 数组列表中的斐波那契数列

javascript - 编辑器 : Programatically close Find Dialog Box

javascript - JS : pass variable onclick

javascript - 谷歌地图API显示折线