vue 微信H5页面打开内置地图的指定位置和获取当前位置(转载收藏)

转载地址

1. 根据经纬度打开地图

首先,装一下微信sdk:

css 复制代码
npm i weixin-js-sdk -S

页面:按钮点击之后打开地图(demo演示)

xml 复制代码
<template>
  <div class="map box clmcenter">
    <button @click="openMap">打开地图</button>
  </div>
</template>

js部分:

1.导入weixin-js-sdk

2.在openMap方法中获取签名之后调用wx.openLocation打开地图

javascript 复制代码
import { Toast } from "vant";
import wx from "weixin-js-sdk";
export default {
  methods: {
    openMap() {
      var url = encodeURIComponent(window.location.href.split("#")[0]);
      this.$http
        .post("后台地址", { url })
        .then((res) => {
          let data = res.data;
          wx.config({
            debug: false,
            appId: data.appId,
            timestamp: data.timestamp,
            nonceStr: data.nonceStr,
            signature: data.signature,
            jsApiList: ["checkJsApi", "openLocation"],
            success(res) {
              //   Toast(res);
            },
          });
        });
      wx.ready(() => {
        wx.checkJsApi({
          jsApiList: ["openLocation"],
          success: (rest) => {
            // Toast("ready*****",rest);
            //打开指定位置
            wx.openLocation({
              latitude: 31.232795315128442, // 纬度,浮点数,范围为90 ~ -90
              longitude: 121.47511024687574, // 经度,浮点数,范围为180 ~ -180。
              name: "人民广场", // 位置名
              address: "", // 地址详情说明
              scale: 18, // 地图缩放级别,整型值,范围从1~28。默认为最大
              infoUrl: "", // 在查看位置界面底部显示的超链接,可点击跳转
            });
          },
        });
      });
    },
  },
};

注意:经纬度的值是浮点数!经纬度的值是浮点数!!经纬度的值是浮点数!!!

只要签名中的配置都能正常拿到,IOS和安卓端都可以正常打开地图,调试的时候也可以把debug改为true,方便查看错误信息。

  1. 获取当前位置后打开地图 获取当前地址用到的是wx.getLocation这个接口,具体使用在微信开发者文档中也有说明。

注意 :

  • 在jsApiList中添加getLocation配置,ready()中对应也要添加。
  • wx.getLocation的type值改为 'gcj02' 火星坐标值。
javascript 复制代码
openMap() {
      var url = encodeURIComponent(window.location.href.split("#")[0]);
      this.$http
        .post("https://www.mahelei.com/index.php/Gm/Wechat/share", { url })
        .then((res) => {
          let data = res.data;
          wx.config({
            debug: false,
            appId: data.appId,
            timestamp: data.timestamp,
            nonceStr: data.nonceStr,
            signature: data.signature,
            jsApiList: ["checkJsApi", "getLocation", "openLocation"],
            success(res) {
              //   Toast(res);
            },
          });
        });
      wx.ready(() => {
        wx.checkJsApi({
          jsApiList: ["getLocation", "openLocation"],
          success: (rest) => {
            // Toast("ready*****",rest);
            //获取当前位置
            wx.getLocation({
              type: "gcj02", // 默认为wgs84的 gps 坐标,如果要返回直接给 openLocation 用的火星坐标,可传入'gcj02'
              success: function (res) {
                //打开指定位置
                wx.openLocation({
                  latitude: res.latitude, // 纬度,浮点数,范围为90 ~ -90
                  longitude: res.longitude, // 经度,浮点数,范围为180 ~ -180。
                  name: "我的位置", // 位置名
                  address: "", // 地址详情说明
                  scale: 18, // 地图缩放级别,整型值,范围从1~28。默认为最大
                  infoUrl: "", // 在查看位置界面底部显示的超链接,可点击跳转
                });
              },
            });
          },
        });
      });
    },
相关推荐
行者全栈架构师17 小时前
UniApp集成vk-uview-ui组件库详解:打造高效UI开发体验
前端·vue.js
Csvn19 小时前
Vue 3 defineModel 翻车实录:多个 v-model 绑定到底怎么写?
前端·vue.js
Momo__21 小时前
VueUse createReusableTemplate —— 单文件组件内的模板复用神器
前端·vue.js
程序员小富21 小时前
我开源了一个开发者专属的智能 JSON 工具,得到了媳妇高度认可
前端·vue.js·后端
JustHappy1 天前
「软件设计思想杂谈🤔」“切图仔”也能懂编译原理?框架源码也许没那么难。聊聊 Vue 的编译(上)
前端·javascript·vue.js
Jinkey1 天前
要用户手机号真的是为了打骚扰电话吗?浅谈微信生态会员账号体系与资产合并
后端·微信·微信小程序
假如让我当三天老蒯2 天前
Options API(选项式 API) 和 Composition API(组合式 API)
前端·vue.js·面试
秃头网友小李5 天前
前端难点:keep-alive 缓存什么?RouterView 的 key 为什么要带 scopeId?
前端·vue.js
徐小夕5 天前
JitWord 3.0 正式发布,高精度Word异构解析+复杂组件兼容,打造web端协同Word编辑器
前端·vue.js·算法
奋斗吧程序媛6 天前
补充一个小知识点:有关@click.native
前端·vue.js