import LocateViewModel from "@arcgis/core/widgets/Locate/LocateViewModel.js";const LocateViewModel = await $arcgis.import("@arcgis/core/widgets/Locate/LocateViewModel.js");- Inheritance:
- LocateViewModel→
GeolocationPositioning→ Accessor
- Since
- ArcGIS Maps SDK for JavaScript 4.0
Provides the logic for the Locate component and Locate widget, which animates the View to the user's current location.
The (geolocation) functionality is not supported on insecure origins. To use it, switch your application to a secure origin, such as HTTPS. Note that localhost is considered "potentially secure" and can be used for easy testing in browsers that supports Window.isSecureContext (currently Chrome and Firefox).
As of version 4.2, the Locate Button no longer displays in non-secure web apps. At version 4.1 this only applied to Google Chrome.
- See also
Locate component
Locate widget - Deprecated since 4.32. Use the Locate component instead.
Example
let locateWidget = new Locate({ viewModel: { // autocasts as new LocateViewModel() view: view // attaches the Locate button to the view }, container: "locateDiv"});Constructors
Constructor
Parameters
| Parameter | Type | Description | Required |
|---|---|---|---|
| properties | | |
Properties
| Property | Type | Class |
|---|---|---|
declaredClass readonly inherited | ||
| | ||
geolocationOptions inherited | ||
goToLocationEnabled inherited | ||
goToOverride inherited | ||
graphic inherited | ||
| | ||
scale inherited | ||
state readonly | | |
view inherited |
error
- Type
- GeolocationPositionError | Error | null | undefined
- Since
- ArcGIS Maps SDK for JavaScript 4.29
Error that caused the last locate-error event to fire.
Example
if(locate.viewModel.state === 'error') console.error(locate.viewModel.error); geolocationOptions
An object used for setting optional position parameters. Refer to the Geolocation API Specification for details on using these parameters.
Example
const track = new Track({ view: view, // Set optional position parameters geolocationOptions: { maximumAge: 0, timeout: 15000, enableHighAccuracy: true }}); goToLocationEnabled
- Type
- boolean
Indicates whether to navigate the view to the position and scale of the geolocated result.
- Default value
- true
goToOverride
- Type
- GoToOverride | null | undefined
- Since
- ArcGIS Maps SDK for JavaScript 4.8
This function provides the ability to override either the MapView goTo() or SceneView goTo() methods.
- See also
Example
// The following snippet uses Search but can be applied to any// widgets that support the goToOverride property.search.goToOverride = function(view, goToParams) { goToParams.options = { duration: updatedDuration }; return view.goTo(goToParams.target, goToParams.options);}; graphic
- Type
- Graphic
The graphic used to show the user's location on the map.
Example
const trackWidget = new Track({ // Assign the track widget to a view view: view, // Overwrite the default symbol used for the // graphic placed at the location of the user graphic: new Graphic ({ symbol: { // autocasts as new SimpleMarkerSymbol() type: "simple-marker", size: "12px", color: "blue", // autocasts as new SimpleLineSymbol() outline: { color: "#efefef", width: "1.5px" } } })}); scale
- Since
- ArcGIS Maps SDK for JavaScript 4.7
Indicates the scale to set on the view when navigating to the position of the geolocated result, after a location is returned.
By default, the view will navigate to a scale of 2500 for 3D and 4514 for 2D.
To override the default in 2D, set the scale property and also set snapToZoom to false.
For 2D views the value should be within the effectiveMinScale
and effectiveMaxScale.
state
- Type
- LocateViewModelState
The current state of the widget.
- Default value
- "disabled"
view
- Type
- MapViewOrSceneView | null | undefined
The view associated with the widget.
Methods
| Method | Signature | Class |
|---|---|---|
cancelLocate(): void | | |
emit inherited | emit<Type extends EventNames<this>>(type: Type, event?: this["@eventTypes"][Type]): boolean | |
hasEventListener inherited | hasEventListener<Type extends EventNames<this>>(type: Type): boolean | |
locate(): Promise<GeolocationPosition | null> | | |
on inherited | on<Type extends EventNames<this>>(type: Type, listener: EventedCallback<this["@eventTypes"][Type]>): ResourceHandle |
cancelLocate
- Signature
-
cancelLocate (): void
- Since
- ArcGIS Maps SDK for JavaScript 4.9
This function provides the ability to interrupt and cancel the process of programmatically obtaining the location of the user's device.
- Returns
- void
emit
- Signature
-
emit <Type extends EventNames<this>>(type: Type, event?: this["@eventTypes"][Type]): boolean
- Type parameters
- <Type extends EventNames<this>>
- Since
- ArcGIS Maps SDK for JavaScript 4.5
Emits an event on the instance. This method should only be used when creating subclasses of this class.
hasEventListener
- Signature
-
hasEventListener <Type extends EventNames<this>>(type: Type): boolean
- Type parameters
- <Type extends EventNames<this>>
Indicates whether there is an event listener on the instance that matches the provided event name.
Parameters
| Parameter | Type | Description | Required |
|---|---|---|---|
| type | Type | The name of the event. | |
- Returns
- boolean
Returns true if the class supports the input event.
locate
- Signature
-
locate (): Promise<GeolocationPosition | null>
Animates the view to the user's location.
- Returns
- Promise<GeolocationPosition | null>
Resolves to an object with the same specification as the event object defined in the locate event.
Example
let locateWidget = new Locate({ viewModel: { // autocasts as new LocateViewModel() view: view }, container: "locateDiv"});
locateWidget.locate().then(function(){ // Fires after the user's location has been found}); on
- Signature
-
on <Type extends EventNames<this>>(type: Type, listener: EventedCallback<this["@eventTypes"][Type]>): ResourceHandle
- Type parameters
- <Type extends EventNames<this>>
Registers an event handler on the instance. Call this method to hook an event with a listener.
Parameters
| Parameter | Type | Description | Required |
|---|---|---|---|
| type | Type | An event or an array of events to listen for. | |
| listener | EventedCallback<this["@eventTypes"][Type]> | The function to call when the event fires. | |
- Returns
- ResourceHandle
Returns an event handler with a
remove()method that should be called to stop listening for the event(s).Property Type Description remove Function When called, removes the listener from the event.
Example
view.on("click", function(event){ // event is the event handle returned after the event fires. console.log(event.mapPoint);});Events
locate
locate: CustomEvent<LocateViewModelLocateEvent> Fires after the locate() method is called and succeeds.
- See also
Example
locateBtn.on("locate", ({ position }) => { const { longitude, latitude } = position.coords; console.log(`lat: ${latitude.toFixed(4)}, long: ${longitude.toFixed(4)}`);}) locate-error
locate-error: CustomEvent<LocateViewModelLocateErrorEvent> Fires after the Locate.locate() method is called and fails.
- See also
Type definitions
LocateViewModelState
- Type
- "disabled" | "ready" | "locating" | "feature-unsupported" | "error"