1 Star 4 Fork 3

小陈同学 / vue-tianditu

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

vue-tianditu

安装

npm i vue-tianditu
# or
yarn add vue-tianditu

快速上手

全局引入

全部引入,解放双手

// main.ts
import { createApp } from "vue";
import App from "./App.vue";
import VueTianditu from "vue-tianditu";

const app = createApp(App);
app.use(VueTianditu, {
  v: "4.0", //目前只支持4.0版本
  tk: "your map token"
});
app.mount("#app");
<!-- App.vue -->
<template>
  <div class="mapDiv">
    <tdt-map :center="state.center" :zoom="state.zoom"></tdt-map>
  </div>
</template>

<script lang="ts" setup>
  import { reactive } from "vue";
  import { TdtMap } from "vue-tianditu";
  const state = reactive({
    center: [113.280637, 23.125178],
    zoom: 12
  });
</script>

<style>
  .mapDiv {
    width: 100%;
    height: 100%;
  }
</style>

按需引入

按需引入,配合 ts 获得类型提示

App.vue

<template>
  <div class="mapDiv">
    <tdt-map :center="state.center" :zoom="state.zoom" :loadConfig="loadScript"></tdt-map>
  </div>
</template>

<script lang="ts" setup>
  import { reactive } from "vue";
  import { TdtMap } from "vue-tianditu";
  const loadConfig = { v: "4.0", tk: "your map token" };
  const state = reactive({
    center: [113.280637, 23.125178],
    zoom: 12
  });
</script>

<style>
  .mapDiv {
    width: 100%;
    height: 100%;
  }
</style>

API 加载器

甚至可以把它当作无情的 API 加载工具

import { useApiLoader } from "vue-tianditu";

useApiLoader({
  v: "4.0",
  tk: "your map token",
  plugins: ["D3", "CarTrack", "HeatmapOverlay", "BufferTool", "ImageOverLayer"]
}).then(() => {
  new T.Map({...});
});

辅助函数

import { toLngLat, toBounds, toPoint, toIcon } from "vue-tianditu";

说明

函数名 返回值 描述
toLngLat(lnglat:[number,number]) T.LngLat 转换为经纬度对象。
参数说明:
lnglat:经纬度数组
toBounds(bounds:[[number,number],[number,number]]) T.Bounds 转换为地理范围对象。
参数说明:
bounds:地理范围数组
toPoint(point:[number,number]) T.Point 转换为像素坐标点对象。
参数说明:
point:像素坐标点数组
toIcon(icon:IconOption|string) T.Icon 转换为图标对象。
参数说明:
icon:string//图片地址{iconUrl:string,//图片地址
iconSize:[number,number],//图片大小
iconAnchor:[number,number]//偏移}

调用原生API

由于API是通过useApiLoader异步加载的,所以需要在API加载完成后才能使用天地图原生的API,有以下三种方式:

  • 不使用组件,只使用API加载器
<template>
  <div id="mapContainer"></div>
</template>

<script>
import { useApiLoader } from "vue-tianditu";
// 加载API
useApiLoader({
  v: "4.0",
  tk: "your map token",
  plugins: ["D3", "CarTrack", "HeatmapOverlay", "BufferTool", "ImageOverLayer"]
}).then(() => {
  const map = new T.Map("mapContainer", {...});
  const marker = new T.Marker({...});
  map.addOverlay(marker);
});
</script>
  • 使用组件,监听组件的初始化事件
<template>
  <tdt-map @init="mapInit"></tdt-map>
</template>

<script>
function mapInit(map){
  // 此时原生API中的T已存在window中
  const marker = new T.Marker({...})
  map.addOverlay(marker);
}
</script>
  • 注册了组件,使用API加载器异步等待API加载完成
// 不用传参数,异步等待之前注册组件时带参数加载的API加载完成
useApiLoader({}).then(() => {
  const marker = new T.Marker({...});
});

如果项目中用到了eslint,则需要在eslintrc配置文件中加入

{
  ...
  globals: {
    ...
    T: "readonly"
  }
}
MIT License Copyright (c) 2021 SoulLyoko Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

天地图 展开 收起
NodeJS 等 4 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
NodeJS
1
https://gitee.com/chenzx78/vue-tianditu.git
git@gitee.com:chenzx78/vue-tianditu.git
chenzx78
vue-tianditu
vue-tianditu
master

搜索帮助