首页 > 微信应用  > 

如何通过微信获取当前地理位置并将其存到session中

如何通过微信获取当前地理位置并将其存到session中
首先,在静态页面中,添加微信的配置文件,通过js获取。

首先,在静态页面中,添加微信的配置文件,通过js获取。

<script type="text/javascript">  wx.config({    debug: false,    appId: &#39;{$signPackage.appId}&#39;,    timestamp: &#39;{$signPackage.timestamp}&#39;,    nonceStr: &#39;{$signPackage.nonceStr}&#39;,    signature: &#39;{$signPackage.signature}&#39;,    jsApiList: [      // 所有要调用的 API 都要加到这个列表中      &#39;checkJsApi&#39;,      &#39;openLocation&#39;,       &#39;getLocation&#39;,       &#39;scanQRCode&#39;    ]  });  wx.ready(function () {    $(&#39;#scan&#39;).click(function(){      wx.scanQRCode({        needResult: 0,      });    });    wx.checkJsApi({       jsApiList: [         &#39;getLocation&#39;      ],      success: function (res) {        if (res.checkResult.getLocation == false)        {          alert(&#39;你的微信版本太低,不支持微信JS接口,请升级到最新的微信版本!&#39;);           return;        }      }    });    wx.getLocation({      success: function (res) {         var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90        var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。        var geoconv = &#39;http://api.map.baidu.com/geoconv/v1/?callback=coordinateTransformation&coords=&#39; + longitude + &#39;,&#39; + latitude + &#39;&from=1&to=5&ak=5BFNbSgnVF5g2O72NpvTDxFm&#39;;        var script = document.createElement(&#39;script&#39;);        script.src = geoconv;        document.head.appendChild(script);       },      cancel: function (res) {        alert(&#39;用户拒绝授权获取<a style='color:#f60; text-decoration:underline;' href="https://www.php.cn/zt/36976.html" target="_blank">地理位置</a>&#39;);      }     });  });  function coordinateTransformation(data)  {    var LATLNG = data.result[0].y + &#39;,&#39; + data.result[0].x;    var url = &#39;http://api.map.baidu.com/geocoder/v2/?callback=getCurrentLocation&ak=5BFNbSgnVF5g2O72NpvTDxFm&location=&#39; + LATLNG + &#39;&output=json&pois=1&#39;;    var script = document.createElement(&#39;script&#39;);    script.src = url;    document.head.appendChild(script);   }  function getCurrentLocation(data)  {    if(data.status === 0)     {      var address = data.result.formatted_address,      x = data.result.location.lng,      y = data.result.location.lat,      city = data.result.addressComponent.city,      street = data.result.addressComponent.street || data.result.formatted_address,      reqData = &#39;street=&#39; + address + &#39;&name=&#39; + street + &#39;&lng=&#39; + x + &#39;&lat=&#39; + y + &#39;&city=&#39; + city;      var url = "{:U(&#39;Index/savePosition&#39;)}";       $.getJSON(url,{&#39;name&#39;:street,&#39;lng&#39;:x,&#39;lat&#39;: y,&#39;city&#39;:city},function(data)       {         if(data.returnCode) { }      });     }   }</script>其次,在控制器中接收ajax传递的地理坐标,然后保存到session中。public function savePosition() {   $city = II(&#39;get.city&#39;,&#39;&#39;,&#39;trim&#39;);   $addr = II(&#39;get.name&#39;,&#39;&#39;,&#39;trim&#39;);   $lng = II(&#39;get.lng&#39;,&#39;&#39;,&#39;trim&#39;);   $lat = II(&#39;get.lat&#39;,&#39;&#39;,&#39;trim&#39;);   $myLocation = array(      &#39;city&#39; =>$city,      &#39;addr&#39; =>$addr,      &#39;lng&#39; =>$lng,     &#39;lat&#39; =>$lat,   );  $_SESSION[&#39;MyLocation&#39;] = $myLocation;   $data[&#39;returnCode&#39;] = 1;      $data[&#39;returnInfo&#39;] = &#39;获取位置成功!&#39;;      $this->ajaxReturn($data);      return;    }

如何通过微信获取当前地理位置并将其存到session中由讯客互联微信应用栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“如何通过微信获取当前地理位置并将其存到session中