Get location information via WeChat, save to server use

Source: Internet
Author: User
First, in the page, add the configuration file to get through JS.

<script type= "Text/javascript" > Wx.config ({debug:false, appId: ' {$signPackage. appId} ', timestamp: ' {$sig      Npackage.timestamp} ', Noncestr: ' {$signPackage. noncestr} ', Signature: ' {$signPackage. Signature} ', Jsapilist: [  All APIs to be called are added to this list ' Checkjsapi ', ' openlocation ', ' getLocation ', ' Scanqrcode ']};       Wx.ready (function () {$ (' #scan '). Click (function () {Wx.scanqrcode ({needresult:0,});    }); Wx.checkjsapi ({jsapilist: [' getLocation '], success:function (res) {if (Res.checkresult . GetLocation = = False) {alert (' Your version is too low to support JS interface, please upgrade to the latest version!           ');        Return    }      }    }); Wx.getlocation ({success:function (res) {var latitude = res.latitude;//latitude, floating point, range from -90 var lo Ngitude = Res.longitude;        Longitude, floating point number, RANGE 180 ~-180. var geoconv = ' Http://api.map.baidu.com/geoconv/v1/?callback=coordinateTransformation&cooRds= ' + longitude + ', ' + latitude + ' &from=1&to=5&ak=5bfnbsgnvf5g2o72npvtdxfm ';        var script = document.createelement (' script ');        SCRIPT.SRC = Geoconv;       Document.head.appendChild (script);         }, Cancel:function (res) {alert (' user denied authorization to obtain geographical location ');  }     });  });    function Coordinatetransformation (data) {var latlng = data.result[0].y + ', ' + data.result[0].x; var url = ' http://api.map.baidu.com/geocoder/v2/?callback=getcurrentlocation&ak=5bfnbsgnvf5g2o72npvtdxfm&    Location= ' + latlng + ' &output=json&pois=1 ';    var script = document.createelement (' script ');    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 = ' street= ' + address + ' &name= ' + street + ' &lng= ' + x + ' &lat= ' + y + ' &c      Ity= ' + City;       var url = "{: U (' index/saveposition ')}";      $.getjson (url,{' name ': Street, ' LNG ': x, ' lat ': Y, ' City ': city},function (data) {if (Data.returncode) {}}); }}</script>

Next, receive the geographic coordinates of the Ajax pass in the controller and save it to the session.

Public Function saveposition () {$city   = II (' get.city ', ' ', ' trim ');  $ADDR = II (' get.name ', ' ', ' trim ');  $LNG   = II (' get.lng ', ' ', ' trim ');  $lat    = II (' Get.lat ', ' ', ' trim ');  $myLocation = Array (' City ' =   $city, ' addr ' + $addr, ' lng ' =   = $lng, ' lat ' = +   $lat,);  $_session[' mylocation '] = $myLocation;  $data [' returncode '] = 1; $data [' returninfo '] = ' Get location successful!  ';  $this->ajaxreturn ($data); return;}


Note: The thinkphp framework is used, and II is a custom method to get the value of a GET or post pass, just like the I function.

Related recommendations:

[Course] Small Program development documentation

JS interface Summary and use detailed _javascript tips

The development of the public number lbs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.