| 
									
										
										
										
											2018-02-27 17:06:06 -05:00
										 |  |  | import { Observable } from 'rxjs'; | 
					
						
							| 
									
										
										
										
											2018-01-09 11:31:41 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // #docregion
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Create an Observable that will start listening to geolocation updates
 | 
					
						
							|  |  |  | // when a consumer subscribes.
 | 
					
						
							|  |  |  | const locations = new Observable((observer) => { | 
					
						
							| 
									
										
										
										
											2020-01-09 20:50:44 +02:00
										 |  |  |   let watchId: number; | 
					
						
							| 
									
										
										
										
											2018-01-09 11:31:41 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Simple geolocation API check provides values to publish
 | 
					
						
							|  |  |  |   if ('geolocation' in navigator) { | 
					
						
							| 
									
										
										
										
											2020-01-09 20:50:44 +02:00
										 |  |  |     watchId = navigator.geolocation.watchPosition((position: Position) => { | 
					
						
							|  |  |  |       observer.next(position); | 
					
						
							|  |  |  |     }, (error: PositionError) => { | 
					
						
							|  |  |  |       observer.error(error); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-01-09 11:31:41 -08:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2020-01-09 20:50:44 +02:00
										 |  |  |     observer.error('Geolocation not available'); | 
					
						
							| 
									
										
										
										
											2018-01-09 11:31:41 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // When the consumer unsubscribes, clean up data ready for next subscription.
 | 
					
						
							| 
									
										
										
										
											2020-01-09 20:50:44 +02:00
										 |  |  |   return { | 
					
						
							|  |  |  |     unsubscribe() { | 
					
						
							|  |  |  |       navigator.geolocation.clearWatch(watchId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2018-01-09 11:31:41 -08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Call subscribe() to start listening for updates.
 | 
					
						
							|  |  |  | const locationsSubscription = locations.subscribe({ | 
					
						
							| 
									
										
										
										
											2020-01-09 20:50:44 +02:00
										 |  |  |   next(position) { | 
					
						
							|  |  |  |     console.log('Current Position: ', position); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   error(msg) { | 
					
						
							|  |  |  |     console.log('Error Getting Location: ', msg); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-01-09 11:31:41 -08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Stop listening for location after 10 seconds
 | 
					
						
							| 
									
										
										
										
											2020-01-09 20:50:44 +02:00
										 |  |  | setTimeout(() => { | 
					
						
							|  |  |  |   locationsSubscription.unsubscribe(); | 
					
						
							|  |  |  | }, 10000); | 
					
						
							| 
									
										
										
										
											2018-01-09 11:31:41 -08:00
										 |  |  | // #enddocregion
 |