如何在Angular2中读取用户当前的地理区域?

时间:2016-05-14 13:28:47

标签: geolocation angular

我实际上在搜索用户的位置(至少是地理区域)api,可以使用Angular2打字稿代码轻松使用。

1 个答案:

答案 0 :(得分:0)

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());
        });
    }
}