- 用的是腾讯地图引入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("系统错误,请联系管理员!") } }); }
微信获取用户的经纬度并转成详细地址
最新推荐文章于 2023-03-18 18:49:17 发布