微信公众号开发获取当前位置

 

微信开始时,获取用户当前地理位置可以不用html5(因为每次都会弹出获取位置的窗口),就可以使用微信的接口获取经纬度,再通过百度地图转变成某城市的名字

<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?type=quick&ak=百度地图密钥&v=1.0"></script>

<script type="text/javascript" >
wx.config({

debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。

appId: '${appId}', // 必填,公众号的唯一标识
timestamp: '${ timestamp}' , // 必填,生成签名的时间戳
nonceStr: '${ nonceStr}', // 必填,生成签名的随机串
signature: '${ signature}',// 必填,签名,见附录1
jsApiList: [

'getLocation' //获取地理位置接口

]
});

wx.ready(function(){
wx.getLocation({
type: 'wgs84',
complete:function(argv){
if(argv.errMsg=="getLocation:fail"){
alert(JSON.stringify(res));
var gc = new BMap.Geocoder();
var pointAdd = new BMap.Point(res.longitude, res.latitude);
gc.getLocation(pointAdd, function(rs){
var city = rs.addressComponents.city;
alert(city);
}
);
}else{
alert("ok");
}
},
cancel: function (res) {
alert('用户拒绝授权获取地理位置');
}
});
});
wx.error(function (res) {
alert("调用微信jsapi返回的状态:"+res.errMsg);
});
});

</script>

 

posted @ 2017-07-27 17:37  MrLuo  阅读(15538)  评论(0编辑  收藏  举报