首先,在静态页面中,添加微信的配置文件,通过js获取。
script type= text/javascript wx.config({ debug: false, appId: #39;{$signPackage.appId} #39;, timestamp: #39;{$signPackage.timestamp} #39;, nonceStr: #39;{$signPackage.nonceStr} #39;, signature: #39;{$signPackage.signature} #39;, jsApiList: [ // 所有要调用的 API 都要加到这个列表中 #39;checkJsApi #39;, #39;openLocation #39;, #39;getLocation #39;, #39;scanQRCode #39; wx.ready(function () { $( #39;#scan #39;).click(function(){ wx.scanQRCode({ needResult: 0, wx.checkJsApi({ jsApiList: [ #39;getLocation #39; success: function (res) { if (res.checkResult.getLocation == false) alert( #39;你的微信版本太低,不支持微信JS接口,请升级到最新的微信版本! #39;); return; wx.getLocation({ success: function (res) { var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90 var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。 var geoconv = #39;http://api.map.baidu.com/geoconv/v1/?callback=coordinateTransformation coords= #39; + longitude + #39;, #39; + latitude + #39; from=1 to=5 ak=5BFNbSgnVF5g2O72NpvTDxFm #39;; var script = document.createElement( #39;script #39;); script.src = geoconv; document.head.appendChild(script); cancel: function (res) { alert( #39;用户拒绝授权获取地理位置 #39;); function coordinateTransformation(data) var LATLNG = data.result[0].y + #39;, #39; + data.result[0].x; var url = #39;http://api.map.baidu.com/geocoder/v2/?callback=getCurrentLocation ak=5BFNbSgnVF5g2O72NpvTDxFm location= #39; + LATLNG + #39; output=json pois=1 #39;; var script = document.createElement( #39;script #39;); 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 = #39;street= #39; + address + #39; name= #39; + street + #39; lng= #39; + x + #39; lat= #39; + y + #39; city= #39; + city; var url = {:U( #39;Index/savePosition #39;)} $.getJSON(url,{ #39;name #39;:street, #39;lng #39;:x, #39;lat #39;: y, #39;city #39;:city},function(data) if(data.returnCode) { } /script 其次,在控制器中接收ajax传递的地理坐标,然后保存到session中。 public function savePosition() { $city = II( #39;get.city #39;, #39; #39;, #39;trim #39;); $addr = II( #39;get.name #39;, #39; #39;, #39;trim #39;); $lng = II( #39;get.lng #39;, #39; #39;, #39;trim #39;); $lat = II( #39;get.lat #39;, #39; #39;, #39;trim #39;); $myLocation = array( #39;city #39; = $city, #39;addr #39; = $addr, #39;lng #39; = $lng, #39;lat #39; = $lat, ); $_SESSION[ #39;MyLocation #39;] = $myLocation; $data[ #39;returnCode #39;] = 1; $data[ #39;returnInfo #39;] = #39;获取位置成功! #39;; $this- ajaxReturn($data); return; }
注:用的是thinkphp框架,II是自定义的方法,获取get或post传递的值,和 I 函数一样。
以上就是如何通过微信获取当前地理位置并将其存到session中的详细内容,更多请关注php中文网其它相关文章!
微信app下载
微信是一款手机通信软件,支持通过手机网络发送语音短信、视频、图片和文字。微信可以单聊及群聊,还能根据地理位置找到附近的人,带给大家全新的移动沟通体验,有需要的小伙伴快来保存下载体验吧!