


Obtain location information through WeChat and save it to the server for use
First, add the WeChat configuration file to the 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>
Secondly, receive the geographical coordinates passed by ajax in the controller, and then save them 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'] = '获取位置成功!'; $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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools