|
導(dǎo)讀微信(WeChat)是騰訊公司于2011年1月21日推出的一個(gè)為智能終端提供即時(shí)通訊服務(wù)的免費(fèi)應(yīng)用程序,由張小龍所帶領(lǐng)的騰訊廣州研發(fā)中心產(chǎn)品團(tuán)隊(duì)打造 [2] 。微信支持跨通信運(yùn)營(yíng)商、跨操作系統(tǒng)平臺(tái)... 微信(WeChat)是騰訊公司于2011年1月21日推出的一個(gè)為智能終端提供即時(shí)通訊服務(wù)的免費(fèi)應(yīng)用程序,由張小龍所帶領(lǐng)的騰訊廣州研發(fā)中心產(chǎn)品團(tuán)隊(duì)打造 [2] 。微信支持跨通信運(yùn)營(yíng)商、跨操作系統(tǒng)平臺(tái)通過網(wǎng)絡(luò)快速發(fā)送免費(fèi)(需消耗少量網(wǎng)絡(luò)流量)語音短信、視頻、圖片和文字,同時(shí),也可以使用通過共享流媒體內(nèi)容的資料和基于位置的社交插件“搖一搖”、“漂流瓶”、“朋友圈”、”公眾平臺(tái)“、”語音記事本“等服務(wù)插件。 首先,在靜態(tài)頁面中,添加微信的配置文件,通過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 都要加到這個(gè)列表中
'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接口,請(qǐng)升級(jí)到最新的微信版本!');
return;
}
}
});
wx.getLocation({
success: function (res) {
var latitude = res.latitude; // 緯度,浮點(diǎn)數(shù),范圍為90 ~ -90
var longitude = res.longitude; // 經(jīng)度,浮點(diǎn)數(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;
}注:用的是thinkphp框架,II是自定義的方法,獲取get或post傳遞的值,和 I 函數(shù)一樣。 以上就是如何通過微信獲取當(dāng)前地理位置并將其存到session中的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章! 微信提供公眾平臺(tái)、朋友圈、消息推送等功能,用戶可以通過“搖一搖”、“搜索號(hào)碼”、“附近的人”、掃二維碼方式添加好友和關(guān)注公眾平臺(tái),同時(shí)微信將內(nèi)容分享給好友以及將用戶看到的精彩內(nèi)容分享到微信朋友圈。 |
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!