Skip to content

Instantly share code, notes, and snippets.

@P1xt
Created March 15, 2017 16:51
Show Gist options
  • Save P1xt/6aca515e3a4c02756adf06e0ebe1c941 to your computer and use it in GitHub Desktop.
Save P1xt/6aca515e3a4c02756adf06e0ebe1c941 to your computer and use it in GitHub Desktop.
Angular 2 service to pull location information from freegeoip.net
import { Injectable } from '@angular/core';
import { Http, Response, Jsonp, URLSearchParams } from '@angular/http';
@Injectable()
export class GeolocationService {
constructor(private jsonp: Jsonp) { }
getLocation = (position) => {
let apiUrl: string = 'http://freegeoip.net/json';
let params = new URLSearchParams();
params.set('callback', 'JSONP_CALLBACK');
return this.jsonp
.get(apiUrl, { search: params })
.map(response => response.json());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment