leaflet电子海图加载openseamap,使用vue

本文介绍如何在前端使用Vue和Leaflet显示OpenSeaMap电子海图,包括两种加载方法:一种通过法国人的GitHub项目,另一种利用JavaScript实现图层叠加。同时提到了底图选择OpenStreetMap,并提供了遇到问题时的联系方式。
该文章已生成可运行项目,

前端使用vue+leafet显示openseamap

openseamap是海图信息图片背景为灰色,而且是开源的,所以需要有一个底图,我选择的底图为openstreetmap,也是一个开源地图

要在leaflet中显示正常的电子海图,需要同事加载这两个图片,openstreetmap在下面,openseamap在上面

有两种方法加载电子海图:

1、通过法国人发布的github方法  (https://github.com/snosan-tools/avurnavs.snosan.fr)

以下为vue的代码,唯一需要的是安装vue2-leaflet的插件和leaflet插件,两个是不同插件,所以要多安装vue2-leaflet的插件,他是通过html方法去实现的

<template>
  <div class="map">
    <l-map :zoom="zoom" :center="center">
      <l-tile-layer :url="url"></l-tile-layer>
      <l-tile-layer :url="xxx"></l-tile-layer>
    </l-map>
  </div>
</template>

<script>
import { LMap, LTileLayer} from 'vue2-leaflet';

export default {
  name: 'example',
  components: {
    LMap,
    LTileLayer,
  },
  data () {
    return {
      zoom: 6,
      center:[48, -1.219482],
      url: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
      seaUrl: 'https://tiles.openseamap.org/seamark/{z}/{x}/{y}.png',
      xxx:'http:///osm.franken.de/cgi-bin/mapserv.fcgi?',

    }
  },
  methods: {
   
  },
}
</script>
<style>
</style>

2、我自己的方法主要是通过JavaScript的方法去实现的

重点在第55行的sea.addTo(this.map);,使用叠加的方法使得两个图层的叠加

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

<script>
import Leaflet from "leaflet";
export default {
  data() {
    return {
      map: null,
      reset_btn: null,
      map_config: {
        center: [39.50404070558415, -111.533203125],
        zoom: 6,
        minZoom: 2,
        maxZoom: 18,
      },
    };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    initMap() {
      var seaUrl = "http://tiles.openseamap.org/seamark/{z}/{x}/{y}.png";
      var osmUrl = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
      var sea = L.tileLayer(seaUrl, { subdomains: ["a", "b", "c"] });
      var OpenStreetMap = L.tileLayer(osmUrl, { subdomains: ["a", "b", "c"] });

      var baseLayers = {
        OpenStreetMap: OpenStreetMap,
      };
      
      this.map = L.map("leaflet-map", {
        zoomControl: false,
        center: this.map_config.center,
        zoom: this.map_config.zoom,
        minZoom: this.map_config.minZoom,
        maxZoom: this.map_config.maxZoom,
        layers: [OpenStreetMap],
      });

      L.control.zoom({position: "topright",}).addTo(this.map);

      this.map.on("zoomend", (event) => {
        if (this.reset_btn && this.map.getZoom() !== this.map_config.zoom) {
          this.reset_btn.enable();
        }
      });

      L.control.layers(baseLayers, null, { position: "bottomright" }).addTo(this.map);

     sea.addTo(this.map);
    },
  },
};
</script>

<style >
#leaflet-map {
  position: absolute;
  width: 100%;
  height: 100%;
}
</style>

如果有不懂的或者调试出问题的可以联系我的邮箱:(769516427@qq.com)(希望可以多点人一起研究)

本文章已经生成可运行项目
Vue 框架中集成 Leaflet加载天地图(TianMap)服务,可以通过 `Proj4Leaflet` 插件实现对非 EPSG:3857 坐标系的支持,因为天地图通常使用的是 CGCS2000 或其他国内常用坐标系。以下是一个完整的实现示例。 ### 1. 安装依赖 首先确保安装了必要的库: ```bash npm install leaflet proj4js proj4leaflet ``` ### 2. 创建 Vue 组件 创建一个名为 `TianMapLeaflet.vue` 的组件,并引入相关资源: ```vue <template> <div id="map" ref="map"></div> </template> <script> import L from 'leaflet'; import 'leaflet/dist/leaflet.css'; import proj4 from 'proj4'; import { CRS } from 'proj4leaflet'; export default { mounted() { // 定义 TianMap 使用的坐标系(例如 CGCS2000) const crs = CRS('EPSG:4526', { resolutions: [1.40625, 0.703125, 0.3515625, 0.17578125, 0.087890625, 0.0439453125], origin: [73.628, 53.567], bounds: L.bounds([73.628, 18.164], [134.773, 53.567]), }); // 初始化地图 const map = L.map(this.$refs.map, { crs: crs, center: [39.9042, 116.4074], // 北京坐标 zoom: 5, }); // 添加天地图瓦片图层 L.tileLayer('http://webst0{s}.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}', { subdomains: ['1','2','3','4'], maxZoom: 18, attribution: '高德地图' }).addTo(map); }, }; </script> <style scoped> #map { width: 100%; height: 100vh; } </style> ``` ### 3. 解释关键部分 - **坐标系统定义**:通过 `proj4leaflet` 和 `proj4js` 定义了一个自定义的坐标参考系统(CRS),用于适配天地图所使用的坐标体系[^1]。 - **地图初始化**:使用自定义的 CRS 来初始化 Leaflet 地图实例。 - **天地图图层**:通过 `L.tileLayer` 加载天地图服务,这里以高德地图风格为例,实际 URL 应根据具体服务提供商进行调整。 --- ### 注意事项 - 天地图服务可能需要申请授权或提供 Token,具体请查阅其官方文档。 - 如果使用的是标准 Web Mercator 投影(EPSG:3857),则不需要额外配置 Proj4Leaflet。 - 在生产环境中建议对瓦片地址进行封装,以便支持跨域和缓存策略。 ---
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值