Created
October 22, 2015 14:12
-
-
Save mrtnbroder/8a9ff2f659897d45736c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Rx from 'rx' | |
| import request from 'superagent' | |
| export function getCurrentPosition(geolocationOptions = {enableHighAccuracy: true, timeout: 8000}) { | |
| if (!window.navigator && !window.navigator.geolocation) { | |
| throw new TypeError('geolocation not available') | |
| } | |
| return new Rx.AnonymousObservable((observer) => { | |
| window.navigator.geolocation.getCurrentPosition( | |
| (data) => { | |
| observer.onNext(data) | |
| observer.onCompleted() | |
| }, | |
| observer.onError.bind(observer), | |
| geolocationOptions | |
| ) | |
| }) | |
| } | |
| export function findLocationByLatLng({lat, lng}) { | |
| return Rx.Observable.fromNodeCallback(request.get)( | |
| `https://maps.googleapis.com/maps/api/geocode/json?` | |
| + [ | |
| `latlng=${lat},${lng}`, | |
| `sensor=false` | |
| ].join('&') | |
| ) | |
| } | |
| export function findLocationByZip(zip = '') { | |
| return Rx.Observable.fromNodeCallback(request.get)( | |
| `https://maps.googleapis.com/maps/api/geocode/json?` | |
| + [ | |
| `address=${zip}`, | |
| `components=country:USA`, | |
| `sensor=false` | |
| ].join('&') | |
| ) | |
| } | |
| export function watchPosition(geolocationOptions) { | |
| if (!window.navigator && !window.navigator.geolocation) { | |
| throw new TypeError('geolocation not available') | |
| } | |
| return new Rx.AnonymousObservable((observer) => { | |
| var watchId = window.navigator.geolocation.watchPosition( | |
| observer.onNext.bind(observer), | |
| observer.onError.bind(observer), | |
| geolocationOptions | |
| ) | |
| return () => { | |
| window.navigator.geolocation.clearWatch(watchId) | |
| } | |
| }).publish().refCount() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment