基于高德地图api,vue实现步行导航

vue代码

<template>
  <div>
    <van-nav-bar
      title="导航"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
       style="margin-top: .2rem;"
    />
    起始位置:
    <van-cell-group style="margin-top: .2rem;">
      <van-field id="start" v-model="startName" />
    </van-cell-group>
    结束位置:
    <van-cell-group style="margin-top: .2rem;">
      <van-field id="end" v-model="endName" placeholder="输入目的地" />
    </van-cell-group>

    <div id="container" />
    <div id="container1" />
    <van-button type="info" style="margin-top: .2rem;" @click="goView">去往这里</van-button>

<div class="chat">
  <Chat></Chat>

</div>
  


    <van-popup v-model="show" class="tanchuang">
      <h1 class="biaoti">是否允许获取当前定位</h1>
      <div>
        <div class="buttonjz">
          <van-button round type="info" @click="position">获取当前定位</van-button>
          <van-button disabled round  style=" margin-left: .3rem;" type="danger"> 不允许 </van-button>
          </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import AMapLoader from "@amap/amap-jsapi-loader";
import Chat from './Chat.vue';
export default {
  data: () => {
    return {
      map: null,
      startName: "",
      endName: "",
      show: true,
    };
  },
  mounted() {
    window._AMapSecurityConfig = {
      securityJsCode: "agc19f2351109059453422cfdfb2b66d", // 申请key对应的秘钥
    };
    // DOM初始化完成进行地图初始化
    this.initMap();
    //   this.position()
  },
   components:{
    Chat
  },
  methods: {
    onClickLeft() {
      this.$router.replace("/");
    },
    goView() {
      document.getElementById("container").style.zIndex = 9;
      document.getElementById("container").style.display = "block";
      document.getElementById("container1").style.display = "none";
      // eslint-disable-next-line no-undef
      const walking = new AMap.Walking({
        map: this.map,
        // 步行路线规划策略
        policy: "panel",
      });
      const points = [
        { keyword: this.startName, city: "全国" },
        { keyword: this.endName, city: "全国" },
      ];

      walking.search(points, (status, result) => {
        // 未出错时,result即是对应的路线规划方案
        console.log("status=", status);
        console.log("result=", result);
      });
    },
    position() {
      var that = this;
      that.show = !that.show;

      document.getElementById("container").style.display = "none";
      document.getElementById("container1").style.display = "block";
      //初始化地图对象,加载地图
      var map = new AMap.Map("container1", {
        resizeEnable: true,
      });
      var options = {
        // 设置定位超时时间,默认:无穷大
        timeout: 4000,
        // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
        zoomToAccuracy: true,
        showButton: true, //是否显示定位按钮
        position: "LB", //定位按钮的位置
        /* LT LB RT RB */
        offset: [10, 20], //定位按钮距离对应角落的距离
        showMarker: true, //是否显示定位点
        markerOptions: {
          //自定义定位点样式,同Marker的Options
          offset: new AMap.Pixel(-18, -36),
          content:
            '<img src="https://a.amap.com/jsapi_demos/static/resource/img/user.png" style="width:36px;height:36px"/>',
        },
        showCircle: false, //是否显示定位精度圈
        circleOptions: {
          //定位精度圈的样式
          strokeColor: "#0093FF",
          noSelect: true,
          strokeOpacity: 0.5,
          strokeWeight: 1,
          fillColor: "#02B0FF",
          fillOpacity: 0.25,
        },
      };
      AMap.plugin(["AMap.Geolocation"], function () {
        var geolocation = new AMap.Geolocation(options);
        map.addControl(geolocation);
        geolocation.getCurrentPosition(function (status, result) {
          if (status == "complete") {
            onComplete(result);
          } else {
            onError(result);
          }
        });
      });

      function onComplete(data) {
        var str = [];
        str.push("定位结果:" + data.position);
        str.push("定位类别:" + data.location_type);
        if (data.accuracy) {
          str.push("精度:" + data.accuracy + " 米");
        } //如为IP精确定位结果则没有精度信息
        str.push("是否经过偏移:" + (data.isConverted ? "是" : "否"));
        // console.log(str)

        AMap.plugin("AMap.Geocoder", function () {
          var geocoder = new AMap.Geocoder({
            // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
            city: "010",
          });

          var lnglat = [data.position];

          geocoder.getAddress(lnglat, function (status, result) {
            if (status === "complete" && result.info === "OK") {
              // result为对应的地理位置详细信息
              console.log(result.regeocodes[0].formattedAddress);
              that.startName = result.regeocodes[0].formattedAddress;
              // console.log(that.startName)
            }
          });
        });
      }
      //解析定位错误信息
      function onError(data) {
        console.log("定位失败");
        console.log(
          "失败原因排查信息:" +
            data.message +
            "</br>浏览器返回信息:" +
            data.originMessage
        );
      }
    },
    initMap() {
      AMapLoader.load({
        key: "cba319d68cfe18669d7f2e94b5f74ad4", // 申请好的Web端开发者Key,首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: ["AMap.ToolBar", "AMap.Walking", "AMap.AutoComplete"], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      })
        .then((AMap) => {
          this.map = new AMap.Map("container", {
            // 设置地图容器id
            viewMode: "3D", // 是否为3D地图模式
            zoom: 10, // 初始化地图级别
            // center: [105.602725, 37.076636] // 初始化地图中心点位置
          });
          const autoOptions = {
            // city 限定城市,默认全国
            city: "全国",
            input: "start",
          };
          // 实例化AutoComplete
          const autoComplete = new AMap.AutoComplete(autoOptions);
          const autoOptions2 = {
            // city 限定城市,默认全国
            city: "全国",
            input: "end",
          };
          // 实例化AutoComplete
          const autoComplete2 = new AMap.AutoComplete(autoOptions2);
          // 根据关键字进行搜索
          /* autoComplete.search(this.startName, (status, result) => {
            // 搜索成功时,result即是对应的匹配数据
            console.log(result)
          }) */
        })
        .catch((e) => {
          console.log(e);
        });
    },
  },
};
</script>
<style scoped>
#container {
  /* display: block; */
  padding: 0px;
  margin: 0px;
  width: 100%;
  height: 3rem;
}
#container1 {
  display: none;

  padding: 0px;
  margin: 0px;
  width: 100%;
  height: 3rem;
}
#panel {
  position: fixed;
  background-color: white;
  max-height: 90%;
  overflow-y: auto;
  top: 10px;
  right: 10px;
  width: 280px;
}

#panel .amap-call {
  background-color: #009cf9;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
}

#panel .amap-lib-walking {
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
  overflow: hidden;
}
.biaoti {
  font-weight: 400;
  font-size: 25px;
  margin-top: 0.3rem;
  margin-left: 0.1rem;
  margin-bottom: 0.5rem;
}
.tanchuang {
  width: 4rem;
  height: 3rem;
  border-radius: 15px;
  /* background-color: aqua; */
}
.buttonjz{
   margin-left: .3rem;
}
.chat{
  margin-top: .2rem;
}
</style>

实现效果

 

进入界面是获取当前地址,输入目的地就规划出路线。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!关于高德地图APIVue2中的使用,你可以按照以下步骤进行操作: 1. 首先,你需要在你的Vue项目中安装高德地图的SDK。你可以通过npm或yarn来安装,命令如下: ``` npm install vue-amap``` 或``` yarn add vue-amap``` 2. 安装完成后,在你的Vue组件中引入高德地图的SDK,并注册为全局组件。在你的入口文件(一般是main.js)中,添加以下代码: ```javascriptimport VueAMap from 'vue-amap'; Vue.use(VueAMap); VueAMap.initAMapApiLoader({ key: 'your-amap-api-key', plugin: [ 'AMap.Geolocation', 'AMap.Marker' ], v: '1.4.15', }); ``` 请确保将'your-amap-api-key'替换为你自己申请的高德地图API的密钥。 3. 在你想要显示地图的组件中,使用`<el-amap>`标签来渲染地图。例如: ```vue<template> <div> <el-amap :zoom="12" :center="center"> <el-amap-marker :position="markerPosition" /> </el-amap> </div> </template> <script> export default { data() { return { center: [116.397428,39.90923], // 地图中心点坐标 markerPosition: [116.397428,39.90923], // 标记点坐标 }; }, }; </script> ``` 上面的代码演示了如何在地图上显示一个标记点。你可以根据自己的需求来设置地图的中心点和标记点的坐标。 这样,你就可以在Vue项目中使用高德地图API了。当然,高德地图API还有很多其他功能和组件,你可以根据自己的需求进行调整和扩展。希望对你有帮助!如需进一步了解,请查阅高德地图APIvue-amap的官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值