4、openlayers6点击地图添加定位图标

<template>
  <div id="map"></div>
</template>

<script>
import "ol/ol.css";
import { Map, View, Feature } from "ol";
import XYZ from "ol/source/XYZ";
import { Vector as VectorSource } from "ol/source";
import { Vector as VectorLayer, Tile as TileLayer } from "ol/layer";
import { Style, Icon } from "ol/style";
import Point from "ol/geom/Point";
export default {
  data() {
    return {
      map: null,
      roadmap: null,
      vectorLayer: null, //创建图层
      vectorSource: null, //图层数据容器
      localtion: [114.028222,31.914839],
    }
  },
  methods: {
    initMap() {
      let _self = this;
      //线路图
      this.roadmap = new TileLayer({
        visible: true,
        name: '电子图',
        source: new XYZ({
          url: 服务地址+"/tiles/ZhengZhouShi/roadmap/{z}/{x}/{y}.png",
          crossOrigin: "anonymous", //离线瓦片跨域处理
        }),
      });
      this.map = new Map({
        target: "map",
        layers: [this.roadmap],
        view: new View({
          center: this.localtion;
          projection: "EPSG:4326",
          zoom: 14,
          minZoom: 12,
          maxZoom: 17,
        }),
      });

      // 创建图层
      this.vectorLayer = new VectorLayer();
      // 创建数据容器
      this.vectorSource = new VectorSource();
      // 把数据容器添加到图层
      this.vectorLayer.setSource(this.vectorSource);
      // 添加到地图上
      this.map.addLayer(this.vectorLayer);
      
      this.setPoint(this.localtion);
      
      //获取鼠标点击的坐标点
      this.map.on("singleclick", function (evt) {
        const coordinate = evt.coordinate;
        let longitude = coordinate[0];
        let latitude = coordinate[1];
        let Markers = { longitude, latitude };
        _self.setPoint(Markers);
      });
    },
    //添加定位图标
    setPoint(local) {
      this.pointLayer = new Feature({
        name: 'point点',
        geometry: new Point(local),
      });
      this.pointLayer.setStyle(
        new Style({
          image: new Icon({
            src: require("@/assets/image/marker-icon.png"),
          })
        })
      );
      this.vectorSource.addFeature(this.pointLayer);
      //定位过度动画
      this.map.getView().animate({
        center: local, // 中心点
        zoom: 14, // 缩放级别
        rotation: undefined, // 缩放完成view视图旋转弧度
        duration: 500, // 缩放持续时间,默认不需要设置
      });
    }
  }
};
</script>

<style scoped lang="scss">
	#map{
		width: 100%;
		height: 800px;
	}
</style>

效果图
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenLayers是一个用于显示地图的JavaScript库,可以使用它来实现经纬度定位。以下是一些实现步骤: 1. 在HTML中创建一个地图容器: ``` <div id="map" style="width: 100%; height: 500px;"></div> ``` 2. 在Javascript中创建地图对象: ``` var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: [0, 0], zoom: 2 }) }); ``` 这里创建了一个基本的地图对象,其中`target`是地图容器的ID,`layers`是地图图层,这里使用了OpenStreetMap作为底图,`view`是地图的视图,其中`center`是地图的中心点坐标,这里设置为[0, 0],`zoom`是地图的缩放级别,这里设置为2。 3. 添加一个定位按钮: ``` var locate = document.createElement('button'); locate.innerHTML = '定位'; document.body.appendChild(locate); locate.onclick = function() { navigator.geolocation.getCurrentPosition(function(position) { var pos = ol.proj.fromLonLat([position.coords.longitude, position.coords.latitude]); map.getView().setCenter(pos); map.getView().setZoom(16); }); }; ``` 这里使用了HTML5的`navigator.geolocation`API来获取当前位置,然后将经纬度转换为OpenLayers的坐标系,并设置地图视图的中心点和缩放级别。 完整的代码如下: ``` <!DOCTYPE html> <html> <head> <title>OpenLayers Geolocation Example</title> <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css"> <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script> </head> <body> <div id="map" style="width: 100%; height: 500px;"></div> <button id="locate">定位</button> <script> var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: [0, 0], zoom: 2 }) }); var locate = document.getElementById('locate'); locate.onclick = function() { navigator.geolocation.getCurrentPosition(function(position) { var pos = ol.proj.fromLonLat([position.coords.longitude, position.coords.latitude]); map.getView().setCenter(pos); map.getView().setZoom(16); }); }; </script> </body> </html> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值