• vue(移动端)使用高德地图实现精准定位


    目录

    效果图

     前提准备

    代码展示


    效果图

    两个页面

    页面一(粗略定位)

    点击城市进入页面二 (粗略定位),搜索框输入具体位置(以大连理工大学为例)

     点击大连理工大学,回到页面一,并展示精准位置

    再次点击位置,进入页面二精准定位地图

     前提准备

    一、vue项目

    二、移动端配置(不配置也可用)

    vue移动端适配

    三、高德地图创建应用获取key及使用前配置

    见如下文章,一、创建应用获取key,二、配置

    vue移动端高德地图的使用及实现最简单的地图功能

    代码展示

    页面一(路由:path:"/home")

    1. <template>
    2. <div>
    3. <router-link to="/map">{{ cityText }}router-link>
    4. div>
    5. template>
    6. <script>
    7. export default {
    8. data(){
    9. return{
    10. cityText:""
    11. }
    12. },
    13. mounted() {
    14. var that=this;
    15. if(that.$store.state.cityinfo.name){
    16. that.cityText=that.$store.state.cityinfo.name
    17. return
    18. }
    19. // eslint-disable-next-line no-undef
    20. AMap.plugin('AMap.CitySearch', function () {
    21. // eslint-disable-next-line no-undef
    22. var citySearch = new AMap.CitySearch()
    23. citySearch.getLocalCity(function (status, result) {
    24. if (status === 'complete' && result.info === 'OK') {
    25. // 查询成功,result即为当前所在城市信息
    26. that.cityText=result.city;
    27. console.log(result)
    28. }
    29. })
    30. })
    31. }
    32. }
    33. script>
    34. <style scoped>
    35. style>

    页面二(路由:path:"/map")

    1. <template>
    2. <div class="body">
    3. <input type="text" v-model="iptVal">
    4. <ul>
    5. <li v-for="item in searchList" :key="item.id" @click="goHome(item)">{{item.name}}li>
    6. ul>
    7. <div id="box">div>
    8. div>
    9. template>
    10. <script>
    11. import {
    12. mapMutations,
    13. // mapState
    14. } from "vuex"
    15. export default {
    16. data(){
    17. return {
    18. iptVal:"",
    19. cityText:"",
    20. searchList:[]
    21. };
    22. },
    23. watch:{
    24. //----------------------监听搜索框变化,获取有关数据-----------------
    25. iptVal(){
    26. var that=this;
    27. // eslint-disable-next-line no-undef
    28. AMap.plugin('AMap.AutoComplete', function(){
    29. var autoOptions = {
    30. //city 限定城市,默认全国
    31. city: that.cityText
    32. };
    33. // 实例化AutoComplete
    34. // eslint-disable-next-line no-undef
    35. var autoComplete= new AMap.AutoComplete(autoOptions);
    36. // 根据关键字进行搜索
    37. // eslint-disable-next-line no-undef
    38. autoComplete.search(that.iptVal, function(status, result) {
    39. // 搜索成功时,result即是对应的匹配数据
    40. console.log(result);
    41. that.searchList=result.tips;
    42. })
    43. })
    44. }
    45. },
    46. mounted() {
    47. var that=this;
    48. //--------------------定位-------------------------
    49. // eslint-disable-next-line no-undef
    50. AMap.plugin('AMap.CitySearch', function () {
    51. // eslint-disable-next-line no-undef
    52. var citySearch = new AMap.CitySearch()
    53. citySearch.getLocalCity(function (status, result) {
    54. if (status === 'complete' && result.info === 'OK') {
    55. // 查询成功,result即为当前所在城市信息
    56. that.cityText=result.city;
    57. console.log(result)
    58. }
    59. })
    60. })
    61. //如果cityinfo里存在数据,地图显示cityinfo里的地点(上一次搜索点击的地点),否则展示定位市区(pc端定位不准)
    62. if(that.$store.state.cityinfo.name){
    63. var cityInfo=this.$store.state.cityinfo;
    64. // eslint-disable-next-line no-undef
    65. var map = new AMap.Map('box', {
    66. zoom:18,//级别
    67. center: [cityInfo.location.lng, cityInfo.location.lat],//中心点坐标
    68. });
    69. // eslint-disable-next-line no-undef
    70. var marker = new AMap.Marker({
    71. position:[cityInfo.location.lng, cityInfo.location.lat]//选择标记位置
    72. })
    73. map.add(marker);//将标记添加到地图
    74. return
    75. }else{
    76. // eslint-disable-next-line no-redeclare,no-unused-vars,no-undef
    77. var map = new AMap.Map('box', {
    78. zoom:8,//级别
    79. });
    80. }
    81. },
    82. methods:{
    83. //点击搜索的地点,将该地点存入cityInfo,并回到首页
    84. goHome(val){
    85. this.addCityINfo(val);
    86. this.$router.push("/home")
    87. },
    88. ...mapMutations({
    89. addCityINfo:"uptCityInfo"
    90. })
    91. }
    92. }
    93. script>
    94. <style scoped>
    95. #box{
    96. width: 100%;
    97. height: 800px;
    98. }
    99. .body input{
    100. margin:50px;
    101. border: 1px solid #ccc;
    102. border-radius: 10px;
    103. height: 80px;
    104. }
    105. .body ul li{
    106. text-align: left;
    107. font-size: 20px;
    108. padding: 10px;
    109. border-bottom: 1px solid #cccccc;
    110. margin: 10px;
    111. }
    112. style>

    vuex(实现跨页面传递数据)

    src/store/index.js

    1. import Vue from 'vue'
    2. import Vuex from 'vuex'
    3. Vue.use(Vuex)
    4. export default new Vuex.Store({
    5. state: {
    6. cityinfo:{}
    7. },
    8. getters: {
    9. },
    10. mutations: {
    11. uptCityInfo(state,val){
    12. state.cityinfo=val;
    13. }
    14. },
    15. actions: {
    16. },
    17. modules: {
    18. }
    19. })

    使用别的方法跨页面传递数据,需要对页面一二获取与存入数据进行更改

    路由根据自己实际情况更改

  • 相关阅读:
    【Python基础入门技能树笔记】数据类型-基本数据类型
    【Leetcode每日一题:1662.检查俩个字符串数组是否相等~~~遍历+模拟+StringBuilder】
    【开发心得】借助修改host测试回调
    数据结构七:七大排序(插入排序,希尔排序,选择排序,堆排序冒泡排序,快速排序,归并排序)
    基于侏儒猫鼬优化的BP神经网络(分类应用) - 附代码
    docker mysqldump备份/恢复数据库
    PX4模块设计之三十五:MulticopterAttitudeControl模块
    Java8 为什么在接口中引入default方法,以及default方法的使用
    【OpenCV】仿射变换中cv2.estimateAffine2D 的原理
    LeetCode53. 最大子数组和
  • 原文地址:https://blog.csdn.net/qq_52126119/article/details/127984726