geolocation
HTML is not merely a set of tags: it also encompasses dozens of new JavaScript “hooks”, broadly referred to as APIs. These provide easy methods for developers to gain information and deliver services that were previously impossible on web pages.
HTML不仅是一组标签:它还包含数十个新JavaScript“挂钩”,广泛地称为API 。 这些为开发人员提供了简便的方法来获取信息并提供以前在网页上无法实现的服务。
While they are strongly associated with HTML5, strictly speaking many of the new APIs are not part of “HTML5” at all, as they do not require or directly interact with any of the elements in the spec. Most could be applied just as easily to an XHTML page: the programming only requires a supportive browser and/or device in order to work. For technical accuracy, I’ll refer to these APIs as part of HTML, rather than HTML5.
尽管它们与HTML5紧密相关,但严格来讲,许多新API根本不是“ HTML5”的一部分,因为它们不需要或不与规范中的任何元素直接交互。 大多数功能都可以轻松地应用于XHTML页面:编程仅需要支持的浏览器和/或设备才能工作。 为了提高技术准确性,我将这些API称为HTML的一部分,而不是HTML5。
我在哪里? (Where Am I?)
The Geolocation, Orientation and Acceleration APIs seek to answer three basic questions of a device:
地理位置,方向和加速API旨在回答设备的三个基本问题:
- Where is the device located? 设备在哪里?
- Is the device moving in a particular direction? 设备是否朝特定方向移动?
- What angle is it being held at? 它保持在什么角度?
Geolocation covers the first question, with each API tackling different aspects of the second and third. It’s worthwhile taking a look at the limitations of each API to avoid confusion between them, and to ensure you use the right one for your project.
地理位置涵盖了第一个问题,每个API都针对第二个和第三个问题。 值得一看的是每种API的局限性,以避免它们之间的混淆,并确保为您的项目使用正确的局限性。
-
Orientation & Acceleration depends on hardware
方向和加速度取决于硬件
-
Both Orientation and Acceleration rely on the presence of a miniature gyroscope, accelerometer and compass that are physically built into a device. Most modern smartphones and tablets support such features, but they are rarely found in laptops, and not at all in desktop computers. The exclusion of the latter makes sense: it’s unlikely that you’ll be waving a 27″ iMac through the air anytime soon to play a game or interact with a site. However, the miniature devices may be part of a mouse or other input that are used with a desktop computer.
方向和加速度都依赖于设备中内置的微型陀螺仪,加速度计和指南针。 大多数现代智能手机和平板电脑都支持此类功能,但在笔记本电脑中很少见,而台式机却根本不见。 排除后者是有道理的:您不太可能会很快在空中挥舞27英寸的iMac来玩游戏或与网站互动。 然而,微型设备可以是与台式计算机所使用的鼠标或其它输入的一部分。
Geolocation is permission-based
地理位置基于权限
-
To safeguard privacy, the Geolocation API asks for the user’s permission before sharing their location with a requesting service; Orientation and Acceleration do not, and can be called on at any time in a web page without informing the user. Geolocation permission must be granted once per session (i.e. when visiting a website), after that, the device’s location might be written to a database 为了保护隐私,Geolocation API在与请求服务共享用户位置之前会先征得用户的同意; 方向和加速度不是,并且可以随时在网页中调用而无需通知用户。 必须在每个会话中(例如,在访问网站时)授予一次地理位置许可,之后,设备的位置可能会写入数据库
Geolocation does not require GPS hardware
地理位置不需要GPS硬件
- While satellites might be used to pinpoint the location of a mobile phone in remote areas, the Geolocation API has a suite of methods to determine where a device is. No GPS chip is required, and the API will work equally well on desktop computers.
- 虽然可能会使用卫星来查明移动电话在偏远地区的位置,但是Geolocation API具有一套确定设备位置的方法。 无需GPS芯片,该API在台式计算机上同样可以正常运行。 Geolocation provides latitude and longitude, not locale 地理位置提供纬度和经度,而不是区域设置
-
- The device’s latitude and longitude 设备的纬度和经度
- The device’s altitude (meters above or below a projected ellipsoid of the earth’s surface). 设备的高度(在地面投影椭圆体上方或下方的米)。
- Accuracy ratings for these values. 这些值的精度等级。
- The direction the device is heading in (in degrees), and its speed (in meters per second). 设备的前进方向(以度为单位)及其速度(以米/秒为单位)。
Note that this does not include street names, city, or country. To get that information, you will need to reverse-engineer it from the supplied latitude and longitude, usually with a reverse geocoding service.
请注意,这不包括街道名称,城市或国家。 要获取该信息,您通常需要使用反向地理编码服务从提供的纬度和经度进行反向工程。
Acceleration, Orientation and Geolocation have differing frames of reference, accuracy and support
加速度,方向和地理位置具有不同的参考框架,准确性和支持
-
The orientation of a device is measured relative to the earth’s surface: if the device laid flat on a table pointing north, its offset angles (referred to as 设备相对于地球表面的方向进行了测量:如果设备平放在指向北方的桌子上,则其偏移角度(称为
The tiny gyroscopes, accelerometers and compasses in modern mobile devices are relatively “noisy”. The information they provide is acceptable within a certain range of error, but trying to integrate the data to provide position or velocity can result in wildly imaccurate values. For most non-gaming applications, I would suggest using the GeoLocation API to gain position or velocity information.
现代移动设备中的微型陀螺仪,加速度计和指南针相对“嘈杂”。 它们提供的信息在一定的误差范围内是可以接受的,但是尝试对数据进行积分以提供位置或速度可能会导致非常不准确的值。 对于大多数非游戏应用程序,我建议使用GeoLocation API来获取位置或速度信息。
Geolocation has great support in all current browsers (IE support goes back to version 9) but acceleration and orientation are trickier: support is good in most mobile browsers (iOS, Android, etc.), but poor to non-existent in the desktop version of the same (no support in IE or Safari, for example). These gaps are understandable, if a little frustrating if you’re after consistency.
地理定位在当前所有浏览器中都具有强大的支持(IE支持可追溯到版本9),但加速和方向性比较棘手:大多数移动浏览器(iOS,Android等)都支持良好,而桌面版本的支持则差强人意相同(例如,不支持IE或Safari)。 这些差距是可以理解的,如果您对一致性感到有些沮丧的话。
Don’t forget Media Queries
不要忘记媒体查询
-
@media screen and (orientation:portrait) { /* CSS for portrait mode of devices */ } @media screen and (orientation:landscape) { /* CSS for landscape mode of devices */ }
Taking all these distinctions of the Geolocation, Orientation and Acceleration APIs into account is very important when choosing the most appropriate technology for your next project. Now that we’ve addressed the basics, we can move on to implementation, which I will cover in future articles.
在为下一个项目选择最合适的技术时,考虑地理位置,方向和加速API的所有这些区别非常重要。 现在我们已经解决了基础知识,我们可以继续进行实现,我将在以后的文章中介绍。
翻译自: https://thenewcode.com/701/Introducing-HTML-GeoLocation-DeviceOrientation-and-Acceleration
geolocation