<?php //php插件下載地址: https://files.cnblogs.com/files/fan-bk/jssdk_php.rar //建立一個php文件 require_once "jssdk_php/jssdk.php"; //引入下載的PHP插件 $jssdk = new JSSDK("這里填寫appid","這里填寫密匙");//填寫公眾號 密匙 $signPackage = $jssdk->GetSignPackage(); ?> <!DOCTYPE html> <html> <body> <script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> <script> wx.config({ debug: false, //調式模式,設置為ture后會直接在網頁上彈出調試信息,用於排查問題 appId: '<?php echo $signPackage["appId"];?>', timestamp: <?php echo $signPackage["timestamp"];?>, nonceStr: '<?php echo $signPackage["nonceStr"];?>', signature: '<?php echo $signPackage["signature"];?>', jsApiList: [ // 所有要調用的 API 都要加到這個列表中 'checkJsApi', 'openLocation', 'getLocation' ] }); wx.ready(function () { wx.checkJsApi({ jsApiList: [ 'getLocation' ], success: function (res) { // alert(JSON.stringify(res)); // alert(JSON.stringify(res.checkResult.getLocation)); 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 speed = res.speed; // 速度,以米/每秒計 var accuracy = res.accuracy; // 位置精度 alert(latitude+','+longitude+','+accuracy); }, cancel: function (res) { alert('用戶拒絕授權獲取地理位置'); } }); }); wx.error(function (res) { //alert(res.errMsg); //打印錯誤消息。及把 debug:false,設置為debug:ture就可以直接在網頁上看到彈出的錯誤提示 }); </script> </body> </html>