经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue+openlayer5获取当前鼠标滑过的坐标实现方法
来源:jb51  时间:2021/11/16 11:12:16  对本文有异议

前言:       

 在vue项目中怎么获取当前鼠标划过的坐标呢,这里来分享下方法。 实现效果:

实现步骤:

1、引入相关文件

  1. import MousePosition from 'ol/control/MousePosition';
  2. import {createStringXY} from 'ol/coordinate';

 2、生成地图

  1. var layers = [
  2. //深蓝色背景
  3. new TileLayer({
  4. source: new XYZ({
  5. url:
  6. "https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
  7. }),
  8. }),
  9. ];
  10. this.map = new Map({
  11. layers: layers,
  12. target: "map",
  13. view: new View({
  14. center: this.center,
  15. projection: this.projection,
  16. zoom: this.centerSize,
  17. maxZoom: 17,
  18. minZoom: 5,
  19. extent: [
  20. 73.32783475401652, 3.33795, 135.16017906160056,
  21. 53.83501005646246,
  22. ],
  23. }),
  24. });

3、加入鼠标事件

  1. var mousePositionControl = new MousePosition({
  2. coordinateFormat: createStringXY(6),//获取位置
  3. projection: 'EPSG:4326',
  4. className: 'custom-mouse-position',
  5. target: document.getElementById('mouse-position'), //将位置数据放到那里
  6. undefinedHTML: '&nbsp'
  7. });
  8. this.map.addControl(mousePositionControl);

4、页面上加入

  1. <div id="map" class="map" ref="imageDom">
  2. 位置div
  3. <div id="mouse-position" style="
  4. color: #fff;
  5. position: absolute;
  6. bottom:10px;
  7. right:10px;
  8. z-index: 10000000;
  9. width: 200px;
  10. line-height: 30px;
  11. background: rgba(0,0,0,0.5);
  12. "></div>
  13. </div>

到此这篇关于vue+openlayer5获取当前鼠标滑过的坐标的文章就介绍到这了,更多相关vue+openlayer5鼠标坐标内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号