当前位置:   article > 正文

Vue 使用高德地图,添加点标记 + 点击地图获取坐标 + 带搜索(即地理编码 + 逆地理编码) - 附完整示例_vue3 高德地图搜索定位

vue3 高德地图搜索定位

高德地图

与真实世界联通 - 高德开放平台为开发者赋能,将地图精致地呈现在您的应用中

无论基于哪种平台,都可以通过高德开放平台API和SDK,轻松地完成地图的构建工作

目录

 效果

一、介绍

1、官方文档:地图 | 高德地图API

二、准备工作

1、注册/登录账号

2、点击控制台

3、创建应用

4、获取key和密钥,如上图所示

三、安装依赖包

1、安装命令

 2、这是我的版本 

四、使用步骤

1、在index.html文件中引入密钥

2、在vue文件中引入依赖包

3、申明变量并初始化调用

五、完整示例


 效果

一、介绍

1、官方文档:地图 | 高德地图API

地图 | 高德地图API地图,地图sdk,地图JS API,地图定制,自定义地图,地图覆盖物,地图绘制,路线规划,坐标转换,距离/面积计算,距离测量,室内地图,地图显示,地图个性化,地图开发,室内定位icon-default.png?t=N7T8https://lbs.amap.com/product/map#/

二、准备工作

1、注册/登录账号

2、点击控制台

3、创建应用

4、获取key和密钥,如上图所示

注:使用web服务API,如下图所示

三、安装依赖包

1、安装命令

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

 2、这是我的版本 

"@amap/amap-jsapi-loader": "^1.0.1",

四、使用步骤

1、在index.html文件中引入密钥

代码如下(示例):

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

2、在vue文件中引入依赖包

代码如下(示例):

import AMapLoader from "@amap/amap-jsapi-loader"

3、申明变量并初始化调用

代码如下(示例):

  1. import { shallowRef, defineEmits, ref, onBeforeUnmount } from 'vue';
  2. const map = shallowRef(null);
  3. let AMapObj, placeSearch, marker, geocoder;
  4. function initMap(){
  5. AMapLoader.load({
  6. key:'', //设置您的key
  7. version:"2.0",
  8. plugins:['AMap.ToolBar','AMap.Driving'],
  9. AMapUI:{
  10. version:"1.1",
  11. plugins:[],
  12. },
  13. Loca:{
  14. version:"2.0.0"
  15. },
  16. }).then((AMap)=>{
  17. // console.log(AMap);
  18. AMapObj = AMap;
  19. map.value = new AMap.Map("map-box",{
  20. viewMode:"3D",
  21. zoom:10,
  22. zooms:[2,22],
  23. center: [105.602725,37.076636],
  24. });
  25. map.value.on('click', onMapClick);
  26. AMap.plugin(
  27. ['AMap.ToolBar','AMap.Scale','AMap.Geolocation','AMap.PlaceSearch',
  28. 'AMap.Geocoder','AMap.AutoComplete'],
  29. () => {
  30. // 缩放条
  31. const toolbar = new AMap.ToolBar();
  32. // 比例尺
  33. const scale = new AMap.Scale();
  34. // 定位
  35. const geolocation = new AMap.Geolocation({
  36. enableHighAccuracy: true, //是否使用高精度定位,默认:true
  37. timeout: 10000, //超过10秒后停止定位,默认:5s
  38. position: 'RT', //定位按钮的停靠位置
  39. buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
  40. zoomToAccuracy: true, //定位成功后是否自动调整地图视野到定位点
  41. });
  42. geocoder = new AMap.Geocoder({
  43. city: '全国',
  44. });
  45. map.value.addControl(geolocation);
  46. map.value.addControl(toolbar);
  47. map.value.addControl(scale);
  48. placeSearch = new AMap.PlaceSearch({
  49. // map: map.value,
  50. city: '全国',
  51. pageSize: 10, // 单页显示结果条数
  52. pageIndex: 1, // 页码
  53. citylimit: false, // 是否强制限制在设置的城市内搜索
  54. autoFitView: true,
  55. });
  56. });
  57. }).catch(e=>{
  58. console.log(e);
  59. })
  60. }
  61. initMap();

五、完整示例

Index.html

  1. <!DOCTYPE html>
  2. <html lang="zh_CN" id="htmlRoot">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  6. <meta name="renderer" content="webkit" />
  7. <meta name="viewport"
  8. content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0" />
  9. <title>
  10. <%= title %>
  11. </title>
  12. <script type="text/javascript">
  13. window._AMapSecurityConfig = {
  14. securityJsCode: '', // 你的密钥
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. <div id="app">
  20. </div>
  21. </body>
  22. </html>

Map.vue

  1. <template>
  2. <div class="home">
  3. <div id="map-box"></div>
  4. <div class="info-box">
  5. <a-select
  6. v-model:value="keyword"
  7. show-search
  8. placeholder="输入关键字搜索"
  9. style="width: 300px"
  10. :default-active-first-option="false"
  11. :show-arrow="false"
  12. :filter-option="false"
  13. :not-found-content="null"
  14. @search="handleSearch"
  15. >
  16. <a-select-option v-for="item in data" :key="item.id" :value="item.id" @click="handleSelect(item)">
  17. <div class="d-flex flex-column">
  18. <span>{{item.name}}</span>
  19. <span style="font-size: '10px'; color: #999999">{{item.address}}</span>
  20. </div>
  21. </a-select-option>
  22. </a-select>
  23. <a-tooltip>
  24. <template #title v-if="coord">点击复制</template>
  25. <span style="margin: 5px 8px;">
  26. 经纬度:<span class="copy" style="cursor: pointer;" :data-clipboard-text="coord">{{coord}}</span>
  27. </span>
  28. </a-tooltip>
  29. </div>
  30. </div>
  31. </template>
  32. <script lang="ts" setup>
  33. import { shallowRef, ref, onBeforeUnmount } from 'vue';
  34. import AMapLoader from "@amap/amap-jsapi-loader";
  35. import { message } from 'ant-design-vue';
  36. import ClipboardJS from 'clipboard';
  37. const clipboard = new ClipboardJS('.copy');
  38. clipboard.on('success', function(e) {
  39. console.log(e);
  40. console.info('Text:', e.text);
  41. message.info('复制成功');
  42. e.clearSelection();
  43. });
  44. clipboard.on('error', function(e) {
  45. if(!e.text) message.error('暂无可复制的内容')
  46. });
  47. onBeforeUnmount(() => {
  48. clipboard.destroy();
  49. })
  50. const map = shallowRef(null);
  51. const keyword = ref(null);
  52. const data = ref([]);
  53. const coord = ref('');
  54. let AMapObj, placeSearch, marker, geocoder;
  55. function initMap(){
  56. AMapLoader.load({
  57. key: '', //设置您的key
  58. version: "2.0",
  59. plugins: ['AMap.ToolBar','AMap.Driving'],
  60. AMapUI: {
  61. version: "1.1",
  62. plugins: [],
  63. },
  64. Loca:{
  65. version: "2.0.0"
  66. },
  67. }).then((AMap)=>{
  68. // console.log(AMap);
  69. AMapObj = AMap;
  70. map.value = new AMap.Map("map-box",{
  71. viewMode:"3D",
  72. zoom:10,
  73. zooms:[2,22],
  74. center: [105.602725,37.076636],
  75. });
  76. map.value.on('click', onMapClick);
  77. AMap.plugin(
  78. ['AMap.ToolBar','AMap.Scale','AMap.Geolocation','AMap.PlaceSearch',
  79. 'AMap.Geocoder','AMap.AutoComplete'],
  80. () => {
  81. // 缩放条
  82. const toolbar = new AMap.ToolBar();
  83. // 比例尺
  84. const scale = new AMap.Scale();
  85. // 定位
  86. const geolocation = new AMap.Geolocation({
  87. enableHighAccuracy: true, // 是否使用高精度定位,默认:true
  88. timeout: 10000, // 超过10秒后停止定位,默认:5s
  89. position: 'RT', // 定位按钮的停靠位置
  90. buttonOffset: new AMap.Pixel(10, 20), // 定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
  91. zoomToAccuracy: true, // 定位成功后是否自动调整地图视野到定位点
  92. });
  93. geocoder = new AMap.Geocoder({
  94. city: '全国',
  95. });
  96. map.value.addControl(geolocation);
  97. map.value.addControl(toolbar);
  98. map.value.addControl(scale);
  99. placeSearch = new AMap.PlaceSearch({
  100. city: '全国',
  101. pageSize: 10, // 单页显示结果条数
  102. pageIndex: 1, // 页码
  103. citylimit: false, // 是否强制限制在设置的城市内搜索
  104. autoFitView: true,
  105. });
  106. });
  107. }).catch(e=>{
  108. console.log(e);
  109. })
  110. }
  111. // 搜索地图
  112. function handleSearch(str) {
  113. placeSearch.search(str, (status, result) => {
  114. console.log(result);
  115. if (result && typeof result === 'object' && result.poiList) {
  116. const list = result.poiList.pois;
  117. list.forEach(item => {
  118. item.value = item.name;
  119. item.label = item.name;
  120. });
  121. data.value = list
  122. }
  123. });
  124. }
  125. // 点击地图
  126. function onMapClick(e) {
  127. coord.value = e.lnglat.lng + ',' + e.lnglat.lat
  128. geocoder.getAddress([e.lnglat.lng, e.lnglat.lat], function(status, result) {
  129. if (status === 'complete' && result.info === 'OK') {
  130. // result为对应的地理位置详细信息
  131. keyword.value = result.regeocode.formattedAddress
  132. }
  133. })
  134. drawMarker(e.lnglat)
  135. }
  136. // 点击搜索项
  137. function handleSelect(item) {
  138. console.log(item);
  139. drawMarker(item.location)
  140. if (item.location != null) {
  141. coord.value = item.location.lng + ',' + item.location.lat
  142. }
  143. }
  144. // 绘制地点marker
  145. function drawMarker(location) {
  146. if (location == null) return
  147. let longitude = location.lng, latitude = location.lat
  148. if (marker) {
  149. marker.setMap(null);
  150. }
  151. marker = new AMapObj.Marker({
  152. position: new AMapObj.LngLat(longitude, latitude),
  153. anchor: 'bottom-center',
  154. });
  155. marker.on('click', () => {
  156. coord.value = location;
  157. })
  158. map.value.add(marker);
  159. map.value.setZoomAndCenter(16, [longitude, latitude]);
  160. }
  161. initMap();
  162. </script>
  163. <style lang="less" scoped>
  164. .home{
  165. height: 500px;
  166. width: 100%;
  167. padding: 0px;
  168. margin: 0px;
  169. position: relative;
  170. .info-box {
  171. position: absolute;
  172. top: 8px;
  173. right: 8px;
  174. width: 300px;
  175. background-color: #1f1f1f;
  176. display: flex;
  177. flex-direction: column;
  178. }
  179. }
  180. #map-box{
  181. height: 100%;
  182. width: 100%;
  183. padding: 0px;
  184. margin: 0px;
  185. }
  186. </style>
  187. <style scoped>
  188. :deep() .amap-logo {
  189. display: none !important;
  190. }
  191. :deep() .amap-copyright {
  192. display: none !important;
  193. }
  194. </style>

注:clipboard一键复制的详细使用方法参考地址

https://mp.csdn.net/mp_blog/creation/editor/131308740

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号