distanceToolEx.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import * as maptalks from 'maptalks'
  2. class DistanceToolEx extends maptalks.DistanceTool {
  3. constructor(options) {
  4. super(options)
  5. this._measureLayers = []
  6. }
  7. _measure(toMeasure) {
  8. const map = this.getMap()
  9. let length
  10. if (toMeasure instanceof maptalks.Geometry) {
  11. length = map.computeGeometryLength(toMeasure)
  12. } else if (Array.isArray(toMeasure)) {
  13. length = map.getProjection().measureLength(toMeasure)
  14. }
  15. this._lastMeasure = length
  16. let units
  17. if (this.options['language'] === 'zh-CN') {
  18. units = [' 米', ' 公里', ' 英尺', ' 英里']
  19. } else {
  20. units = [' m', ' km', ' feet', ' mile']
  21. }
  22. let content = ''
  23. if (this.options['metric']) {
  24. content += length < 1000 ? length.toFixed(3) + units[0] : (length / 1000).toFixed(2) + units[1]
  25. }
  26. if (this.options['imperial']) {
  27. length *= 3.2808399
  28. if (content.length > 0) {
  29. content += '\n'
  30. }
  31. content += length < 5280 ? length.toFixed(3) + units[2] : (length / 5280).toFixed(2) + units[3]
  32. }
  33. return content
  34. }
  35. }
  36. export default DistanceToolEx