


How to get the current geographical location through WeChat and save it in the session
Sep 14, 2017 am 09:04 AMFirst, 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: [ // 所有要調(diào)用的 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; // 緯度,浮點數(shù),范圍為90 ~ -90 var longitude = res.longitude; // 經(jīng)度,浮點數(shù),范圍為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('用戶拒絕授權(quán)獲取地理位置'); } }); }); 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傳遞的地理坐標(biāo),然后保存到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

Undress AI Tool
Undress images for free

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

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

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Yes, for security, personalized services and account management, Amap requires registration with a mobile phone number. The registration steps include: Open the Amap app, click "My" and "Login/Register", select a mobile phone number to register, enter the mobile phone number to get the verification code, set a password to complete the registration.

Yes, you can modify the Douyin IP location as follows: Open Douyin and edit your profile. Modify the city information and select the city or region you want to display. Log out and log back in for the changes to take effect.

Users can not only watch a variety of interesting short videos on Douyin, but also publish their own works and interact with netizens across the country and even the world. In the process, Douyin’s IP address display function has attracted widespread attention. 1. How is Douyin’s IP address displayed? Douyin’s IP address display function is mainly implemented through geographical location services. When a user posts or watches a video on Douyin, Douyin automatically obtains the user's geographical location information. This process is mainly divided into the following steps: first, the user enables the Douyin application and allows the application to access its geographical location information; secondly, Douyin uses location services to obtain the user's geographical location information; finally, Douyin transfers the user's geographical location information Geographic location information is associated with the video data they posted or watched and will

The steps for posting pictures and videos at the same time on Weibo are as follows: Select related or complementary pictures and videos. Open the Weibo client and click the Publish button. Select the "Pictures & Videos" tab. Add pictures and videos (up to 9 pictures and 1 video). Enter text and add relevant information. Just publish it.

As a social platform focusing on short videos, Douyin’s recommendation algorithm is one of its core functions. It can recommend relevant video content based on users' interests and behaviors. Sometimes users may want to reset the recommendation algorithm to get content more in line with their preferences. So, how to reset Douyin recommendations? How to change Douyin recommendation to featured? This article will answer both questions for you. 1. How to reset Douyin recommendations? 1. Open Douyin APP and enter your personal homepage. 2. Click the "Settings" icon in the upper right corner to enter the settings page. 3. On the settings page, find the "Recommended Management" option and click to enter. 4. On the recommendation management page, you can see your interest tags and interest preferences. You can select or deselect different

Steps to modify Douyin location permissions: 1. Open the Douyin app and click "Me". 2. Click the "three horizontal bars icon" in the upper right corner. 3. Select Settings. 4. Find "Privacy Settings" and click on it. 5. Click "Location Services". 6. Select Allow targeting or Only ask when using, as appropriate. 7. After modification, you need to restart the Douyin application to take effect.

Quark Browser is a lightweight browser with very good compatibility and has been recognized by the majority of users today. However, some users have encountered the problem of slow download speed when using Quark Browser. In fact, the download speed is affected by many factors. Let’s take a look with the editor below! 1. Change the download source: specific download source server Slow download speeds may be caused by high load or geographical distance. You can try to find an alternate download link through a search engine, or visit the software's official website to get a direct download link. 2. Check the network connection: Make sure your network connection is stable and fast. If you're using a wireless network, try moving closer to your router or using a wired connection. 3. Close other programs that occupy bandwidth: close other programs that are running

1. First, we open the camera and click the settings icon in the upper right corner. 2. Turn off the geolocation and automatic watermark switches.
