SlideShare uma empresa Scribd logo
1 de 14
HTML5 开发
Web,MobileWeb & Apps
范圣刚
安博中程在线
Geolocation
定位和跟踪
介绍
安博中程在线
Geolocation工作原理
Geolocation API
Example
扩展阅读
·
·
getCurrentPosition方法
Geolocation数据
Handling Errors
PositionOptions对象
watchPosition()方法和clearWatch()方法
-
-
-
-
-
·
·
3/14
Geolocation工作原理
识别地理位置的主要方法:
安博中程在线
通过IP地址
利用基站获取手机网络的接入位置
通过利用卫星定位获得经纬度信息的GPS设备
·
·
·
4/14
Geolocation API
可以获得用户所在地理位置的经纬度信息
调用Geolocation API从全局navigator对象的geolocation属性开始:
navigator.geolocation
安博中程在线
<scripttype='text/javascript'>
//最简单的使用方式:没有做任何的检测,错误处理,也没有选项参数
functionfind_my_location(){
navigator.geolocation.getCurrentPosition(show_map);
}
</script>
JAVASCRIPT
5/14
Geolocation API
检测浏览器是否支持地理位置API
方法1: 检测检测 全局对象navigator是否具有geolocation属性
方法2: 使用Modernizr提供的方法 检测检测
安博中程在线
<script type='text/javascript'>
function supports_geolocation() {
return !!navigator.geolocation;
}
</script>
JAVASCRIPT
<script type='text/javascript'>
function find_my_location() {
if (Modernizr.geolocation) {
navigator.geolocation.getCurrentPosition(show_map);
} else {
// 浏览器没有提供原生支持,使用回退方案
}
}
</script>
JAVASCRIPT
6/14
Geolocation API
getCurrentPosition()
使用getCurrentPosition()方法 获取当前位置信息获取当前位置信息 .
安博中程在线
<script type='text/javascript'>
function find_my_location() {
if (Modernizr.geolocation) {
navigator.geolocation.getCurrentPosition(show_map);
} else {
// 其他方案
}
}
function show_map(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
alert('Lat: ' + latitude + ' Lon: ' + longitude);
}
</script>
JAVASCRIPT
7/14
Position数据
成功的回调函数的参数是一个Position对象,包括coords和timestamp两个属性。
属性 类型 备注
coords.latitude double 纬度(度)
coords.longitude double 经度(度)
coords.accuracy double 精度(米)
coords.altitude double或null 海拔(米)
coords.altitudeAccuracy double或null 海拔精度(米)
coords.heading double或null 度(顺时针,以正北为基准)
coords.speed double或null 米/秒
timestamp DOMTimeStamp 可以转成Date对象
安博中程在线 8/14
Handling Errors
getCurrentPosition()的第二个参数(可选)——容错处理的回调函数:
在获取地理位置过程中有任何错误,都会调用该回调函数,并且会传入一个PositionError对象作为参数。
安博中程在线
<script type='text/javascript'>
function find_my_location() {
if (Modernizr.geolocation) {
navigator.geolocation.getCurrentPosition(show_map, handle_error);
} else {
// 其他方案
}
}
function handle_error(err) {
if (err.code == 1) {
// 用户说不!
}
}
</script>
JAVASCRIPT
9/14
PositionError对象
属性 类型 备注
code short 可枚举
message DOMString 与终端用户无关
其中code属性具有以下属性值:
安博中程在线
PERMISSION_DENIED(1): 用户不同意被获取位置信息。
POSITION_UNAVAILABLE(2): 网络不可用或者无法连接到获取位置信息的卫星。
TIMEOUT(3): 网络可用但是花了太长时间的计算用户的位置上。
UNKNOWN_ERROR(0): 发生其他未知错误。
·
·
·
·
10/14
PositionOptions对象
两种定位方式和缓存的位置信息
属性 类型 默认值 备注
enableHighAccuracy boolean false 设成true可能会使得获取位置的速度变慢
timeout long 没有默认值 单位:毫秒
maximumAge long 0 单位:毫秒
安博中程在线 11/14
Example
获得经纬度信息并在地图上显示
显示位置显示位置
安博中程在线
navigator.geolocation.getCurrentPosition(function(position) {
var latLng = new google.maps.LatLng(
position.coords.latitude, position.coords.longitude);
var marker = new google.maps.Marker({position: latLng, map: map});
map.setCenter(latLng);
}
JAVASCRIPT
12/14
扩展阅读
安博中程在线
W3C地理位置 API
geo.js, 地理位置的JS Framework。依照W3C地理位置 API规范封装了底层系统特定的实现。
·
·
13/14
<Thank you!>
微博 @范圣刚
博客 www.tfan.org
github github.com/princetoad

Mais conteúdo relacionado

Destaque

Webstorage
WebstorageWebstorage
WebstorageTom Fan
 
Intro to-html5
Intro to-html5Intro to-html5
Intro to-html5Tom Fan
 
PhoneGap 通信原理和插件系统
PhoneGap 通信原理和插件系统PhoneGap 通信原理和插件系统
PhoneGap 通信原理和插件系统Tom Fan
 
AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状Tom Fan
 
Podularityftw 130528020402-phpapp02
Podularityftw 130528020402-phpapp02Podularityftw 130528020402-phpapp02
Podularityftw 130528020402-phpapp02Wooga
 
Portfolio Performance: GE Global Research Cracks the Code with Clarizen
Portfolio Performance: GE Global Research Cracks the Code with ClarizenPortfolio Performance: GE Global Research Cracks the Code with Clarizen
Portfolio Performance: GE Global Research Cracks the Code with ClarizenClarizen
 
Searching For New Direct Response Marketing Channels
Searching For New Direct Response Marketing ChannelsSearching For New Direct Response Marketing Channels
Searching For New Direct Response Marketing ChannelsChris Zaharias
 
Trasparenze interpretazione comunicazione
Trasparenze interpretazione comunicazioneTrasparenze interpretazione comunicazione
Trasparenze interpretazione comunicazioneGiuseppe Cistaro
 
The top 20 luxurious & expensive hotels in the world
The top 20 luxurious & expensive hotels in the worldThe top 20 luxurious & expensive hotels in the world
The top 20 luxurious & expensive hotels in the worldAlexLee01
 
Cartner on Environmental Heat Stress
Cartner on Environmental Heat StressCartner on Environmental Heat Stress
Cartner on Environmental Heat StressSMACC Conference
 
Mary McCaskill: Neonatal Nightmares
Mary McCaskill: Neonatal NightmaresMary McCaskill: Neonatal Nightmares
Mary McCaskill: Neonatal NightmaresSMACC Conference
 
Baby Gadgets
Baby GadgetsBaby Gadgets
Baby GadgetsMihex
 

Destaque (18)

Webstorage
WebstorageWebstorage
Webstorage
 
Css3
Css3Css3
Css3
 
Intro to-html5
Intro to-html5Intro to-html5
Intro to-html5
 
PhoneGap 通信原理和插件系统
PhoneGap 通信原理和插件系统PhoneGap 通信原理和插件系统
PhoneGap 通信原理和插件系统
 
AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状
 
Podularityftw 130528020402-phpapp02
Podularityftw 130528020402-phpapp02Podularityftw 130528020402-phpapp02
Podularityftw 130528020402-phpapp02
 
Portfolio Performance: GE Global Research Cracks the Code with Clarizen
Portfolio Performance: GE Global Research Cracks the Code with ClarizenPortfolio Performance: GE Global Research Cracks the Code with Clarizen
Portfolio Performance: GE Global Research Cracks the Code with Clarizen
 
Searching For New Direct Response Marketing Channels
Searching For New Direct Response Marketing ChannelsSearching For New Direct Response Marketing Channels
Searching For New Direct Response Marketing Channels
 
Trasparenze interpretazione comunicazione
Trasparenze interpretazione comunicazioneTrasparenze interpretazione comunicazione
Trasparenze interpretazione comunicazione
 
The top 20 luxurious & expensive hotels in the world
The top 20 luxurious & expensive hotels in the worldThe top 20 luxurious & expensive hotels in the world
The top 20 luxurious & expensive hotels in the world
 
Cartner on Environmental Heat Stress
Cartner on Environmental Heat StressCartner on Environmental Heat Stress
Cartner on Environmental Heat Stress
 
Paeds plenary-mc caskill
Paeds plenary-mc caskillPaeds plenary-mc caskill
Paeds plenary-mc caskill
 
Mary McCaskill: Neonatal Nightmares
Mary McCaskill: Neonatal NightmaresMary McCaskill: Neonatal Nightmares
Mary McCaskill: Neonatal Nightmares
 
Atention
AtentionAtention
Atention
 
Baby Gadgets
Baby GadgetsBaby Gadgets
Baby Gadgets
 
Deep dive
Deep diveDeep dive
Deep dive
 
Incite: Marketing
Incite: Marketing Incite: Marketing
Incite: Marketing
 
Incite: Communications
Incite: CommunicationsIncite: Communications
Incite: Communications
 

Mais de Tom Fan

Html5 最重要的部分
Html5 最重要的部分Html5 最重要的部分
Html5 最重要的部分Tom Fan
 
PhoneGap 2.0 开发
PhoneGap 2.0 开发PhoneGap 2.0 开发
PhoneGap 2.0 开发Tom Fan
 
Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发Tom Fan
 
HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型Tom Fan
 
18 NSUserDefaults
18 NSUserDefaults18 NSUserDefaults
18 NSUserDefaultsTom Fan
 
17 Localization
17 Localization17 Localization
17 LocalizationTom Fan
 
16 CoreData
16 CoreData16 CoreData
16 CoreDataTom Fan
 
15 Subclassing UITableViewCell
15 Subclassing UITableViewCell15 Subclassing UITableViewCell
15 Subclassing UITableViewCellTom Fan
 
14 Saving Loading and Application States
14 Saving Loading and Application States14 Saving Loading and Application States
14 Saving Loading and Application StatesTom Fan
 
13 UIPopoverController and Modal View Controller
13 UIPopoverController and Modal View Controller13 UIPopoverController and Modal View Controller
13 UIPopoverController and Modal View ControllerTom Fan
 
12 Camera
12 Camera12 Camera
12 CameraTom Fan
 
11 UINavigationController
11 UINavigationController11 UINavigationController
11 UINavigationControllerTom Fan
 
10 Editing UITableView
10 Editing UITableView10 Editing UITableView
10 Editing UITableViewTom Fan
 
09 UITableView and UITableViewController
09 UITableView and UITableViewController09 UITableView and UITableViewController
09 UITableView and UITableViewControllerTom Fan
 
08 Notification and Rotation
08 Notification and Rotation08 Notification and Rotation
08 Notification and RotationTom Fan
 
07 View Controllers
07 View Controllers07 View Controllers
07 View ControllersTom Fan
 
06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollView06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollViewTom Fan
 

Mais de Tom Fan (17)

Html5 最重要的部分
Html5 最重要的部分Html5 最重要的部分
Html5 最重要的部分
 
PhoneGap 2.0 开发
PhoneGap 2.0 开发PhoneGap 2.0 开发
PhoneGap 2.0 开发
 
Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发
 
HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型
 
18 NSUserDefaults
18 NSUserDefaults18 NSUserDefaults
18 NSUserDefaults
 
17 Localization
17 Localization17 Localization
17 Localization
 
16 CoreData
16 CoreData16 CoreData
16 CoreData
 
15 Subclassing UITableViewCell
15 Subclassing UITableViewCell15 Subclassing UITableViewCell
15 Subclassing UITableViewCell
 
14 Saving Loading and Application States
14 Saving Loading and Application States14 Saving Loading and Application States
14 Saving Loading and Application States
 
13 UIPopoverController and Modal View Controller
13 UIPopoverController and Modal View Controller13 UIPopoverController and Modal View Controller
13 UIPopoverController and Modal View Controller
 
12 Camera
12 Camera12 Camera
12 Camera
 
11 UINavigationController
11 UINavigationController11 UINavigationController
11 UINavigationController
 
10 Editing UITableView
10 Editing UITableView10 Editing UITableView
10 Editing UITableView
 
09 UITableView and UITableViewController
09 UITableView and UITableViewController09 UITableView and UITableViewController
09 UITableView and UITableViewController
 
08 Notification and Rotation
08 Notification and Rotation08 Notification and Rotation
08 Notification and Rotation
 
07 View Controllers
07 View Controllers07 View Controllers
07 View Controllers
 
06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollView06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollView
 

Geolocation