Android Geolocation

Just starting out? Need help? Post your questions and find answers here.
Eric
Posts: 1
Joined: Wed Aug 19, 2020 1:47 pm

Android Geolocation

Post by Eric »

I am developing an application for Android that uses the Geolocation and am having various issues with this on a Galaxy Note 10+ phone. Although the location service is on, the app does not always pick it up, especially the elevation & speed functions. Any ideas of how I can fix this would be much appreciated. Also, if anyone knows how to access the barometer sensor of the phone, this would be appreciated.

The main code is:

Code: Select all

Structure UKAirp
  Name.s
  ID.s
  Lat.s
  Lng.s
  Elev.s
EndStructure

Global.d AppAng, TrgLat, TrgLng, TrgElev

Procedure.d Dist(Lat1.d, Lng1.d, Lat2.d, Lng2.d)

  Define.d DLat, DLng, a, c, Lat1R, Lng1R, Lat2R, Lng2R
  
  Lat1R = Lat1 * #PI / 180
  Lng1R = Lng1 * #PI / 180
  Lat2R = Lat2 * #PI / 180
  Lng2R = Lng2 * #PI / 180
  
  DLat = (Lat2 - Lat1) * #PI / 180
  DLng = (Lng2 - Lng1) * #PI / 180
  
  a = Sin(DLat / 2) * Sin(DLat / 2) + Cos(Lat1R) * Cos(Lat2R) * Sin(DLng / 2) * Sin(DLng / 2)
  c = 2 * ATan2(Sqr(1 - a), Sqr(a))
  
  ProcedureReturn c * 6371 * 0.53995680346
  
EndProcedure
Procedure CloseWindowEvent()
  StopGeolocation()
  CloseWindow(EventWindow())
  End
EndProcedure

Procedure Events()
  
  Define.d Ht, dH, ScrnFct, HtMin, HtMax
  Define.d Distnc, CurntHt 
  Define LineY.i, Spd.d, VSpd.d
  
;  Static.d Distnc, CurntHt  ;for dev
  
;  Debug "GeolocationAltitude: " + GeolocationAltitude()
;  Debug "GeolocationSpeed: " + GeolocationSpeed()     
  
  ;52.6078	-1.0320 - EGBG
;52.1803	-1.1138 - Daventry
  
  Distnc = dist(GeolocationLatitude(), GeolocationLongitude(), TrgLat, TrgLng)
  CurntHt = GeolocationAltitude()*3.2808399 - TrgElev  ;m -> ft converted to QFE
  Spd = GeolocationSpeed()*1.94384449         ;m/s -> knots
  If Spd = 0 
    Spd = 30
  EndIf
    
  ht=Distnc*Tan(AppAng*#PI/180)*6076.11549        ;convert to ft
  Htmin = Distnc*Tan((AppAng-1)*#PI/180)*6076.11549   ;convert to ft
  HtMax = Distnc*Tan((AppAng+1)*#PI/180)*6076.11549   ;convert to ft
  
  ScrnFct = 400/(htmax-htmin)     ;Distnc*6076.11549)
  dH = CurntHt - ht
  LineY = dH*ScrnFct +200
  
  VSpd = Distnc * 60 / Spd         ;Time in min to dest
  VSpd = CurntHt / VSpd
  
  StringGadget(1,  10, 402, 200, 17, "Distance(nm): "+StrD(Distnc,1))
  StringGadget(2,  10, 422, 200, 17, "GPS Speed (kn)  : "+StrD(Spd,0))
  StringGadget(3,  10, 442, 200, 17, "QFE height(ft): "+StrD(Ht,0))
  StringGadget(4,  10, 462, 200, 17, "Vertical speed(ft/min): " + StrD(VSpd,0))
  StringGadget(5,  10, 482, 200, 17, "Current Alt: " + StrD(CurntHt,0))
  
  If CreateImage(0, 300, 400)
    If StartDrawing(ImageOutput(0))
      
      Box(0, 0, 300, lineY, #Green)
      Box(0, LineY, 300, 400, #Red)
      
      LoadFont(0, "Calibri", 12)
      DrawingFont(FontID(0))
      
      DrawingMode(#PB_2DDrawing_Transparent)
      
      DrawText(2, LineY-13, StrD(CurntHt,0)+" / "+StrD(Spd,0))
      
      DrawText(260, 187, StrD(Ht,0))
      
      LineXY(0, 199, 300, 199, #Blue)
      LineXY(0, 200, 300, 200, #Blue)
      LineXY(0, 201, 300, 201, #Blue)
      
      LineXY(0, 399, 300, 399, #Black)
      
      StopDrawing()
     
   EndIf
   
  EndIf
 
  ImageGadget(0, 0, 0, 300, 400, ImageID(0))  
  
EndProcedure

;No debug
CloseDebugOutput()

OpenWindow(0, 0, 0, 300, 500, "VNav", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
  Define Input.s, I.i, No.i
  Dim Airports.UKAirp(167)
  
  Input = InputRequester("Enter Approach angle (deg)? ", "3")
  If Input = "" 
    AppAng = 3
  Else
    AppAng = ValD(Input)
  EndIf
  
  Input = InputRequester("Enter 4 letter airport ID ? ", "EGBG")
  Restore StringData

  For i = 0 To 166
    Read . s Airports(I)\Name
    Read . s Airports(I)\ID
    Read . s Airports(I)\Lat
    Read . s Airports(I)\Lng
    Read . s Airports(I)\Elev
    If Input = Airports(I)\ID
      No = I
      Break
    EndIf
  Next I
  
  TrgLat  = ValD(Airports(No)\Lat)
  TrgLng  = ValD(Airports(No)\Lng)
  TrgElev = ValD(Airports(No)\Elev)
  SetWindowTitle(0, "QFE VNav to " + Airports(No)\Name)
  
  StartGeolocation(100)
  
  AddWindowTimer(0, 0, 1000) ; Add a timer every second (1000 ms)
  BindEvent(#PB_Event_Timer, @Events())
  BindEvent(#PB_Event_CloseWindow, @CloseWindowEvent())

;Fin


;-----------------Airport data
DataSection
; Not included here.....
Edit: Code-Tags added (Peter)