Jump to content
Sign in to follow this  
erva

Android crashes in formula

Recommended Posts

Hi,

 

Android TLocation.LocationChanged crashes when executing code at line where variable lstDistance is calculated. iOS works fine. I have tried Double type instead of Extended type but with no help. In iOS both types work fine. If i change lstDistance := 1;, code works fine.

 

Code is working Taximeter where charge is counted based on distance and time:)

 

procedure TfrmMain.senLocationLocationChanged(Sender: TObject; const OldLocation,
  NewLocation: TLocationCoord2D);
var
  Latitude1: Extended;
  Longitude1: Extended;
  Latitude2: Extended;
  Longitude2: Extended;
  lstDistance: Extended;
begin

  //MATKAN MITTAUS
  Latitude1 := OldLocation.Latitude;
  Longitude1 := OldLocation.Longitude;
  Latitude2 := NewLocation.Latitude;
  Longitude2 := NewLocation.Longitude;
  lstDistance := ArcCos(Sin(DegToRad(Latitude1)) * Sin(DegToRad(Latitude2)) + Cos(DegToRad(Latitude1)) * Cos(DegToRad(Latitude2)) * Cos(DegToRad(Longitude2 - Longitude1))) * 6367000;

 

// THESE LINES I HAVE COMMENTED OUT TO MAKE SURE THEY DON*T CAUSE CRASHING

//  lstDistance := lstDistance * 1.01; //Calibrointi
//  totDistance := totDistance + lstDistance;
//
//  //COUNT OF MEASUREMENTS
//  mitattuKpl := mitattuKpl + 1;
//
//  //DISTANCE IN EUROS
//  matkaE := totDistance * (0.99 / 1000);
//

// TIME IN EUROS
//  aikaLopetus := Now;
//  kyydinKesto := aikaLopetus - aikaAloitus;
//  aikaE := ((24*60)*0.89)*kyydinKesto;
//  summaE := lahtoE + matkaE + aikaE;
//
//  //UPDATE LABELS
//  lblLongitude.Text := Format('%2.6f', [NewLocation.Longitude]);
//  lblLatitude.Text := Format('%2.6f', [NewLocation.Latitude]);
//  lblMitattu.Text := Format('%2.2f', [lstDistance]);
//  lblMatka.Text := Format('%2.2f', [totDistance]);
//  lblMittauksia.Text := IntToStr(mitattuKpl);
//  lblLahto.Text := Format('%2.2f', [lahtoE]);
//  lblKmE.Text := Format('%2.2f', [matkaE]);
//  lblAikaE.Text := Format('%2.2f', [aikaE]);
//  lblSumma.Text := Format('%2.2f', [summaE]);

end;
 

Share this post


Link to post

On iOS, the first location change has values of 0 for OldLocation.Latitude and OldLocation.Longitude. On Android, they're both NaN, so your code will fail in that case. You should do this check (at least):

    if not IsNan(Latitude1) and not IsNan(Longitude1) then


 

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×