经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue如何使用原生高德地图你知道吗
来源:jb51  时间:2022/2/28 9:23:23  对本文有异议

1、先在vue项目根目录下新建vue.config.js,这个文件是没有的,vue不提供

  1. module.exports = {
  2. configureWebpack: {
  3. externals: {
  4. 'AMap': 'AMap', // 高德地图配置
  5. 'AMapUI': 'AMapUI'
  6. }
  7. },
  8. }

2、在vue文件index.html中引入高德地图js文件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width,initial-scale=1.0">
  7. <link rel="icon" href="<%= BASE_URL %>favicon.ico">
  8. <title>default</title>
  9. </head>
  10. <body>
  11. <noscript>
  12. <strong>We're sorry but default doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
  13. </noscript>
  14. <div id="app"></div>
  15. <!-- built files will be auto injected -->
  16. <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.3&key=你的高德地图key&plugin=AMap.ControlBar"></script>
  17. </body>
  18. </html>

3、在vue文件中使用

  1. <template>
  2. <div class="box">
  3. <div id="container" style="width:1500px; height:900px"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import AMap from 'AMap' // 引入高德地图
  8. let map,marker;
  9. export default {
  10. data() {
  11. return {
  12. markers : [
  13. {
  14. icon: 1,
  15. position: [121.506377, 31.243105],
  16. name:'张三',
  17. }, {
  18. icon: 1,
  19. position: [121.506077, 31.242105],
  20. name:'李四',
  21. }, {
  22. icon: 1,
  23. position: [121.506577, 31.240105],
  24. name:'王五',
  25. }
  26. ]
  27. }
  28. },
  29. mounted () {
  30. this.init()
  31. },
  32. methods: {
  33. init () {
  34. let that = this;
  35. map = new AMap.Map('container', {
  36. resizeEnable: true,
  37. rotateEnable:true,
  38. pitchEnable:true,
  39. zoom: 17,
  40. pitch:50,
  41. rotation:-15,
  42. viewMode:'3D',//开启3D视图,默认为关闭
  43. buildingAnimation:true,//楼块出现是否带动画
  44. // expandZoomRange:true,
  45. zooms:[3,20],
  46. center:that.markers[0].position
  47. })
  48. AMap.plugin(['AMap.ControlBar',], function(){
  49. // 添加 3D 罗盘控制
  50. map.addControl(new AMap.ControlBar());
  51. });
  52. this.addMarker(this.markers)
  53. },
  54. //拖动事件
  55. mapDraw(arriveCoor){
  56. map = new AMap.Map('container', { //map-location是嵌地图div的id
  57. resizeEnable: true, //是否监控地图容器尺寸变化
  58. zoom:20, //初始化地图层级
  59. center: arriveCoor //初始化地图中心点
  60. });
  61. // 定位点
  62. this.addMarker(arriveCoor);
  63. },
  64. // 实例化点标记
  65. addMarker(arriveCoor) {
  66. var _this = this;
  67. arriveCoor.forEach(item=>{
  68. marker = new AMap.Marker({
  69. icon: item.icon, //图片ip
  70. imageSize: "20px",
  71. position: [item.position[0], item.position[1]],
  72. // offset: new AMap.Pixel(-13, -30),
  73. content:`<div class="custom-content-marker"><span style="display:block;width:200px">${item.name}</span><img src=${mapicon} onclick="clickImgMarker(${item.name})"></div>`,
  74. // 设置是否可以拖拽
  75. draggable: true,
  76. cursor: 'move',
  77. // 设置拖拽效果
  78. // raiseOnDrag: true
  79. });
  80. marker.setMap(map);
  81. })
  82. },
  83. },
  84. }
  85. </script>

5、卫星图动漫切换镜头,动画效果

  1. <template>
  2. <div class="box">
  3. <div style="position: absolute;z-index: 10;cursor:pointer" @click="animates()">点击去鼎旺中心</div>
  4. <div id="container" style="width: 2400px;height: 920px;"></div>
  5. </div>
  6. </template>
  7. <script>
  8. import AMap from 'AMap' // 引入高德地图
  9. var map;
  10. export default {
  11. data() {
  12. return {
  13. }
  14. },
  15. mounted () {
  16. this.init()
  17. },
  18. methods: {
  19. init () {
  20. var _this = this;
  21. map = new AMap.Map('container', {
  22. resizeEnable: true,
  23. rotateEnable:true,
  24. pitchEnable:true,
  25. zoom: 13,
  26. pitch: 65,
  27. rotation: 45,
  28. viewMode:'3D',//开启3D视图,默认为关闭
  29. buildingAnimation:true,//楼块出现是否带动画
  30. expandZoomRange:true,
  31. center:[113.2385,22.96605],
  32. layers: [
  33. // 高德默认标准图层
  34. new AMap.TileLayer.Satellite(),
  35. // 楼块图层
  36. new AMap.Buildings({
  37. zooms: [16, 18],
  38. zIndex: 10,
  39. heightFactor: 2 //2倍于默认高度,3D下有效
  40. }),
  41. ],
  42. })
  43. //定位鼎旺中心
  44. var maskerIn = new AMap.Marker({
  45. position:[113.2385,22.96605],
  46. map:map
  47. });
  48. var loca = window.loca = new Loca.Container({
  49. map,
  50. });
  51. var pl = window.pl = new Loca.PolygonLayer({
  52. zIndex: 120,
  53. shininess: 10,
  54. hasSide: true,
  55. cullface: 'back',
  56. depth: true,
  57. });
  58. pl.setLoca(loca);
  59. map.on('complete', function () {
  60. loca.animate.start();
  61. // setTimeout(_this.animates, 2000);//调用镜头动画
  62. });
  63. },
  64. //点击调用精通动漫
  65. animates(){
  66. var speed = 1;
  67. loca.viewControl.addAnimates([
  68. // 寻迹
  69. {
  70. center: {
  71. value: [113.2385,22.96605],
  72. control: [[113.2385,22.96605], [113.2385,22.96605]],
  73. timing: [0.3, 0, 0.1, 1],
  74. duration: 8000 / speed,
  75. },
  76. //快速移动,前进
  77. zoom: {
  78. value: 17,
  79. control: [[0.3, 15], [1, 17]],
  80. timing: [0.3, 0, 0.7, 1],
  81. duration: 4000 / speed,
  82. },
  83. }, {
  84. // 环绕
  85. rotation: {
  86. value: 270,
  87. control: [[0, 0], [1, 270]],
  88. timing: [0, 0, 0, 1],
  89. duration: 7000 / speed,
  90. },
  91. //前进
  92. zoom: {
  93. value: 17,
  94. control: [[0.3, 16], [1, 17]],
  95. timing: [0.3, 0, 0.7, 1],
  96. duration: 5000 / speed,
  97. },
  98. }], function () {
  99. pl.hide(1000);
  100. setTimeout(animate, 2000);
  101. console.log('结束');
  102. });
  103. },
  104. },
  105. }
  106. </script>
  107. <style>
  108. .amap-e, .amap-maps {
  109. width: 100%;
  110. height: 100%;
  111. outline: none;
  112. background: #050b17;
  113. }
  114. .amap-copyright {
  115. display: none!important;
  116. left: 77px;
  117. height: 16px;
  118. bottom: 0;
  119. padding-bottom: 2px;
  120. font-size: 11px;
  121. font-family: Arial,sans-serif;
  122. }
  123. .amap-copyright, .amap-logo {
  124. display: none!important;
  125. }
  126. </style>

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注w3xue的更多内容!   

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号