1,先去天地图官方网站注册并创建key
2,在项目的public里的index.html中把刚才注册好的key给引进去
<!DOCTYPE html> <html lang=""> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="icon" href="<%= BASE_URL %>favicon.ico"> <title><%= htmlWebpackPlugin.options.title %></title> <script src="http://api.tianditu.gov.cn/api?v=4.0&tk=刚才注册的key" type="text/javascript"></script> </head> <body> <noscript> <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> </noscript> <div id="app"></div> <!-- built files will be auto injected --> </body> </html>
3,在vue文件里加入以下代码
<div id="mapDiv"></div>//切记给他设置宽高 <script> import { onMounted } from "vue"; export default { setup() { const init = () => { var map = null; var T = window.T; map = new T.Map("mapDiv"); map.setMapType(window.TMAP_SATELLITE_MAP); map.centerAndZoom(new T.LngLat(112, 36), 7); }; onMounted(() => { init(); }); return { }; }, }; </script>