将Google Maps Elevation输出转换为英尺而不是米

时间:2013-09-13 02:49:01

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

这只是我项目中的代码片段,但它目前正在显示地图和高程栏。高程栏目前显示的是公制,而不是英制。显然,我已经定义了帝国主义。有什么想法吗?

     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的提升服务?看起来如果你在这里定义帝国,它应该贯彻。

1 个答案:

答案 0 :(得分:2)

您的代码会返回海拔高度表。 要将此转换为IMPERIAL,请将米转换为英尺。

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