通过HTML5 网页代码获取设备Sensor值

HTML5 Sensors
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Web sensor</title>
</head>
<body>
<div id="error"></div>
<div id="light"></div>
<div id="locate"></div>
<div id="gsensor"></div>
<div id="gyro"></div>
<button οnclick="getLightSensor()">光感测试</button><br>
<button οnclick="getGsensor()">重力感应测试</button><br>
<button οnclick="getLocation()">WiFi定位</button>
<button οnclick="getGyro()">Gryo</button>
<button οnclick="getCompassClb()">CompassClb</button>

<script>
function getLightSensor() {
	var x=document.getElementById("light");
	var e=document.getElementById('error');
	window.addEventListener('devicelight', function(event) {
		x.innerHTML = 'Lux:' + Math.round(event.value) + 'lux';
	},true);
}

function getGsensor(){
	var x=document.getElementById("gsensor");
	window.addEventListener("devicemotion", function(event) {
		var eventacceleration = event.acceleration;
		x.innerHTML = "acceleration:<br>"+
		eventacceleration.x+"<br>"+
		eventacceleration.y+"<br>"+
		eventacceleration.z
	}, true);
}

function getGyro(){
	var x=document.getElementById("gyro");
	window.addEventListener("deviceorientation", function(event) {
		x.innerHTML = 'Gyro:<br>'
		event.alpha+'<br>'+
		event.beta+"<br>"+
		event.gamma+'<br>';
	}, true);
}

function getCompassClb(){
	window.addEventListener("compassneedscalibration", function(event) {
		alert('You need to calibrate you Compass');
		event.preventDefault();
	}, true);
}
</script>
<script>
	var x=document.getElementById("locate");
	function getLocation()
	{
		if (navigator.geolocation)
		{
			navigator.geolocation.watchPosition(showPosition);
		}
		else{x.innerHTML="Geolocation is not supported by this browser.";}
	}
	
	function showPosition(position)
	{
		x.innerHTML="Latitude: " + position.coords.latitude + "<br />Longitude: " + position.coords.longitude;
	}
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值