博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vue 地理位置定位_HTML5地理位置
阅读量:1531 次
发布时间:2019-04-21

本文共 2719 字,大约阅读时间需要 9 分钟。

HTML5 Geolocation API可让您与自己喜爱的网站分享您的位置。Javascript可以捕获您的纬度和经度,并且可以发送到后端Web服务器,并且可以在地图上查找本地业务或显示您的位置,并进行喜欢的位置感知。

Today most of the browsers and mobile devices support Geolocation API. The geolocation APIs work with a new property of the global navigator object ie. Geolocation object which can be created as follows:

var geolocation = navigator.geolocation;

The geolocation object is a service object that allows widgets to retrieve information about the geographic location of the device.

Geolocation Methods

The geolocation object provides the following methods −

Method

Description

This method retrieves the current geographic location of the user.

This method retrieves periodic updates about the current geographic location of the device.

This method cancels an ongoing watchPosition call.

Example

Following is a sample code to use any of the above method −

function getLocation() {

var geolocation = navigator.geolocation;

geolocation.getCurrentPosition(showLocation, errorHandler);

}

这里showLocation和errorHandler是回调方法,将用于获取实际位置,如下一节所述,并处理错误(如果有的话)。

位置属性

地理位置方法getCurrentPosition()和getPositionUsingMethodName()指定检索位置信息的回调方法。这些方法与存储完整位置信息的对象Position异步地调用。

“位置”对象指定设备的当前地理位置。该位置表示为一组地理坐标以及关于航向和速度的信息。

下表描述了Position对象的属性。对于可选属性,如果系统无法提供值,则该属性的值将设置为null。

属性

类型

描述

协调

对象

指定设备的地理位置。该位置表示为一组地理坐标以及关于航向和速度的信息。

coords.latitude

以十进制度指定纬度估计。值范围为[-90.00,+90.00]。

协调一致

指定十进制度的经度估计。值范围是[-180.00,+180.00]。

协调

[可选]指定高于WGS 84椭球的米高。

coords.accuracy

[可选]以米为单位指定纬度和经度估计的准确度。

coords.altitude精确度

[可选]以米为单位指定高度估计的准确度。

协调

[可选]指定设备当前的运动方向,以相对于真北方向顺时针方向计数。

coords.speed

[可选]指定设备的当前地面速度,单位为米/秒。

时间戳

date

指定检索位置信息和创建Position对象的时间。

以下是使用Position对象的示例代码。这里showLocation方法是一个回调方法 -

function showLocation( position ) {

var latitude = position.coords.latitude;

var longitude = position.coords.longitude;

...

}

处理错误

地理位置复杂,非常需要捕获任何错误并妥善处理。

地理位置方法getCurrentPosition()和watchPosition()使用给出PositionError对象的错误处理程序回调方法。此对象具有以下两个属性 -

属性

类型

描述

包含错误的数字代码。

信息

string

包含错误的可读描述。

下表描述了在PositionError对象中返回的可能的错误代码。

不变

描述

0

未知错误

该方法由于未知错误而无法检索设备的位置。

1

没有权限

该方法无法检索设备的位置,因为应用程序没有使用位置服务的权限。

2

POSITION_UNAVAILABLE

设备的位置无法确定。

3

时间到

该方法无法在指定的最大超时间隔内检索位置信息。

以下是使用PositionError对象的示例代码。这里的errorHandler方法是一种回调方法 -

functionerrorHandler(err){if(err.code==1){// access is denied}...}

位置选项

以下是getCurrentPosition()方法的实际语法 -

getCurrentPosition(callback, ErrorCallback, options)

这里第三个参数是PositionOptions对象,它指定一组用于检索设备地理位置的选项。

以下是可以指定为第三个参数的选项 -

属性

类型

描述

enableHighAccuracy

boolean

指定窗口小部件是否想要接收最准确的位置估计。默认情况下是false。

时间到

timeout属性是您的Web应用程序愿意等待一个位置的毫秒数。

最大值

指定缓存位置信息的到期时间(以毫秒为单位)。

以下是一个示例代码,显示如何使用上述方法 -

function getLocation() {

var geolocation = navigator.geolocation;

geolocation.getCurrentPosition(showLocation, errorHandler, {maximumAge: 75000});

}

转载地址:http://xbzdy.baihongyu.com/

你可能感兴趣的文章
HDU 3336 Count the string【KMP的next数组性质】
查看>>
洛谷 P1196 [NOI2002]银河英雄传说【带权并查集】
查看>>
HDU 4825 Xor Sum【01字典树/贪心】(两数最大/最小异或和)
查看>>
洛谷 P4551 最长异或路径【01字典树/贪心】
查看>>
LeetCode C++ 622. Design Circular Queue【设计/循环队列】中等
查看>>
LeetCode 921. 使括号有效的最少添加(栈)
查看>>
LeetCode 1018. 可被 5 整除的二进制前缀
查看>>
LeetCode 961. 重复 N 次的元素
查看>>
LeetCode 925. 长按键入(双指针)
查看>>
LeetCode 1309. 解码字母到整数映射
查看>>
动态规划应用--最长递增子序列 LeetCode 300
查看>>
LeetCode 53. 最大子序和(动态规划)
查看>>
图Graph--拓扑排序(Topological Sorting)
查看>>
图Graph--最短路径算法(Shortest Path Algorithm)
查看>>
LeetCode 674. 最长连续递增序列
查看>>
LeetCode 70. 爬楼梯(动态规划)
查看>>
数据结构--位图 BitMap
查看>>
朴素贝叶斯算法--过滤垃圾短信
查看>>
向量空间 Vector Space -- 推荐系统
查看>>
B+树 -- MySQL数据库索引
查看>>