map.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <!-- 百度地图 -->
  3. <div id="bai-du-map">
  4. <!-- 技术支持和联系方式 -->
  5. </div>
  6. </template>
  7. <script>
  8. import AMapLoader from '@amap/amap-jsapi-loader'
  9. window._AMapSecurityConfig = {
  10. // 设置安全密钥
  11. securityJsCode: 'b0a69d5602eae8990937ceb4699d2e7d'
  12. }
  13. export default {
  14. data() {
  15. return {
  16. map: null,
  17. mouseTool: null,
  18. overlays: [],
  19. auto: null,
  20. placeSearch: null
  21. }
  22. },
  23. methods: {
  24. initMap() {
  25. console.log(12312)
  26. AMapLoader.reset()
  27. AMapLoader.load({
  28. 'key': 'b0a69d5602eae8990937ceb4699d2e7d', // 申请好的Web端开发者Key,首次调用 load 时必填
  29. 'version': '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  30. 'plugins': ['AMap.Scale', 'AMap.ToolBar', 'AMap.AutoComplete', 'AMap.PlaceSearch', 'AMap.ControlBar', 'AMap.MouseTool'] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  31. // "plugins": [], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  32. }).then((AMap) => {
  33. this.map = new AMap.Map('bai-du-map', {
  34. viewMode: '2D', // 是否为3D地图模式
  35. zoom: 13, // 初始化地图级别
  36. center: [120.366855, 31.492167], //中心点坐标 郑州
  37. resizeEnable: true
  38. })
  39. this.mouseTool = new AMap.MouseTool(this.map)
  40. let marker = new AMap.Marker({
  41. show: false,
  42. position: [120.366855, 31.492167],//要展示marker的经度、纬度。数据格式就是数组里放入经纬度数据
  43. // icon: require("../../../public/img/bg/site.png"), //显示的图标
  44. offset: [-16, -32]//图标偏移量,展示时会默认以图标的左上角为原点,不设置偏移量会导致地图放大缩小时造成图标偏移的情况,偏移量设置为图标宽度的负一半,负整个高度。
  45. })
  46. this.map.add(marker) //添加marker
  47. var info = []
  48. // info.push('<div style="padding:7px 0px 0px 0px;"><h4>无锡xxx公司</h4>')
  49. // info.push('<p class=\'input-item\'>电话 : 010-84107000 邮编 : 100102</p>')
  50. // info.push('<p class=\'input-item\'>地址 :江苏省无锡市新吴区演示街道测试地址xx大楼</p></div></div>')
  51. let infoWindow = new AMap.InfoWindow({
  52. content: info.join('') //使用默认信息窗体框样式,显示信息内容
  53. })
  54. infoWindow.open(this.map, this.map.getCenter())
  55. this.mouseTool.on('draw', function(e) {
  56. this.overlays.push(e.obj)
  57. }.bind(this))
  58. }).catch(e => {
  59. console.log(e)
  60. })
  61. //1.创建Marker对象
  62. },
  63. draw(type) {
  64. switch (type) {
  65. case 'marker': {
  66. this.mouseTool.marker({
  67. //同Marker的Option设置
  68. })
  69. break
  70. }
  71. case 'polyline': {
  72. this.mouseTool.polyline({
  73. strokeColor: '#80d8ff'
  74. //同Polyline的Option设置
  75. })
  76. break
  77. }
  78. case 'polygon': {
  79. this.mouseTool.polygon({
  80. fillColor: '#00b0ff',
  81. strokeColor: '#80d8ff'
  82. //同Polygon的Option设置
  83. })
  84. break
  85. }
  86. case 'rectangle': {
  87. this.mouseTool.rectangle({
  88. fillColor: '#00b0ff',
  89. strokeColor: '#80d8ff'
  90. //同Polygon的Option设置
  91. })
  92. break
  93. }
  94. case 'circle': {
  95. this.mouseTool.circle({
  96. fillColor: '#00b0ff',
  97. strokeColor: '#80d8ff'
  98. //同Circle的Option设置
  99. })
  100. break
  101. }
  102. }
  103. }
  104. },
  105. mounted() {
  106. this.initMap()
  107. }
  108. }
  109. </script>
  110. <style lang="scss">
  111. #bai-du-map {
  112. height: 100vh;
  113. width: 100vw;
  114. .bottom-center {
  115. display: none !important;
  116. }
  117. }
  118. </style>