My current project involves retrieving my exact location (City Name
) using Typescript
. I am working on creating a service that can accomplish this task. Below is the code snippet I have implemented:
import { Injectable} from '@angular/core';
import { Observable } from 'rxjs/Observable';
import {Http} from '@angular/http';
import 'rxjs/Rx';
@Injectable()
export class CurrentLocationService {
constructor(private _http: Http) {}
getCurrentLocation(): Observable<any> {
return this._http.get('http://ipinfo.io/json?callback=JSON_CALLBACK')
.map(response => response.json())
.catch(error => {
console.log(error);
return Observable.throw(error.json());
});
}
}
However, the data returned by this Service
seems to be incorrect. When I manually access the URL
http://ipinfo.io/json?callback=JSON_CALLBACK
, I receive the following values:
"city": "Munich",
"region": "Bavaria",
"country": "DE",
These values are accurate in my case, but the Service mentioned above incorrectly returns Connaught Place, IN
as the location. Is there a more reliable way to determine the current location using a service in Typescript
?