a04a1b1166b61a1eafc32b52d9ee46347bd0f855.svn-base 759 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { cuttingString } from './util'
  2. function average(aver, curr, index) {
  3. aver[0] += curr[0]
  4. aver[1] += curr[1]
  5. if (index) {
  6. aver[0] /= 2
  7. aver[1] /= 2
  8. }
  9. return aver
  10. }
  11. function grentText(features) {
  12. let texts = []
  13. features.forEach(fe => {
  14. if (fe.properties.name) {
  15. let averagePoint = fe.geometry.coordinates.reduce((tPoint, geometry, index) => {
  16. return average(
  17. tPoint,
  18. geometry.reduce(average, [0, 0]),
  19. index
  20. )
  21. }, [0, 0])
  22. let name = cuttingString(fe.properties.name, 15)
  23. texts.push({
  24. name: name.join('\n'),
  25. point: averagePoint,
  26. height: fe.height + 3 * name.length + 5
  27. })
  28. }
  29. })
  30. return texts
  31. }
  32. export default grentText