HTML5 new features mobile device API
为了更好地为移动设备服务,HTML5推出了一系列针对移动设备的API。
1、Geolocation API
Geolocation接口用于获取用户的地理位置。它使用的方法基于GPS或者其他机制(比如IP地址、WIFI热点等)。
下面的方法,可以检查浏览器是否支持这个接口。
if (navigator.geolocation) { // 支持} else { //不支持}
1.1 getCurrentPosition方法
getCurrentPosition方法,用来获取用户的地理位置。使用它需要得到用户的授权,浏览器会跳出一个对话框,询问用户是否许可当前页面获取他的地理位置。必须考虑两种情况的回调函数:一种是同意授权,另一种是拒绝授权。如果用户拒绝授权会抛出一个错误。
navigator.geolocation.getCurrentPosition(geoSuccess, geoError);
上面代码指定了处理当前地址位置的两个回调函数。
(1)同意授权
如果用户同意授权,就会调用geoSuccess。
function geoSuccess(event) { var coords = event.coords; console.log('latitude: ' + coords.latitude); //纬度 console.log('longitude: ' + coords.longitude); //经度 console.log('accuracy: ' + coords.accuracy); //精度 console.log('altitude: ' + coords.altitude); //海拔 console.log('altitudeAccuracy: ' + coords.altitudeAccuracy); //海拔精度(单位:米) console.log('heading: ' + coords.heading); //以360度表示的方向 console.log('speed: ' + coords.speed); //每秒的速度(单位:米)}
geoSuccess的参数是一个event对象。event.coords属性指向一个对象,包含了用户的位置信息,主要是以下几个值:
coords.latitude:纬度
coords.longitude:经度
coords.accuracy:精度
coords.altitude:海拔
coords.altitudeAccuracy:海拔精度(单位:米)
coords.heading:以360度表示的方向
coords.speed:每秒的速度(单位:米)
(2)拒绝授权
如果用户拒绝授权,就会调用geoError。
function geoError(event) { console.log('Error code ' + event.code + '. ' + event.message); }
geoError的参数也是一个event对象。event.code属性表示错误类型,有四个值:
0:未知错误,浏览器没有提示出错的原因,相当于常量event.UNKNOWN_ERROR。
1:用户拒绝授权,相当于常量event.PERMISSION_DENIED
2:没有得到位置,GPS或其他定位机制无法定位,相当于常量event.POSITION_UNAVAILABLE。
3:超时,GPS没有在指定时间内返回结果,相当于常量event.TIMEOUT。
event.message为错误提示信息。
(3)设置定位行为
getCurrentPosition方法还可以接受一个对象作为第三个参数,用来设置定位行为。
var option = { enableHighAccuracy: true, timeout: Infinity, maximumAge: 0}; navigator.geolocation.getCurrentPosition(geoSuccess, geoError, option);
这个参数对象有三个成员:
enableHighAccuracy:如果设为true,就要求客户端提供更精确的位置信息,这会导致更长的定位时间和更大的耗电,默认设置为false。
timeout:等待客户端做出回应的最大毫秒数,默认值为Infinity(无限)。
maxinumAge:客户端可以使用缓存数据的最大毫秒数。如果设为0,客户端不读取缓存;如果设为infinity,客户端只读取缓存。
1.2 watchPosition方法和clearWatch方法
watchPosition方法可以用来监听用户位置的持续改变,使用方法与getCurrentPosition方法一样。
var watchID = navigator.geolocation.watchPosition(geoSuccess, geoError);
一旦用户位置发生改变,就会调用回调函数。
如果要取消监听,则使用clearWatch方法。
navigator.geolocation.clearWatch(watchID);
2、Vibration API
Vibration接口用于在浏览器中发出命令,使得设备振动。由于该操作很耗电,在低电量时最好取消该操作。
使用下面的代码检查该接口是否可用。目前,只有Chrome和Firefox的Android平台最新版本支持它。
navigator.vibrate = navigator.vibrate || navigator.webkitVibrate || navigator.mozVibrate || navigator.msVibrate;if (navigator.vibrate) { // 支持}
vibrate方法可以使得设备振动,它的参数就是振动持续的毫秒数。
navigator.vibrate(1000);
上面的代码使得设备振动1秒钟。
vibrate方法还可以接受一个数组作为参数,表示振动的模式。偶数位置的数组成员表示振动的毫秒数,奇数位置的数组成员表示等待的毫秒数。
navigator.vibrate([500, 300, 500]);
上面代码表示,设备先振动500毫秒,然后等待300毫秒,再接着振动500毫秒。
vibrate是一个非阻塞式的操作,即手机振动的同时,JavaScript代码继续向下运行。要停止振动,只有将0毫秒传入vibrate方法。
navigator.vibrate(0);
3、亮度调节
当移动设备的亮度传感器,感知外部亮度发生显著变化时,会触发devicelight事件。目前,只有Firefox布署了这个API。
window.addEventListener('devicelight', function(event) { console.log(event.value + 'lux'); })
下面代码表示,devicelight事件的回调函数,接受一个事件对象作为参数。该对象的value属性就是亮度值。
这种API的一种应用是,我们可以针对亮度的强弱来改网页背景和文字颜色。
The above is the detailed content of HTML5 new features mobile device API. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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)

Hot Topics











Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.
