Created
February 27, 2016 15:31
-
-
Save PeterOrneholm/6eda1d4bd176db0645bb to your computer and use it in GitHub Desktop.
TypeScript position example
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 {IPositionTracker} from "IPositionTracker"; | |
| import GeoLocationPositionTracker from "GeoLocationPositionTracker"; | |
| class App { | |
| constructor(private positionTracker : IPositionTracker) { | |
| } | |
| start(): void { | |
| this.positionTracker.subscribe(c => { | |
| alert(`Lat: ${c.latitude}; Lon: ${c.longitude}`); | |
| }); | |
| } | |
| } | |
| let tracker = new GeoLocationPositionTracker(true); | |
| let app = new App(tracker); | |
| app.start(); |
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 {IPositionTracker} from "IPositionTracker"; | |
| import {ICoordinates} from "ICoordinates"; | |
| export default class GeoLocationPositionTracker implements IPositionTracker { | |
| constructor(private enableHighAccuracy: boolean = true) { | |
| } | |
| subscribe(onNewPosition: (coords: ICoordinates) => void): void { | |
| const options = { | |
| enableHighAccuracy: this.enableHighAccuracy, | |
| maximumAge: 15000 | |
| }; | |
| navigator.geolocation.watchPosition((position) => { | |
| onNewPosition({ | |
| latitude: position.coords.latitude, | |
| longitude: position.coords.longitude | |
| }); | |
| }, null, options); | |
| } | |
| } |
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
| export interface ICoordinates { | |
| latitude: number; | |
| longitude: number; | |
| } |
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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>TypeScript</title> | |
| <meta charset="utf-8" /> | |
| </head> | |
| <body> | |
| <h1>TypeScript GPS</h1> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.22/system.js"></script> | |
| <script> | |
| System.defaultJSExtensions = true; | |
| System.config({ | |
| baseURL: '/' | |
| }); | |
| System.import('app.js'); | |
| </script> | |
| </body> | |
| </html> |
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 {ICoordinates} from "ICoordinates"; | |
| export interface IPositionTracker { | |
| subscribe(onNewPosition: (coords: ICoordinates) => void): void; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment