查看: 725|回复: 0

微信jssdk定位详解

[复制链接]

748

主题

39

回帖

3907

积分

管理员

总舵主

积分
3907
QQ
发表于 2024-5-25 20:15:17 | 显示全部楼层 |阅读模式
先看html代码:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.         <meta charset="UTF-8">
  5.         <title>微信定位</title>        
  6. </head>
  7. <body>
  8.         
  9. </body>
  10. <!-- 这里src中前面可不加http/https -->
  11. <script type="text/javascript" src="//res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
  12. <script type="text/javascript">
  13.         // 此方法只有在微信中有效
  14.         wx.config({
  15.             debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  16.             appId: '{$appid}', // 必填,公众号的唯一标识
  17.             timestamp: {$timestamp}, // 必填,生成签名的时间戳
  18.             nonceStr: '{$nonceStr}', // 必填,生成签名的随机串
  19.             signature: '{$signature}',// 必填,签名
  20.             jsApiList: ['getLocation', 'openLocation'] // 必填,需要使用的JS接口列表
  21.         });
  22.         // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
  23.         wx.ready(function(){
  24.             getLocation(openLocation);
  25.         });
  26.         // 获取地理位置接口
  27.         function getLocation(callback) {
  28.                 wx.getLocation({
  29.                         type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  30.                         success: function (res) {
  31.                                 var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
  32.                                 var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
  33.                                 var speed = res.speed; // 速度,以米/每秒计
  34.                                 var accuracy = res.accuracy; // 位置精度
  35.                                 // 判断返回值是否定义
  36.                                 if(callback != undefined) {
  37.                                         callback(res);
  38.                                 }
  39.                         }
  40.                 });
  41.         }
  42.         
  43.         // 使用微信内置地图查看位置接口,创建一个方法
  44.         function openLocation(res) {
  45.                 wx.openLocation({
  46.                         latitude: res.latitude, // 纬度,浮点数,范围为90 ~ -90
  47.                         longitude: res.longitude, // 经度,浮点数,范围为180 ~ -180。
  48.                         name: '', // 位置名
  49.                         address: '', // 地址详情说明
  50.                         scale: 15, // 地图缩放级别,整形值,范围从1~28。默认为最大
  51.                         infoUrl: '' // 在查看位置界面底部显示的超链接,可点击跳转
  52.                 });
  53.         }        
  54. </script>
  55. </html>
复制代码
微擎实际操作:
  1. <script>
  2. wx.ready(function(){
  3.         wx.getLocation({
  4.         type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  5.         success: function (res) {
  6.             var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
  7.             var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
  8.             var speed = res.speed; // 速度,以米/每秒计
  9.             var accuracy = res.accuracy; // 位置精度
  10.                   var qqmapkey = "{php echo $this->module['config']['qqmapkey']}";
  11.             $.ajax({
  12.                       url:"http://apis.map.qq.com/ws/geocoder/v1/?location=" +latitude+ "," +longitude+ "&coord_type=5&key=" + qqmapkey + "&output=jsonp&callback=calllocation" ,
  13.                 type:"GET",
  14.                 dataType:'jsonp',
  15.                 jsonp:'calllocation'
  16.             });
  17.         },
  18.         cancel: function (res) {
  19.              alert('您拒绝了我们的位置请求!');
  20.         }
  21.     });
  22.          
  23. });
  24. function calllocation(data){
  25.     //var name=data.result.formatted_addresses.rough;
  26.           var name = data.result.address_component.street;
  27.     var address=data.result.formatted_addresses.recommend;
  28.     var lat=data.result.location.lat;
  29.     var lng=data.result.location.lng;
  30.           var city=data.result.address_component.city;
  31.     alert("我的城市:" + city);
  32.     wx.openLocation({
  33.         latitude: lat, // 纬度,浮点数,范围为 90 ~ -90
  34.         longitude: lng, // 经度,浮点数,范围为 180 ~ -180 。
  35.         name: name, // 位置名
  36.         address: address, // 地址详情说明
  37.         scale: 18, // 地图缩放级别 , 整形值 , 范围从 1~28 。默认为最大
  38.         infoUrl: '' // 在查看位置界面底部显示的超链接 , 可点击跳转
  39.     });
  40. }
  41. </script>
复制代码
腾讯地址https://lbs.qq.com/webservice_v1/guide-gcoder.html

道不同,不相为谋!理想要有,万一实现了呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:2513533699@qq.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|鲁ICP备19000917号|鲁公网安备37078302000320号

在本版发帖
关注公众号
QQ客服返回顶部