近期接到大连某个项目,一个H5的活动,其中有一个需求就是:这个活动的参与者仅限大连地区的用户
所以参考了微信API
得出的操作结果为:
wx.ready(function() {
wx.getLocation({
type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function (res) {
var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
var speed = res.speed; // 速度,以米/每秒计
var accuracy = res.accuracy; // 位置精度
if (longitude >= 121.09 && longitude <= 123.66 && latitude >= 38.74 && latitude <= 40.43) {
//是大连
}else if (longitude >= 102.54 && longitude <= 104.53 && latitude >= 30.05 && latitude <= 31.26) {
//是成都(我是成都的,这里是关于我这边测试用的)
// $(".zhezhao p").text("本活动仅限大连地区用户参与");
// $(".zhezhao").show();
} else {
$(".zhezhao p").text("本活动仅限大连地区用户参与");
$(".zhezhao").show();
};
},cancel: function () {
$(".zhezhao p").text("位置获取失败,禁止参与");
$(".zhezhao").show();
},fail: function () {
$(".zhezhao p").text("位置获取失败,禁止参与");
$(".zhezhao").show();
}
});
});