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