先说下需求吧,我们的需求是通过一个经纬度,然后打开微信内置的地图页面
代码如下:
//导航到停车场
gogogo() {
//获取当前页面url
//var url = location.href.split('#')[0];
// alert(11111)
// window.location.href =
// "https://apis.map.qq.com/uri/v1/marker?marker=coord:30.595810,103.912830;title:渔虾跳主题餐吧;addr: 城南优品道广场星光广场一楼";
this.$http
.get(
this.HOST +
"user-service/api/wx/getConfig?url=https://nanjingdev.harborgo.com/front/#/parkDetail"
)
.then(data => {
// 记录token
if (data.body.returnCode == "000000") {
wx.config({
// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
debug: false,
// 必填,公众号的唯一标识
appId: data.body.doc.appid,
// 必填,生成签名的时间戳
timestamp: "" + data.body.doc.timestamp,
// 必填,生成签名的随机串
nonceStr: data.body.doc.nonce,
// 必填,签名,见附录1
signature: data.body.doc.signature,
// 必填,需要使用的JS接口列表,所有JS接口列表见附录2
jsApiList: ["checkJsApi", "openLocation"],
success(res){
console.log('sdddd',res)
}
});
} else {
this.$vux.toast.text(data.body.msg);
}
});
wx.ready(function() {
wx.checkJsApi({
jsApiList: ["openLocation"],
success: function(res) {
console.log('res',res, typeof that.lat,typeof that.lng)
wx.openLocation({
latitude: that.lat, // 纬度,浮点数,范围为90 ~ -90
longitude: that.lng, // 经度,浮点数,范围为180 ~ -180。
name: that.parkName, // 位置名
address: '', // 地址详情说明
scale: 15, // 地图缩放级别,整形值,范围从1~28。默认为最大
success(res){
console.log('成功了吗',res)
},
});
}
});
});
},
其中第一步wx.config所需的配置参数,是调用后台提供的接口返回的参数
第二步就是wx.ready下面的代码了,调用wx.openLocation方法调起页面
但是碰到一个棘手的问题,就是,我所有的参数配置成功,api也调用成功,就是没有调起页面,百思不得其解。
各种百度搜索,我试着把调用后台接口的url换了一种写法,结果竟然好了,不知道是不是这个原因,这个url是指本地页面的路径,之前是写死的,现在是获取的,拼在接口后面的
//获取当前页面url
var url = location.href.split('#')[0];
this.$http
.get(
this.HOST +
"user-service/api/wx/getConfig?url="+url
)
.then(data => {
// 记录token
if (data.body.returnCode == "000000") {
wx.config({
// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
debug: false,
// 必填,公众号的唯一标识
appId: data.body.doc.appid,
// 必填,生成签名的时间戳
timestamp: "" + data.body.doc.timestamp,
// 必填,生成签名的随机串
nonceStr: data.body.doc.nonce,
// 必填,签名,见附录1
signature: data.body.doc.signature,
// 必填,需要使用的JS接口列表,所有JS接口列表见附录2
jsApiList: ["checkJsApi", "openLocation"],
success(res){
console.log('sdddd',res)
}
});
} else {
this.$vux.toast.text(data.body.msg);
}
});