Popup.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import Overlay from 'ol/Overlay'
  2. import KBaseObject from './KBaseObject';
  3. import * as proj from 'ol/proj'
  4. class Popup extends KBaseObject{
  5. constructor(lng,lat,url,offsetX,offsetY,width,height,mapInstance = null){
  6. super(mapInstance)
  7. const vm = this;
  8. vm.position = [lng,lat];
  9. vm.initPopup(lng,lat,url,offsetX,offsetY,width,height);
  10. }
  11. initPopup(lng,lat,url,offsetX,offsetY,width,height){
  12. //创建popup的容器
  13. const vm = this;
  14. vm.popupContainer = document.createElement("div");
  15. vm.popupContainer.style.position = "absolute";
  16. vm.popupContainer.style.width = width + "px";
  17. vm.popupContainer.style.height = height + "px";
  18. vm.popupContainer.style.top = "0px";
  19. vm.popupContainer.style.left = "0px";
  20. vm.popupContainer.style.zIndex = 1;
  21. // vm.popupContainer.style.display = "none";
  22. vm.popupContainer.style.background = "url(" + url + ") no-repeat";
  23. vm.popupContainer.style.backgroundSize = "100% 100%";
  24. // vm.popupContainer.innerHTML = "点击查看";
  25. //创建popup
  26. vm.popup = new Overlay({
  27. element: vm.popupContainer,
  28. positioning: 'center-center',
  29. stopEvent: false,
  30. position: proj.fromLonLat([lng,lat]),
  31. offset: [offsetX,offsetY]
  32. });
  33. vm.mapInstance.map.addOverlay(vm.popup);
  34. }
  35. setPostionAnimate(lng,lat,duration = 1000){
  36. const vm = this;
  37. let curLng = vm.position[0];
  38. let curLat = vm.position[1];
  39. let nextLng = lng;
  40. let nextLat = lat;
  41. // 计算经纬度的中间值差距
  42. let latDiff = (nextLng - curLng) / 100;
  43. let lngDiff = (nextLat - curLat) / 100;
  44. let j = 0;
  45. // 在当前坐标和下一个坐标之间插入 100 个中间值的经纬度
  46. let timer = setInterval(function (){
  47. let x = curLng + j * latDiff;
  48. let y = curLat + j * lngDiff;
  49. vm.popup.setPosition(proj.fromLonLat([x,y]));
  50. j++;
  51. if(j == 100){
  52. clearInterval(timer)
  53. }
  54. }, duration / 100)
  55. }
  56. setPosition(x,y){
  57. this.popup.setPosition(proj.fromLonLat([x,y]));
  58. }
  59. insertMiddleValues(coordinates) {
  60. // 初始化一个新的数组
  61. let result = [];
  62. // 循环遍历所有的坐标
  63. for (let i = 0; i < coordinates.length - 1; i++) {
  64. // 获取当前坐标和下一个坐标
  65. let current = coordinates[i];
  66. let next = coordinates[i + 1];
  67. // 计算经纬度的中间值差距
  68. let latDiff = (next[0] - current[0]) / 200;
  69. let lngDiff = (next[1] - current[1]) / 200;
  70. // 将当前坐标添加到结果数组
  71. result.push(current);
  72. // 在当前坐标和下一个坐标之间插入 200 个中间值的经纬度
  73. for (let j = 1; j < 200; j++) {
  74. let lat = current[0] + j * latDiff;
  75. let lng = current[1] + j * lngDiff;
  76. result.push([lat, lng]);
  77. }
  78. }
  79. // 添加最后一个坐标到结果数组
  80. result.push(coordinates[coordinates.length - 1]);
  81. return result;
  82. }
  83. remove(){
  84. const vm = this;
  85. if(vm.popup){
  86. vm.mapInstance.map.removeOverlay(vm.popup);
  87. vm.popup = null;
  88. }
  89. }
  90. }
  91. export default Popup;