Home  >  Article  >  Backend Development  >  Obtain location information through WeChat and save it to the server for use

Obtain location information through WeChat and save it to the server for use

*文
*文Original
2017-12-22 15:51:342236browse

First, add the WeChat configuration file to the page and obtain it through 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;用户拒绝授权获取地理位置&#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>

Secondly, receive the geographical coordinates passed by ajax in the controller, and then save them to the 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;
}


Note: The thinkphp framework is used. II is a custom method to obtain the value passed by get or post, the same as the I function.

Related recommendations:

[Course] WeChat Mini Program Development Document

WeChat JS interface summary and usage details_javascript skills

WeChat public account development LBS

The above is the detailed content of Obtain location information through WeChat and save it to the server for use. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn