search
HomeWeChat AppletWeChat DevelopmentHow to get the current geographical location through WeChat and save it in the session

First, add the WeChat configuration file to the static 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>
其次,在控制器中接收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;    
           }

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.

The above is the detailed content of How to get the current geographical location through WeChat and save it in the session. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor