![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@ng-web-apis/geolocation
Advanced tools
This is a library for declarative use of Geolocation API with Angular
This is an Observable
based abstraction over
Geolocation API to use with Angular
If you do not have @ng-web-apis/common:
npm i @ng-web-apis/common
Now install the package:
npm i @ng-web-apis/geolocation
GeolocationService
is an Observable
, that emits
Position object
Import service in your component:
import {GeolocationService} from '@ng-web-apis/geolocation';
// ...
constructor(private readonly geolocation$: GeolocationService) {}
Now, to observe user position, you can subscribe to geolocation$
:
geolocation$.subscribe((position) => doSomethingWithPosition(position));
If you need to get position just once and stop observing user location, you can subscribe to geolocation$
and use
take(1)
RxJs operator:
geolocation$.pipe(take(1)).subscribe((position) => doSomethingWithPosition(position));
Or you can use async pipe to get position directly in template:
<div *ngIf="geolocation$ | async as position">
<span>{{position.coords.latitude}}</span>
</div>
Service is cold, meaning if there are no active subscriptions, it doesn't track position.
The library also provides some tokens to simplify working with Geolocation API:
GEOLOCATION_SUPPORT
returns true
if user's browser supports
Geolocation APIexport class Example {
constructor(@Inject(GEOLOCATION_SUPPORT) private readonly geolocationSupport: boolean) {}
}
POSITION_OPTIONS
token with optional properties named enableHighAccuracy
, timeout
and maximumAge
. It uses {}
by default.@NgModule({
...
providers: [
{
provide: POSITION_OPTIONS,
useValue: {enableHighAccuracy: true, timeout: 3000, maximumAge: 1000},
},
],
})
export class AppModule {}
GEOLOCATION
token.![]() | ![]() | ![]() | ![]() | ![]() |
---|---|---|---|---|
9+ | 3.5+ | 5+ | 5+ | 3.2+ |
You can try online demo here
Other Web APIs for Angular by @ng-web-apis
FAQs
This is a library for declarative use of Geolocation API with Angular
The npm package @ng-web-apis/geolocation receives a total of 1,995 weekly downloads. As such, @ng-web-apis/geolocation popularity was classified as popular.
We found that @ng-web-apis/geolocation demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.