第一步下载sdk
    微信获取当前地理位置接口 - 图1
    2.前台视图需要的js代码

    1. <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    2. <script charset="utf-8" src="http://map.qq.com/api/js?v=2.exp"></script>
    3. <script>
    4. // 添加收货地址定位
    5. function setWechatJSSDK(res){
    6. wx.config({
    7. debug: false,
    8. appId: res.appId,
    9. timestamp: res.timestamp,
    10. nonceStr: res.nonceStr,
    11. signature: res.signature,
    12. jsApiList: [
    13. 'getLocation',
    14. 'scanQRCode'
    15. ]
    16. });
    17. wx.ready(function () {
    18. // 点击获取地理位置
    19. $('.iconfont').click(function(){
    20. wx.getLocation({
    21. type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
    22. success: function (res) {
    23. var location = res.latitude+','+res.longitude;
    24. var url = 'http://apis.map.qq.com/ws/geocoder/v1/?'
    25. var data={
    26. location: location,
    27. key: 'SQGBZ-QV7RG-EPTQC-IZTVB-ID44J-YMFTA',
    28. output: "jsonp"
    29. };
    30. $.ajax({
    31. type:'get',
    32. dataType:'jsonp',
    33. data:data,
    34. url:url,
    35. success:function(json){
    36. alert(json.result.address);
    37. //这里是替换input的内容
    38. $('#address').html(json.result.address);
    39. // 补充弹框消失效果
    40. },
    41. error : function(err){alert("服务端错误,请刷新浏览器后重试")}
    42. });
    43. }
    44. });
    45. })
    46. $('.getqrcode').click(function(){
    47. wx.scanQRCode({
    48. needResult: 1,
    49. desc: 'scanQRCode desc',
    50. success: function (res) {
    51. var id = res.resultStr.split('=')[1];
    52. alert(JSON.stringify(res));
    53. }
    54. });
    55. })
    56. });
    57. }
    58. function loadWechatJSSDK() {
    59. $.get("{:U('addon/WeiuidDeit/Mobile/getlocation')}",{url:location.href}, function (res) {
    60. setWechatJSSDK(res);
    61. });
    62. }
    63. setTimeout(loadWechatJSSDK, 500);

    3.控制器需要的代码

    1. // 获取地址
    2. public function getlocation(){
    3. $wechatInfo = D('Mp')->find(3);
    4. $options = array(
    5. 'token' => $wechatInfo['valid_token'],
    6. 'encodingaeskey' => $wechatInfo['encodingaeskey'],
    7. 'appid' => $wechatInfo['appid'],
    8. 'appsecret' => $wechatInfo['appsecret']
    9. );
    10. $wechatObj = new Wechat($options);
    11. $data = $wechatObj->getJsSign(I('get.url'));
    12. $this->ajaxReturn($data);
    13. }