微信获取用户的经纬度并转成详细地址

本文介绍了如何在微信中获取用户的经纬度,并利用腾讯地图的JS文件将这些坐标逆解析为详细的地理位置地址。
摘要由CSDN通过智能技术生成
  1. 用的是腾讯地图引入js文件
    <script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=您申请的秘钥"></script>
    <script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js" type="text/javascript" charset="utf-8"></script>

    2.获取经纬度逆解析为地址

    	//微信获取个人位置
                var url = window.location.href;
                // var urlParam = Public.urlParam();
                // var vendorId = urlParam.vendorId;
                $(function() {
                    $.ajax({
                        type: "POST",        
                        url:"后台接口,获取微信的appid,签名等参数",
                        data: {
                            urlStr: url,
                            vendorId: 0
                        },
                        async: false,
                        success: function (data) {
                            // alert(JSON.stringify(data));
                            if (data.status == 1 && data.dataValue) {
                                var data1 = JSON.parse(data.dataValue);
                                // alert(JSON.stringify(data1));
                                configWXJSApi(data1.url, data1.appId, data1.timestamp, data1.nonceStr, data1.signature);
                            }
                        },
                       error:function () {
    			            alert("失败了")
                            }
    
                    });
                    function configWXJSApi(shareUrl, appId, timestamp, nonceStr, signature) {
                        var jsApiList = [
                            'openLocation',
                            'getLocation'
                        ] // 使用微信内置地图查看位置接口、获取地理位置接口
                        wx.config({
                            debug: false,
                            appId: appId,
                            timestamp: timestamp,
                            nonceStr: nonceStr,
                            signature: signature,
                            jsApiList: jsApiList
                        });
    
                        wx.ready(function() {
                            wx.checkJsApi({
                                jsApiList: jsApiList, // 需要检测的JS接口列表,所有JS接口列表见附录2,
                                success: function(res) {
                                    // 以键值对的形式返回,可用的api值true,不可用为false
                                    // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
                                    wx.getLocation({
                                        type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
                                        success: function (res) {
                                            // alert(JSON.stringify(res));
                                            var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
                                            var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
                                            var speed = res.speed; // 速度,以米/每秒计
                                            var accuracy = res.accuracy; // 位置精度
                                            // alert(latitude)
                                            // alert(longitude)
                                            geocoder(latitude,longitude)
                                            /*wx.openLocation({
                                             latitude: latitude, // 纬度,浮点数,范围为90 ~ -90
                                             longitude: longitude, // 经度,浮点数,范围为180 ~ -180。
                                             //name: 'aaa', // 位置名
                                             //address: '123asd', // 地址详情说明
                                             scale: 15, // 地图缩放级别,整形值,范围从1~28。默认为    最大
                                             //infoUrl: 'aaa' // 在查看位置界面底部显示的超链接,可点击跳转
                                             });*/
                                        }
                                    });
                                }
                            });
                        });
                        wx.error(function(res) {
                            alert("失败了"+res.message)
                        });
                    }
                });
                //将经纬度解析为详细的地址
                function geocoder(latitude,longitude) {
                    var ll=latitude+","+longitude;
                    $.ajax({
                        type : 'get',
                        url : 'http://apis.map.qq.com/ws/geocoder/v1',
                        dataType:'jsonp',
                        data : {
                            key:"xxxxxxx",//开发密钥
                            location:ll,
                            //位置坐标
                            get_poi:"1",//是否返回周边POI列表:1.返回;0不返回(默认)
                            coord_type:"1",//输入的locations的坐标类型,1 GPS坐标
                            parameter:{"scene_type":"tohome","poi_num":20},//附加控制功能
                            output:"jsonp"
                        },
                        success : function(data, textStatus) {
                            if(data.status == 0){
                                var address = data.result.address;
                                // $("#address").html(address);
                                // alert(address)
                                $("#currentAddress").html(address);
                            }else {
                                alert("系统错误,请联系管理员!")
                            }
                        },
                        error : function() {
                            alert("系统错误,请联系管理员!")
                        }
                    });
                }

     

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值