代码如下:
<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>//重点 引入这句
<!-- <script type="text/javascript" src="https://res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/vconsole/2.5.1/vconsole.min.js"></script> -->//测试 引用
function jsSdk() {
var baseUrl = '***********';
$.ajax({
cache: false,
url: baseUrl + "/api/boss/js_sign",
type: "GET",
data: 'URL=' + encodeURIComponent(location.href.split('#')[0]),
dataType: "json",
success: function (json) {
if (json) {
jsLocation(json)
}
},
error: function (e, errorType, error) {
},
});
};
function jsLocation(params) {
wx.config({
beta: true,
debug: false,
appId: params.appId,
timestamp: params.timestamp,
nonceStr: params.nonceStr,
signature: params.signature,
jsApiList: ['checkJsApi', 'openLocation', 'getLocation'] // 必填,需要使用的JS接口列表
});
wx.ready(function () {
wx.checkJsApi({
jsApiList: ['getLocation'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
success: function (res) {
if (res.checkResult.getLocation == false) {
alert("你的微信版本太低,不支持微信JS接口,请升级到最新的微信版本!");
return;
}
}
});
var latitude = '';
var longitude = '';
var speed;
var accuracy;
wx.getLocation({
success: function (res) {
var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
var speed = res.speed; // 速度,以米/每秒计
var accuracy = res.accuracy; // 位置精度
var baseUrl = '*********';
//获取微信管理员信息
var location = {
"cafeId": window.cafeId,
"gpsLng": longitude,
"gpsLat": latitude,
}
$.ajax({
cache: false,
url: baseUrl + "/api/boss/update_cafe_location",
type: "POST",
contentType: "application/json",
data: JSON.stringify(location),
dataType: "json",
success: function (json) {
if (json.success) {
alert("上传位置信息成功!")
} else {
alert("网络错误!")
}
},
error: function (e, errorType, error) {
alert("请求异常")
},
});
},
fail: function (e) {
alert("未能获取地理位置信息");
},
cancel: function (res) {
console.log('_____未能获取地理位置______');
alert("未能获取地理位置");
}
});
});
};