"# Angular 5 Geolocation Service"
How to called in component. Add the reference in component class file.
- import { GeoLocationService } from './geo-location.service';
- ChangeDetectorRef from @angular/core import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
- Add fllowing line in constructor
errorMsg: string;
currentLocation: Coordinates = null;
constructor(
private ref: ChangeDetectorRef,
private geoLocationService: GeoLocationService ) { } - Call service
searchByCurrent() {
let self = this;
const accuracy = { enableHighAccuracy: true }; self.geoLocationService.getLocation(accuracy).subscribe( function(position) {
self.currentLocation = position; self.ref.detectChanges();
}, function(error) { self.errorMsg = error; self.ref.detectChanges(); } );