当前位置:   article > 正文

vue 使用高德地图实现自定义选取起点和终点功能,支持搜索地址跳转定位(保姆级教程)_vue地图选点

vue地图选点

一、效果演示

1. 起点终点选择

 2. 地址搜索

 

二、准备工作

1. 获取高德地图key

1.1  访问高德地图官网注册完成后登录,进入控制台

 1.2  左侧 应用管理-我的应用,点击创建新应用

1.3 点击添加

 

1.4 选择Web端(JS API) 

1.5 创建完成,得到key和安全密钥

 

2. 引入高德地图npm包

npm i @amap/amap-jsapi-loader --save

 三、正式开始写代码

提示:以下代码全部在*.vue文件中编写,无其他文件

1. 设置key和安全密钥,初始化地图

把xxxxxxxxxxxxxxxxxxx换成自己申请的

  1. <script>
  2. import AMapLoader from "@amap/amap-jsapi-loader";
  3. // 设置安全密钥
  4. window._AMapSecurityConfig = {
  5. securityJsCode: 'xxxxxxxxxxxxxxxxx',
  6. }
  7. export default {
  8. mounted() {
  9. this.initMap();
  10. },
  11. data(){
  12. return {
  13. //提交表单
  14. form:{},
  15. //地图实例
  16. map: null,
  17. //路径坐标点集合
  18. coordinateList: [],
  19. //起点坐标
  20. startCoordinate: {},
  21. //终点坐标
  22. endCoordinate: {},
  23. //起点坐标描述
  24. startCoordinateDescription: '经度:请选择起点' + ', 纬度:请选择起点' ,
  25. //终点坐标描述
  26. endCoordinateDescription: '经度:请选择终点' + ', 纬度:请选择终点',
  27. //选择起点
  28. isStart: true,
  29. //起点Marker
  30. startMarker: null,
  31. //终点Marker
  32. endMarker: null,
  33. //搜索点Marker
  34. searchMarker: null,
  35. // 搜索提示
  36. AutoComplete: null,
  37. // 搜索关键字
  38. keywords: "",
  39. // 搜索节流阀
  40. loading: false,
  41. // 搜索提示信息
  42. options: [],
  43. }
  44. },
  45. methods: {
  46. //初始化地图
  47. initMap() {
  48. AMapLoader.reset()
  49. AMapLoader.load({
  50. key: 'xxxxxxxxxxxxxxxxxxxxxxxxx',
  51. version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  52. plugins: ['AMap.AutoComplete', 'AMap.PlaceSearch', 'AMap.Marker'], // 需要使用的的插件列表
  53. AMapUI: {
  54. version: '1.1',
  55. plugins: []
  56. }
  57. }).then((AMap)=>{
  58. // 初始化地图
  59. this.map = new AMap.Map('guide-map',{
  60. viewMode : "2D", // 是否为3D地图模式
  61. zoom : 13, // 初始化地图级别
  62. center : [113.370824,23.131265], //中心点坐标 广州
  63. resizeEnable: true,
  64. willreadoften: true
  65. });
  66. //鼠标点击事件
  67. this.map.on('click', this.clickMapHandler)
  68. // 搜索提示插件
  69. this.AutoComplete = new AMap.AutoComplete({ city: "全国" });
  70. }).catch(e => {
  71. console.log(e);
  72. });
  73. }
  74. }
  75. }
  76. </script>

 2. 选取起点和终点

  1. // 点击地图事件
  2. clickMapHandler(e){
  3. //选择起点
  4. if (this.isStart){
  5. if (this.startMarker !== null){
  6. this.map.remove(this.startMarker)
  7. }
  8. this.startCoordinate.lon = e.lnglat.getLng()
  9. this.startCoordinate.lat = e.lnglat.getLat()
  10. this.startCoordinateDescription = '经度:' + this.startCoordinate.lon + ', 纬度:' + this.startCoordinate.lat
  11. //标点
  12. this.startMarker = new AMap.Marker({
  13. position: new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat()), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
  14. title: '起点',
  15. label: {
  16. content: '起点'
  17. }
  18. })
  19. // 将创建的点标记添加到已有的地图实例
  20. this.map.add(this.startMarker)
  21. }
  22. //选择终点
  23. else {
  24. if (this.endMarker !== null){
  25. this.map.remove(this.endMarker)
  26. }
  27. this.endCoordinate.lon = e.lnglat.getLng()
  28. this.endCoordinate.lat = e.lnglat.getLat()
  29. this.endCoordinateDescription = '经度:' + this.endCoordinate.lon + ', 纬度:' + this.endCoordinate.lat
  30. this.endMarker = new AMap.Marker({
  31. position: new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat()), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
  32. title: '终点',
  33. label: {
  34. content: '终点'
  35. }
  36. })
  37. this.map.add(this.endMarker)
  38. }
  39. }

3.搜索地址功能

  1. // 搜索地址
  2. remoteMethod(query) {
  3. if (query !== "") {
  4. this.loading = true;
  5. setTimeout(() => {
  6. this.loading = false;
  7. this.AutoComplete.search(query, (status, result) => {
  8. this.options = result.tips;
  9. });
  10. }, 200);
  11. } else {
  12. this.options = [];
  13. }
  14. },
  15. // 选中提示
  16. currentSelect(val) {
  17. // 清空时不执行后面代码
  18. if (!val) {
  19. return ;
  20. }
  21. // 自动适应显示想显示的范围区域
  22. this.map.setFitView();
  23. //清除marker
  24. if (this.searchMarker){
  25. this.map.remove(this.searchMarker)
  26. }
  27. //设置marker
  28. this.searchMarker = new AMap.Marker({
  29. map: this.map,
  30. position: [val.location.lng, val.location.lat],
  31. });
  32. this.keywords = val.name
  33. //定位
  34. this.map.setCenter([val.location.lng, val.location.lat])
  35. }

4. 页面代码

  1. <template>
  2. <div class="app-container">
  3. <!-- 表单-->
  4. <div style="width: 70%;margin-left: 15%;">
  5. <el-input v-model="form.name" placeholder="请输入路线名称" style="margin-bottom: 3px;">
  6. <template slot="prepend"><label style="width: 120px;">路线名称</label></template>
  7. </el-input>
  8. <el-input :value="startCoordinateDescription" disabled style="; margin-bottom: 3px;">
  9. <el-button slot="prepend" style="width: 120px; background: #13ce66; color: white" @click="isStart = true">选择起点</el-button>
  10. </el-input>
  11. <el-input :value="endCoordinateDescription" disabled>
  12. <el-button slot="prepend" style="width: 120px; background: #cc3333; color: white" @click="isStart = false">选择终点</el-button>
  13. </el-input>
  14. </div>
  15. <!-- 搜索组件-->
  16. <div>
  17. 搜索:
  18. <el-select
  19. v-model="keywords"
  20. filterable
  21. remote
  22. placeholder="请输入关键词"
  23. :remote-method="remoteMethod"
  24. :loading="loading"
  25. :clearable="true"
  26. size="mini"
  27. @change="currentSelect"
  28. style="width: 500px"
  29. >
  30. <el-option
  31. v-for="item in options"
  32. :key="item.id"
  33. :label="item.name"
  34. :value="item"
  35. class="one-text"
  36. >
  37. <span style="float: left">{{ item.name }}</span>
  38. <span style="float: right; color: #8492a6; font-size: 13px">{{
  39. item.district
  40. }}</span>
  41. </el-option>
  42. </el-select>
  43. </div>
  44. <!-- 地图组件-->
  45. <div id="guide-map" style="height: 500px;"></div>
  46. </div>
  47. </template>

5. 全部代码

  1. <template>
  2. <div class="app-container">
  3. <!-- 表单-->
  4. <div style="width: 70%;margin-left: 15%;">
  5. <el-input v-model="form.name" placeholder="请输入路线名称" style="margin-bottom: 3px;">
  6. <template slot="prepend"><label style="width: 120px;">路线名称</label></template>
  7. </el-input>
  8. <el-input :value="startCoordinateDescription" disabled style="; margin-bottom: 3px;">
  9. <el-button slot="prepend" style="width: 120px; background: #13ce66; color: white" @click="isStart = true">选择起点</el-button>
  10. </el-input>
  11. <el-input :value="endCoordinateDescription" disabled>
  12. <el-button slot="prepend" style="width: 120px; background: #cc3333; color: white" @click="isStart = false">选择终点</el-button>
  13. </el-input>
  14. </div>
  15. <!-- 搜索组件-->
  16. <div>
  17. 搜索:
  18. <el-select
  19. v-model="keywords"
  20. filterable
  21. remote
  22. placeholder="请输入关键词"
  23. :remote-method="remoteMethod"
  24. :loading="loading"
  25. :clearable="true"
  26. size="mini"
  27. @change="currentSelect"
  28. style="width: 500px"
  29. >
  30. <el-option
  31. v-for="item in options"
  32. :key="item.id"
  33. :label="item.name"
  34. :value="item"
  35. class="one-text"
  36. >
  37. <span style="float: left">{{ item.name }}</span>
  38. <span style="float: right; color: #8492a6; font-size: 13px">{{
  39. item.district
  40. }}</span>
  41. </el-option>
  42. </el-select>
  43. </div>
  44. <!-- 地图组件-->
  45. <div id="guide-map" style="height: 500px;"></div>
  46. </div>
  47. </template>
  48. <script>
  49. import AMapLoader from "@amap/amap-jsapi-loader";
  50. // 设置安全密钥
  51. window._AMapSecurityConfig = {
  52. securityJsCode: 'xxxxxxxxxxxxxxxxx',
  53. }
  54. export default {
  55. mounted() {
  56. this.initMap();
  57. },
  58. data(){
  59. return {
  60. //提交表单
  61. form:{},
  62. //地图实例
  63. map: null,
  64. //路径坐标点集合
  65. coordinateList: [],
  66. //起点坐标
  67. startCoordinate: {},
  68. //终点坐标
  69. endCoordinate: {},
  70. //起点坐标描述
  71. startCoordinateDescription: '经度:请选择起点' + ', 纬度:请选择起点' ,
  72. //终点坐标描述
  73. endCoordinateDescription: '经度:请选择终点' + ', 纬度:请选择终点',
  74. //选择起点
  75. isStart: true,
  76. //起点Marker
  77. startMarker: null,
  78. //终点Marker
  79. endMarker: null,
  80. //搜索点Marker
  81. searchMarker: null,
  82. // 搜索提示
  83. AutoComplete: null,
  84. // 搜索关键字
  85. keywords: "",
  86. // 搜索节流阀
  87. loading: false,
  88. // 搜索提示信息
  89. options: [],
  90. }
  91. },
  92. methods: {
  93. //初始化地图
  94. initMap() {
  95. AMapLoader.reset()
  96. AMapLoader.load({
  97. key: 'xxxxxxxxxxxxxxxxxxxxxxxxx',
  98. version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  99. plugins: ['AMap.AutoComplete', 'AMap.PlaceSearch', 'AMap.Marker'], // 需要使用的的插件列表
  100. AMapUI: {
  101. version: '1.1',
  102. plugins: []
  103. }
  104. }).then((AMap)=>{
  105. // 初始化地图
  106. this.map = new AMap.Map('guide-map',{
  107. viewMode : "2D", // 是否为3D地图模式
  108. zoom : 13, // 初始化地图级别
  109. center : [113.370824,23.131265], //中心点坐标 广州
  110. resizeEnable: true,
  111. willreadoften: true
  112. });
  113. //鼠标点击事件
  114. this.map.on('click', this.clickMapHandler)
  115. // 搜索提示插件
  116. this.AutoComplete = new AMap.AutoComplete({ city: "全国" });
  117. }).catch(e => {
  118. console.log(e);
  119. });
  120. },
  121. // 点击地图事件
  122. clickMapHandler(e){
  123. //选择起点
  124. if (this.isStart){
  125. if (this.startMarker !== null){
  126. this.map.remove(this.startMarker)
  127. }
  128. this.startCoordinate.lon = e.lnglat.getLng()
  129. this.startCoordinate.lat = e.lnglat.getLat()
  130. this.startCoordinateDescription = '经度:' + this.startCoordinate.lon + ', 纬度:' + this.startCoordinate.lat
  131. //标点
  132. this.startMarker = new AMap.Marker({
  133. position: new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat()), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
  134. title: '起点',
  135. label: {
  136. content: '起点'
  137. }
  138. })
  139. // 将创建的点标记添加到已有的地图实例
  140. this.map.add(this.startMarker)
  141. }
  142. //选择终点
  143. else {
  144. if (this.endMarker !== null){
  145. this.map.remove(this.endMarker)
  146. }
  147. this.endCoordinate.lon = e.lnglat.getLng()
  148. this.endCoordinate.lat = e.lnglat.getLat()
  149. this.endCoordinateDescription = '经度:' + this.endCoordinate.lon + ', 纬度:' + this.endCoordinate.lat
  150. this.endMarker = new AMap.Marker({
  151. position: new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat()), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
  152. title: '终点',
  153. label: {
  154. content: '终点'
  155. }
  156. })
  157. this.map.add(this.endMarker)
  158. }
  159. },
  160. // 搜索地址
  161. remoteMethod(query) {
  162. if (query !== "") {
  163. this.loading = true;
  164. setTimeout(() => {
  165. this.loading = false;
  166. this.AutoComplete.search(query, (status, result) => {
  167. this.options = result.tips;
  168. });
  169. }, 200);
  170. } else {
  171. this.options = [];
  172. }
  173. },
  174. // 选中提示
  175. currentSelect(val) {
  176. // 清空时不执行后面代码
  177. if (!val) {
  178. return ;
  179. }
  180. // 自动适应显示想显示的范围区域
  181. this.map.setFitView();
  182. //清除marker
  183. if (this.searchMarker){
  184. this.map.remove(this.searchMarker)
  185. }
  186. //设置marker
  187. this.searchMarker = new AMap.Marker({
  188. map: this.map,
  189. position: [val.location.lng, val.location.lat],
  190. });
  191. this.keywords = val.name
  192. //定位
  193. this.map.setCenter([val.location.lng, val.location.lat])
  194. }
  195. }
  196. }
  197. </script>

参考:vue对高德地图的简单使用:点击标记并获取经纬度和详细地址

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/215858
推荐阅读
相关标签
  

闽ICP备14008679号