


How 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: '{$signPackage.appId}', timestamp: '{$signPackage.timestamp}', nonceStr: '{$signPackage.nonceStr}', signature: '{$signPackage.signature}', jsApiList: [ // 所有要调用的 API 都要加到这个列表中 '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('你的微信版本太低,不支持微信JS接口,请升级到最新的微信版本!'); return; } } }); wx.getLocation({ success: function (res) { var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90 var longitude = res.longitude; // 经度,浮点数,范围为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('用户拒绝授权获取地理位置'); } }); }); 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 + '&city=' + city; var url = "{:U('Index/savePosition')}"; $.getJSON(url,{'name':street,'lng':x,'lat': y,'city':city},function(data) { if(data.returnCode) { } }); } } </script> 其次,在控制器中接收ajax传递的地理坐标,然后保存到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'] = '获取位置成功!'; $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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

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
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor