shaoing

 

<script>

wx.config({  
    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。  
    appId: \'{$wx_config.appId}\', 
    timestamp: \'{$wx_config.timestamp}\', 
    nonceStr: \'{$wx_config.nonceStr}\', 
    signature: \'{$wx_config.signature}\',
    jsApiList: [\'checkJsApi\',
                \'chooseImage\',
                \'previewImage\',
                 \'uploadImage\',
                 \'downloadImage\',
                  \'getNetworkType\',//网络状态接口
                  \'openLocation\',//使用微信内置地图查看地理位置接口
                  \'getLocation\' //获取地理位置接口
               ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2  
});


function get2set2addr(latitude, longitude){

    $.ajax({
        url:"{:url(\'addr/ajax_addr\')}",
        data:{\'latitude\':latitude, \'longitude\':longitude},
        dataType:"json",
        type:"post",
        success:function(r){
            if(r.check==1){
                $(\'#addr\').val(r.msg);
                
            }else{
                alert(r.msg); 
            }

        }
    })
}

function get_la_lo(){
    wx.getLocation({
        type: \'wgs84\', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入\'gcj02\'
        success: function (res) {
      
        var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
        var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。

        //alert(JSON.stringify(res));

        get2set2addr(latitude, longitude);
        
      },

      cancel: function (res) {
        alert(\'用户拒绝授权,无法获取地理位置\');
      }

    });     
}

               
wx.ready(function () { 

// 7 地理位置接口 开始
  // 7.1 查看地理位置

$("#openLocation").click(function(){  
    wx.openLocation({
      latitude: 23.099994,
      longitude: 113.324520,
      name: \'TIT 创意园\',
      address: \'广州市海珠区新港中路 397 号\',
      scale: 14,
      infoUrl: \'http://weixin.qq.com\'
    });
});


  // 7.2 获取当前地理位置

$("#addr").click(function(){  

    get_la_lo();

}); 
// 7 地理位置接口 结束              
               


});
</script>

 

    public function ajax_addr()
    {
        if (request()->isAjax()) {//ajax latitude, longitude

            $d = $this->request->param();

            $r = https_request("http://apis.map.qq.com/ws/geocoder/v1/?location=".$d[\'latitude\'].",".$d[\'longitude\']."&key=F2GBZ-SREWQ-A3K56-GSLK5-ELOHS-PRB2X&get_poi=1");

            if(!$r[\'result\'][\'address\']){

                exit( json_encode([\'check\'=>0, \'msg\'=>\'广东省深圳市\']) );
            
            }else{

                exit( json_encode([\'check\'=>1, \'msg\'=>$r[\'result\'][\'address\']]) );
            }

        }
    }

 

分类:

技术点:

相关文章: