Vue+OpenLayers 创建地图并显示鼠标所在经纬度

1、效果

2、创建地图

本文用的是高德地图

页面

dart 复制代码
 <div class="map" id="map"></div>
                    <div id="mouse-position" class="position_coordinate"></div>

初始化地图

go 复制代码
     var gaodeLayer = new TileLayer({
          title: "高德地图",
          source: new XYZ({
            url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
            wrapX: false
          })
        });
        this.map = new Map({
          layers: [gaodeLayer],
          target: 'map',
          view: new View({
            center: transform([123.23, 25.73], 'EPSG:4326', 'EPSG:3857'), //地图初始中心点
            projection: 'EPSG:3857',
            zoom: 4,
            minZoom: 1

          }),
        });

3、添加经纬度

go 复制代码
   var mousePositionControl = new MousePosition({
          coordinateFormat: function (coordinate) {
            return formatAxirs(coordinate, '经度:{x} 纬度:{y}', 2);
          },
          projection: 'EPSG:4326',
          className: "custom-mouse-position",
          target: document.getElementById("mouse-position"), //将位置数据放到那里
          undefinedHTML: "",
        });
        this.map.addControl(mousePositionControl);

4、完整代码

javascript 复制代码
<script>
import { Map, View } from "ol";

    import TileLayer from 'ol/layer/Tile.js';

  import XYZ from 'ol/source/XYZ.js';
  import { get as getProjection, transform } from 'ol/proj.js';
    import MousePosition from "ol/control/MousePosition";
   import { format as formatAxirs } from 'ol/coordinate';
  export default {
    data() {
  return {
         map: null,
        draw: null,
      };
    },
    mounted() {
      this.initMap();

    },
      methods: {
     //初始化地图
      initMap() {
        var gaodeMapLayer = new TileLayer({
          title: "高德地图",
          source: new XYZ({
            url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
            wrapX: false
          })
        });
        this.map = new Map({
          layers: [gaodeMapLayer],
          target: 'map',
          view: new View({
            center: transform([103.23, 35.33], 'EPSG:4326', 'EPSG:3857'), //地图初始中心点
            projection: 'EPSG:3857',
            zoom: 4,
            minZoom: 1

          }),
        });

        // 获取鼠标在地图的经纬度
        var mousePositionControl = new MousePosition({
          coordinateFormat: function (coordinate) {
            return formatAxirs(coordinate, '经度:{x} 纬度:{y}', 2);
          },
          projection: 'EPSG:4326',
          className: "custom-mouse-position",
          target: document.getElementById("mouse-position"), //将位置数据放到那里
          undefinedHTML: "",
        });
        this.map.addControl(mousePositionControl);
      },
}
}

附css代码

css 复制代码
  .position_coordinate {
    color: #6a6a6a;
    position: absolute;
    font-size: 14px;
    bottom: 20px;
    right: 20px;
    z-index: 999;
    text-align: center;
    line-height: 30px;
  }
相关推荐
阿猫的故乡8 小时前
Vue过渡动画从入门到装X:淡入淡出、滑动、列表动画、第三方库全搞定
前端·javascript·vue.js
裕波8 小时前
Vue&ViteConf 2026 将于 7 月 18 日在上海举办,尤雨溪将现场发表主题演讲
vue.js·vite
如果超人不会飞10 小时前
TinyRobot SuggestionPills紧凑的建议按钮组组件
前端·vue.js
如果超人不会飞10 小时前
TinyRobot Container构建优雅的AI对话容器
前端·vue.js
如果超人不会飞10 小时前
TinyRobot SuggestionPopover智能建议弹出框组件
前端·vue.js
zhedream12 小时前
从模糊到清晰:一次组件重构里的开发哲学
vue.js
如果超人不会飞12 小时前
TinyRobot AI 对话组件库全组件使用指南
前端·vue.js
如果超人不会飞13 小时前
Vue.js
vue.js