申请key

1、注册登陆 高德开放平台,申请 key

高德地图集成 - 图1

添加 Key

高德地图集成 - 图2

高德地图集成 - 图3

高德地图集成 - 图4

注意

image.png

image.png

  1. <script type="text/javascript">
  2. window._AMapSecurityConfig = {
  3. securityJsCode:'您申请的安全密钥',
  4. }
  5. </script>

放在 index.html 文件里面,密钥必须比 key 先加载就行了
image.png

导入 vue-amap

安装 vue-amap

  1. npm install vue-amap --save

安装完成后,main.js 文件中引入

  1. import VueAMap from "vue-amap";
  2. VueAMap.initAMapApiLoader({
  3. key: "KEY",
  4. plugin: [
  5. "AMap.Autocomplete", //输入提示插件
  6. "AMap.PlaceSearch", //POI搜索插件
  7. "AMap.Scale", //右下角缩略图插件 比例尺
  8. "AMap.OverView", //地图鹰眼插件
  9. "AMap.ToolBar", //地图工具条
  10. "AMap.MapType", //类别切换控件,实现默认图层与卫星图、实施交通图层之间切换的控制
  11. "AMap.PolyEditor", //编辑 折线多,边形
  12. "AMap.CircleEditor", //圆形编辑器插件
  13. "AMap.Geolocation" //定位控件,用来获取和展示用户主机所在的经纬度位置
  14. ],
  15. v: '1.4.4',
  16. uiVersion: '1.0'
  17. });
  18. Vue.use(VueAMap);

使用

  1. <template>
  2. <el-row>
  3. <el-col :span="22" :offset="1" >
  4. <div class="amap-page-container">
  5. <el-amap-search-box class="search-box"
  6. :search-option="amap.searchOption"
  7. :on-search-result="onSearchResult"
  8. :center="amap.center"></el-amap-search-box>
  9. <div style="width: 900px;height: 400px;margin-top: -35px; border: #2c3e50 solid 1px;">
  10. <el-amap vid="amapDemo" :zoom="amap.zoom" :center="amap.center" class="amap-demo">
  11. <el-amap-marker :position="amap.marker.position" :events="amap.marker.events"
  12. :visible="amap.marker.visible" :draggable="amap.marker.draggable"></el-amap-marker>
  13. </el-amap>
  14. </div>
  15. </div>
  16. </el-col>
  17. </el-row>
  18. </template>
  19. <!-- vid = 地图容器节点的ID。
  20. zoom = 缩放级别
  21. center = 地图可见区域的中心点
  22. marker = 覆盖物
  23. visible = 显示点标记
  24. draggable = 设置点标记是否可拖拽移动,默认为false。
  25. -->
  26. <style>
  27. .search-box {
  28. position: absolute;
  29. top: 25px;
  30. left: 25px;
  31. }
  32. </style>
  33. <script>
  34. module.exports = {
  35. name: 'amap-page',
  36. data() {
  37. return {
  38. amap: {
  39. zoom: 14,
  40. center: [116.7833687777778, 31.89548288888889],
  41. marker: {
  42. position: [116.7833687777778, 31.89548288888889],
  43. events: {
  44. click: (e) => {
  45. alert('click marker');
  46. let {lng, lat} = e.lnglat;
  47. this.amap.lng = lng;
  48. this.amap.lat = lat;
  49. this.amap.center = [e.lnglat.lng, e.lnglat.lat]
  50. },
  51. dragend: (e) => {
  52. console.log('---event---: dragend')
  53. console.log(e.lnglat.lng, e.lnglat.lat)
  54. this.amap.marker.position = [e.lnglat.lng, e.lnglat.lat];
  55. }
  56. },
  57. visible: true,
  58. draggable: false, // 设置点标记是否可拖拽移动,默认为false。
  59. },
  60. // lng: 0,
  61. // lat: 0,
  62. // address:'',
  63. // q: '',
  64. searchOption: {
  65. city: '全国',
  66. citylimit: true
  67. }
  68. },
  69. form:{
  70. lng:"",
  71. lat:"",
  72. address:""
  73. }
  74. };
  75. },
  76. methods: {
  77. onSearchResult(pois) {
  78. let latSum = 0
  79. let lngSum = 0
  80. pois.forEach(poi => {
  81. lngSum += poi.lng
  82. latSum += poi.lat
  83. })
  84. let center = {
  85. lng: lngSum / pois.length,
  86. lat: latSum / pois.length
  87. }
  88. var address = pois[0].address;
  89. console.log(center.lng, center.lat);
  90. this.amap.center = [center.lng, center.lat]
  91. this.amap.marker.position = [center.lng, center.lat]
  92. this.form.lng = center.lng;
  93. this.form.lat = center.lat;
  94. this.form.address = address;
  95. console.log(this.form.address);
  96. },
  97. }
  98. };
  99. </script>

效果

高德地图集成 - 图8

公交站点搜索

参考: https://blog.csdn.net/reactL/article/details/84779468

  1. <template>
  2. <el-row>
  3. <el-col :span="22" :offset="1" >
  4. <div class="amap-page-container">
  5. <el-amap-search-box class="search-box"
  6. :search-option="amap.searchOption"
  7. :on-search-result="onSearchResult"
  8. :center="amap.center"></el-amap-search-box>
  9. <div style="width: 900px;height: 400px;margin-top: -35px; border: #2c3e50 solid 1px;">
  10. <el-amap vid="amapDemo" :zoom="amap.zoom" :center="amap.center" class="amap-demo">
  11. <el-amap-marker v-for="(marker, index) in amap.markers" :events="marker.events"
  12. :position="marker.point" :icon="marker.icon" :key="index"/>
  13. </el-amap>
  14. </div>
  15. </div>
  16. </el-col>
  17. </el-row>
  18. </template>
  19. <!-- vid = 地图容器节点的ID。
  20. zoom = 缩放级别
  21. center = 地图可见区域的中心点
  22. marker = 覆盖物
  23. visible = 显示点标记
  24. draggable = 设置点标记是否可拖拽移动,默认为false。
  25. -->
  26. <style>
  27. .search-box {
  28. position: absolute;
  29. top: 25px;
  30. left: 25px;
  31. }
  32. </style>
  33. <script>
  34. module.exports = {
  35. name: 'amap-page',
  36. data() {
  37. return {
  38. amap: {
  39. zoom: 14,
  40. center: [116.7833687777778, 31.89548288888889],
  41. searchOption: {
  42. city: '0551',
  43. citylimit: true
  44. },
  45. stationSearch:{},
  46. markers: [
  47. {
  48. point: [116.7833687777778, 31.89548288888889],
  49. events: {
  50. click: (e) => {
  51. // alert('click marker');
  52. let {lng, lat} = e.lnglat;
  53. this.amap.lng = lng;
  54. this.amap.lat = lat;
  55. this.amap.center = [e.lnglat.lng, e.lnglat.lat]
  56. }
  57. },
  58. }
  59. ],
  60. },
  61. form:{
  62. lng:"",
  63. lat:"",
  64. address:""
  65. }
  66. };
  67. },
  68. methods: {
  69. onSearchResult(pois) {
  70. console.log(pois,"pois");
  71. let latSum = 0
  72. let lngSum = 0
  73. pois.forEach(poi => {
  74. lngSum += poi.lng
  75. latSum += poi.lat
  76. })
  77. let center = {
  78. lng: lngSum / pois.length,
  79. lat: latSum / pois.length
  80. }
  81. var address = pois[0].address;
  82. var name = pois[0].name;
  83. // console.log(center.lng, center.lat);
  84. // this.amap.center = [center.lng, center.lat]
  85. // this.amap.marker.position = [center.lng, center.lat]
  86. // this.form.lng = center.lng;
  87. // this.form.lat = center.lat;
  88. // this.form.address = address;
  89. // console.log(this.form.address);
  90. this.getBusStation(name);
  91. },
  92. // 获取站点信息列表,使用父组件传入输入框的值
  93. getBusStation(name) {
  94. console.log('name',name);
  95. this.stationSearch = {
  96. pageIndex: 1, // 页码
  97. pageSize: 30, // 单页显示结果条数
  98. city: '0551' // 确定搜索城市
  99. }
  100. var stationList = new AMap.StationSearch(this.stationSearch)
  101. // 一:
  102. // stationList.search('小', function (status, result) {
  103. // // result即是对应的公交站点数据信息
  104. // if (status === 'complete' && result.info === 'OK') {
  105. // console.log(result)
  106. // console.log(this.stationListData)
  107. // } else {
  108. // console.log('公交路线数据查询失败' + result)
  109. // }
  110. // })
  111. // 二:
  112. stationList.search(name) // mock一个假数据
  113. let _this = this;
  114. new AMap.event.addListener(stationList, 'complete', (res) => {
  115. // res为获取到的当前位置的信息
  116. this.stationListData = res
  117. console.log(this.stationListData,"stationListData")
  118. this.stationListData.stationInfo.forEach(data=>{
  119. console.log("data=>",data)
  120. let id = data.id;
  121. let dlng = data.location.lng;
  122. let dlat = data.location.lat;
  123. _this.amap.markers.push({point: [dlng, dlat],
  124. events: {
  125. click: (e) => {
  126. let {lng, lat} = e.lnglat;
  127. console.log('click marker 坐标:',e.lnglat);
  128. console.log('click marker 公交id:',id);
  129. this.amap.lng = lng;
  130. this.amap.lat = lat;
  131. this.amap.center = [lng, lat]
  132. this.$confirm('是否确认提交站点数据?', "提示", {
  133. confirmButtonText: "确定",
  134. cancelButtonText: "取消",
  135. type: "warning"
  136. }).then(function() {
  137. }).then(() => {
  138. this.msgSuccess("提交成功");
  139. }).catch(function() {});
  140. }
  141. }
  142. });
  143. _this.amap.center = [dlng, dlat]
  144. })
  145. }) // 返回定位信息
  146. new AMap.event.addListener(stationList, 'error', (err) => {
  147. console.log(err)
  148. }) // 返回定位出错信息
  149. },
  150. }
  151. };
  152. </script>

image.png