import { Component } from "@angular/core" ; import { Location , getCurrentLocation , isEnabled , distance , enableLocationRequest } from "@nativescript/geolocation" ; import { Enums } from "@nativescript/core" ; @ Component ( { moduleId : module . id , styleUrls : [ "./style.css" ] , templateUrl : "./basic-location-example.html" } ) export class BasicLocationExampleComponent { public distanceResult : string = "0" ; public distance : number = 0 ; public index : number = 0 ; public startpointLongitude : number = 42.696552 ; public startpointLatitude : number = 23.32601 ; public endpointLongitude : number = 40.71448 ; public endpointLatitude : number = - 74.00598 ; constructor ( ) { enableLocationRequest ( true ) ; } public isLocationEnabled ( ) { isEnabled ( ) . then ( function ( isLocationEnabled ) { let message = "Location services are not available" ; if ( isLocationEnabled ) { message = "Location services are available" ; } alert ( message ) ; } , function ( e ) { console . log ( "Location error received: " + ( e . message || e ) ) ; } ) ; } public getDistance ( ) { let startLocation = new Location ( ) ; startLocation . longitude = this . startpointLongitude ; startLocation . latitude = this . startpointLatitude ; let endLocation = new Location ( ) ; endLocation . longitude = this . endpointLongitude ; endLocation . latitude = this . endpointLatitude ; this . distance = distance ( startLocation , endLocation ) ; console . log ( "distance - " + this . distance ) ; this . distanceResult = ( this . distance * 0.001 ) . toFixed ( 3 ) ; } public getLocationOnce ( ) { getCurrentLocation ( { desiredAccuracy : Enums . Accuracy . high , timeout : 5000 } ) . then ( location => { console . log ( "Location received: " + location ) ; this . startpointLatitude = location . latitude ; this . startpointLongitude = location . longitude ; } ) . catch ( error => { console . log ( "Location error received: " + error ) ; alert ( "Location error received: " + error ) ; } ) ; } }