Loading

微信获取地理位置转城市demo

    <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak= 你的key"></script>  
    <?php $signPackage=GetSignPackage();?>
    <script language="javascript" type="text/javascript">
    <!-- //获取地理位置 -->
    wx.config({
        debug: false,//这里是开启测试,如果设置为true,则打开每个步骤,都会有提示,是否成功或者失败
        appId: '<?php echo $signPackage["appId"];?>',
        timestamp: '<?php echo $signPackage["timestamp"];?>',
        nonceStr: '<?php echo $signPackage["nonceStr"];?>',
        signature: '<?php echo $signPackage["signature"];?>',
        jsApiList: [
          // 所有要调用的 API 都要加到这个列表中
            'getLocation'
        ]
    });
    
    wx.ready(function () {
        wx.getLocation({
            type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
            success: function (res) {
                //alert(res.latitude);
                var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
                var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
                var speed = res.speed; // 速度,以米/每秒计
                var accuracy = res.accuracy; // 位置精度
                var gc = new BMap.Geocoder();
                var pointAdd = new BMap.Point(res.longitude, res.latitude);
                gc.getLocation(pointAdd, function(rs) {
                    var city = rs.addressComponents.city;
                    $(".citycon").text(city.replace('市',''));
                });
            },
            //用户取消授权位置回调函数
            cancel:function(res){
                //下面执行定位后的函数
                $(".citycon").text('请选择');
            },
            fail: function () { 
              // 用户shibai分享后执行的回调函数
                $(".citycon").text('请选择');
            }
        });
        
    });
    </script>

 微信文档地址: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115

 

posted @ 2018-07-05 11:41  王召波  阅读(302)  评论(0编辑  收藏  举报