从OpenLayers坐标输出度数分钟,但没有秒?

时间:2018-06-04 09:26:17

标签: javascript html5 openlayers openlayers-3

方法PlacementTarget将以罗盘方向的分数,分钟和秒的格式输出,例如Popup

但有没有办法在没有秒的情况下输出度数和分钟数?截断秒是不合适的,因为它会忽略舍入。

e.g。以上示例应该舍入到(control as DatePicker).FontSize = 24; (control as DatePicker).FontWeight = FontWeights.Light; (control as DatePicker).MinWidth = 450; (control as DatePicker).VerticalAlignment = VerticalAlignment.Center; (control as DatePicker).Loaded += (ss, ee) => { DatePicker dp = (DatePicker)ss; System.Windows.Controls.Primitives.Popup popup = dp.Template.FindName("PART_Popup", dp) as System.Windows.Controls.Primitives.Popup; Button button = dp.Template.FindName("PART_Button", dp) as Button; if (popup != null && button != null) popup.PlacementTarget = button; }; 但是截断秒会错误地给出ol.coordinate.toStringHDMS()

2 个答案:

答案 0 :(得分:0)

您需要自己创建函数

答案 1 :(得分:0)

建议的派生函数:

function toStringHDM(coordinate) {
  if (coordinate) {
      return (degreesToStringHDM('NS', coordinate[1]) +
          ' ' +
          degreesToStringHDM('EW', coordinate[0]));
  }
  else {
      return '';
  }
}

和:

function degreesToStringHDM(hemispheres, degrees, opt_fractionDigits = 0) {
  var normalizedDegrees = modulo(degrees + 180, 360) - 180;
  var x = Math.abs(3600 * normalizedDegrees);
  var dflPrecision = opt_fractionDigits || 0;
  var precision = Math.pow(10, dflPrecision);
  var deg = Math.floor(x / 3600);
  var min = Math.floor((x - deg * 3600) / 60);
  var sec = x - deg * 3600 - min * 60;
  sec = Math.ceil(sec * precision) / precision;
  if (sec >= 60) {
      sec = 0;
      min += 1;
  }
  if (min >= 60) {
      min = 0;
      deg += 1;
  }
  return (deg +
      '\u00b0 ' +
      padNumber(min, 2) +
      '\u2032 ' +
      (normalizedDegrees == 0
          ? ''
          : ' ' + hemispheres.charAt(normalizedDegrees < 0 ? 1 : 0)));
}

请注意:假设编译器会为我完成这项工作,我故意在 degreeToStringHDM() 中留下了很多死代码,以使其尽可能接近 OpenLayers 的原始函数 degreeToStringHDMS(),如果您需要将来比较两者(例如,当 OpenLayers 将发布此功能的新版本时,可能会修复一些错误,您可能有兴趣执行 diff 以便在这里快速找到需要修复的内容...)< /p>