• vue接入高德地图获取经纬度


    🤍step1:高德地图开放平台,根据指引注册成为高德开放平台开发者,并申请 web 平台(JS API)的 key 和安全密钥;

    🤍step2:在html引入安全密钥(获取经纬度用,不然会报错)

    按 NPM 方式安装使用 Loader  

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

    新建地图组件 MapContainer.vue 并引入 amap-jsapi-loader 

    import AMapLoader from '@amap/amap-jsapi-loader';

    🤍效果图:MapContainer组件只将经纬度和地址传到父组件

    默认位置和经纬度

    点击地图上某个位置时:

    地图组件代码:(只将经纬度传给父组件,因为form表单还有与地图无关的数据,所以没放到组件中)

    1. <script>
    2. /**JS API Loader
    3. * 创建者:XRr
    4. * 创建时间:2023-10-10
    5. * 组件功能:获取地图上某个地点的经纬度
    6. * */
    7. import AMapLoader from "@amap/amap-jsapi-loader";
    8. export default {
    9. name: "MapDialog",
    10. props: ["defaultPoint", "defaultAddress"],
    11. data() {
    12. return {
    13. visible: false, //弹窗显隐
    14. center: [115.97539, 28.715532], //地图中心点
    15. point: [], //经纬度-lng lat
    16. map: null, //地图实例
    17. marker: null, //地图icon
    18. geocoder: null, //逆解析实例
    19. address: null, //地址
    20. };
    21. },
    22. mounted() {
    23. this.initMap();
    24. },
    25. methods: {
    26. //DOM初始化完成进行地图初始化
    27. initMap() {
    28. // 默认的经纬度和地址
    29. if (this.defaultPoint && this.defaultAddress) {
    30. this.address = this.defaultAddress;
    31. this.point = this.defaultPoint;
    32. this.center = this.point;
    33. }
    34. AMapLoader.load({
    35. key: "xxx", // 申请好的Web端开发者Key,首次调用 load 时必填
    36. version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
    37. plugins: [
    38. "AMap.Geocoder", // 逆向地理解码插件
    39. "AMap.Marker", // 点标记插件
    40. "AMap.ToolBar",//比例尺,显示当前地图中心的比例尺
    41. "AMap.Scale",//比例尺,显示当前地图中心的比例尺
    42. "AMap.ElasticMarker",//灵活点标记,可以随着地图级别改变样式和大小的 Marker
    43. "AMap.Geolocation",//定位,提供了获取用户当前准确位置、所在城市的方法
    44. ], // 需要使用的的插件列表
    45. })
    46. .then((AMap) => {
    47. this.map = new AMap.Map("container", {
    48. //设置地图容器id
    49. zoom: 12, //初始化地图级别
    50. center: this.center, //初始化地图中心点位置
    51. });
    52. // 如果父组件传入了有效值 回显一个icon
    53. if (this.point.length > 0) {
    54. this.addMarker();
    55. }
    56. //监听用户的点击事件
    57. this.map.on("click", (e) => {
    58. let lng = e.lnglat.lng;
    59. let lat = e.lnglat.lat;
    60. this.point = [lng, lat];
    61. // 增加点标记
    62. this.addMarker();
    63. // 获取地址
    64. this.getAddress();
    65. });
    66. })
    67. .catch((e) => {
    68. console.log(e);
    69. });
    70. },
    71. // 增加点标记
    72. addMarker() {
    73. // 清除其他icon
    74. if (this.marker) {
    75. this.marker.setMap(null);
    76. this.marker = null;
    77. }
    78. // 重新渲染icon
    79. this.marker = new AMap.Marker({
    80. icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
    81. position: this.point, //icon经纬度
    82. offset: new AMap.Pixel(-13, -30), //icon中心点的偏移量
    83. });
    84. this.marker.setMap(this.map); //设置icon
    85. },
    86. // 将经纬度转换为地址
    87. getAddress() {
    88. const self = this;
    89. // 这里通过高德 SDK 完成。
    90. this.geocoder = new AMap.Geocoder({
    91. city: "全国", //地理编码时,设置地址描述所在城市; 默认全国; 可选值:城市名(中文或中文全拼)、citycode、adcode
    92. radius: 100, //逆地理编码时,以给定坐标为中心点; 默认1000; 取值范围(0-3000)
    93. extensions: "all", //逆地理编码时,返回信息的详略; 默认值:base,返回基本地址信息; 默认值:base,返回基本地址信息
    94. });
    95. //调用逆解析方法 个人开发者调用量上限5000(次/日)
    96. this.geocoder.getAddress(this.point, function (status, result) {
    97. if (status === "complete" && result.info === "OK") {
    98. if (result && result.regeocode) {
    99. // this指向改变
    100. self.address = result.regeocode.formattedAddress;
    101. self.$emit('sendLoction',self.point,self.address)
    102. }
    103. }
    104. });
    105. }
    106. },
    107. };
    108. script>
    109. <style scoped>
    110. #container {
    111. padding: 0px;
    112. margin: 0px;
    113. width: 100%;
    114. height: 400px;
    115. }
    116. style>

     父组件:弹出框代码

    记录一下,虽然简单,但是每次用到都要重新梳理流程,过程很烦躁。

  • 相关阅读:
    一个反向代理神器 ——Nginx Proxy Manager
    SecureCRT® 字选择和字定界符
    drf从无到有学习Django Rest Framework框架——什么是DRF
    图片怎么改成jpg格式
    家用厨房电器测试报告办理流程
    这可是全网网工基础知识最详细的整理,没有之一
    建筑工地无线视频监控 工业级无线路由器应用
    Compositional Minimax Optimization学习之路
    生成式AI - 大模型(LLM)提示工程(Prompt)技巧
    使用eslint + prettier + husky + lint-staged 约束项目的最佳实践!
  • 原文地址:https://blog.csdn.net/Stitch_xiao/article/details/133736589