GeolocateControl
定义于: src/ui/control/geolocate_control.ts:184
GeolocateControl
控件提供了一个按钮,当点击该按钮时,使用设备的地理定位API来确定用户的位置;如果成功,该地图将设置fitBoundsOptions
选项中指定的缩放级别,或者如果未指定,则将通过创建一个周围的accuracy
圆来确定缩放级别;如果位于活动状态下,控件将监听geolocation API监视事件并更新用户的位置在地图上;为了有效使用该控件,您必须首先检查浏览器是否支持地理定位,并且需要获得用户的许可;程序化控制用户的位置在控制中是通过Geolocation.getCurrentPosition()
和Geolocation.watchPosition()
方法实现的;地理定位控件有两种状态:1)活动:地图能够接收位置更新,2)无效:位置信息不可用;请参阅状态图;
状态图
示例
// 使用高精度定位并实时跟踪用户位置的地理定位控件的初始化
map.addControl(new GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
trackUserLocation: true
}));
参见
Geolocation API
事件
基于用户位置跟踪状态,GeolocateControl
会触发以下事件:
事件 当地理定位控件状态发生变化时,将触发类型为Event的trackuserlocationstart
事件;初始启用跟踪时即立即发生;请注意此时仍处于LOCATING
状态,可能会从此状态转换到其他状态;参见状态图;
事件 当地理定位控件处于后台状态时,将触发类型为Event的trackuserlocationend
事件;当用户变更摄像机且trackUserLocation
为true
时会发生;在后台状态下,地图上的点会随位置更新而更新,但摄像机不会;
事件 当地理定位控件更改为活动锁定状态时,将触发类型为Event的userlocationfocus
事件,当用户在后台状态下点击地理定位按钮时发生,此状态使用最后已知位置重新定位地图并进入活动锁定状态;
事件 当每次地理定位API位置更新成功时,将触发类型为Event的geolocate
事件;data
- 从Geolocation.getCurrentPosition()或Geolocation.watchPosition()回调返回的Position对象;
事件 当每次地理定位API位置更新返回错误时,将触发类型为Event的error
事件;data
- 从Geolocation.getCurrentPosition()或Geolocation.watchPosition()回调返回的PositionError对象;
事件 当每次地理定位API位置更新成功但用户位置超出地图maxBounds
时,将触发类型为Event的outofmaxbounds
事件;data
- 从Geolocation.getCurrentPosition()或Geolocation.watchPosition()回调返回的Position对象;
继承
- Evented
实现
- IControl
构造函数
构造函数
new GeolocateControl(options: GeolocateControlOptions): GeolocateControl
定义于: src/ui/control/geolocate_control.ts:275
参数
参数 | 类型 | 描述 |
---|---|---|
options | GeolocateControlOptions | 控件的选项 |
返回
GeolocateControl
覆盖
Evented.constructor
方法
_isOutOfMapMaxBounds()
_isOutOfMapMaxBounds(position: GeolocationPosition): boolean
定义于: src/ui/control/geolocate_control.ts:318
检查地理定位API位置是否超出地图的maxBounds
;
参数
参数 | 类型 | 描述 |
---|---|---|
position | GeolocationPosition | 地理定位API位置 |
返回
boolean
如果位置超出地图的maxBounds
则返回true
,否则返回false
;
_onSuccess()
_onSuccess(position: GeolocationPosition): void
定义于: src/ui/control/geolocate_control.ts:363
当地理定位API返回新位置时,更新GeolocateControl
;
参数
参数 | 类型 | 描述 |
---|---|---|
position | GeolocationPosition | 地理定位API位置 |
返回
void
_updateCamera()
_updateCamera(position: GeolocationPosition): void
定义于: src/ui/control/geolocate_control.ts:430
更新摄像机位置以居中显示当前位置;
参数
参数 | 类型 | 描述 |
---|---|---|
position | GeolocationPosition | 地理定位API位置 |
返回
void
_updateMarker()
_updateMarker(position?: GeolocationPosition): void
定义于: src/ui/control/geolocate_control.ts:447
将用户位置点标记更新到当前位置;
参数
参数 | 类型 | 描述 |
---|---|---|
position? | GeolocationPosition | 地理定位API位置 |
返回
void
listens()
listens(type: string): boolean
定义于: src/util/evented.ts:165
如果这个Evented实例或任何转发的Evented实例对指定类型有监听器,则返回true;
参数
参数 | 类型 | 描述 |
---|---|---|
type | string | 事件类型 |
返回
boolean
如果指定的事件类型至少有一个注册的监听器,则为true
,否则为false
;
继承自
Evented.listens
off()
off(type: string, listener: Listener): GeolocateControl
定义于: src/util/evented.ts:90
移除先前注册的事件监听器;
参数
参数 | 类型 | 描述 |
---|---|---|
type | string | 要移除监听器的事件类型 |
listener | Listener | 要移除的监听器函数 |
返回
GeolocateControl
继承自
Evented.off
on()
on(type: string, listener: Listener): Subscription
定义于: src/util/evented.ts:73
为指定的事件类型添加监听器;
参数
参数 | 类型 | 描述 |
---|---|---|
type | string | 要添加监听的事件类型 |
listener | Listener | 事件触发时要调用的函数;监听器函数使用传递给fire的数据对象调用,该对象扩展了target和type属性 |
返回
Subscription
继承自
Evented.on
onAdd()
onAdd(map: Map): HTMLElement
定义于: src/ui/control/geolocate_control.ts:281
在地图上注册控件,并让其有机会注册事件监听器和资源;此方法由Map#addControl在内部调用;
参数
参数 | 类型 | 描述 |
---|---|---|
map | Map | 将添加此控件的地图 |
返回
HTMLElement
控件的容器元素;这应该由控件创建并由onAdd返回,而不附加到DOM:地图将根据需要将控件的元素插入到DOM中;
实现
IControl.onAdd
once()
once(type: string, listener?: Listener): Promise<any> | GeolocateControl
定义于: src/util/evented.ts:106
添加一个只会被调用一次的监听器到指定的事件类型;
在注册监听器后,首次触发事件时将调用该监听器;
参数
参数 | 类型 | 描述 |
---|---|---|
type | string | 要监听的事件类型 |
listener? | Listener | 首次触发事件时要调用的函数 |
返回
Promise<any> | GeolocateControl
如果未提供监听器,则返回this
或Promise;
继承自
Evented.once
onRemove()
onRemove(): void
定义于: src/ui/control/geolocate_control.ts:290
在地图上注销控件,并让其有机会分离事件监听器和资源;此方法由Map#removeControl在内部调用;
返回
void
实现
IControl.onRemove
setEventedParent()
setEventedParent(parent?: Evented, data?: any): GeolocateControl
定义于: src/util/evented.ts:176
将此Evented实例触发的所有事件冒泡到此Evented的父实例;
参数
参数 | 类型 | 描述 |
---|---|---|
parent? | Evented | |
data? | any |
返回
GeolocateControl
继承自
Evented.setEventedParent
trigger()
trigger(): boolean
定义于: src/ui/control/geolocate_control.ts:618
以编程方式请求并将地图移动到用户的位置;
返回
boolean
如果在控件添加到地图之前调用,则返回false
,否则返回true
;
示例
// 初始化地理定位控件
let geolocate = new GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
trackUserLocation: true
});
// 将控件添加到地图
map.addControl(geolocate);
map.on('load', () => {
geolocate.trigger();
});