Jump to content
JohnLM

Developing apps for GPS related info, i.e., miles/speed/location/etc

Recommended Posts

I have an e-scooter that I use for riding around towns and trails. 

 

There is an app to download.  However, that app does not connect to my scooter and the developer is slow to fix/resolve/respond and still does not connect. 

 

So, I would like to look into writing my own app for my phone (android) to do the following: 

 

1. track start and end times
3. track my routes - where i've been or stopped at 
4. show the miles I am traveling during my ride in real-time 
5. show the total miles of my ride 
6. possibly show speed (via calculation) and in real-time 
7. eventually, to show a map of my route in real-time, and show all my stops 

 

Is this possible with Delphi 11.2 and free components/services/API's ?

Share this post


Link to post

You showed the features you want your application have. Good.

But what can you do and what can't you do?

To answer to your exact question: Yes, it is possible to do it with Delphi 11.2 and free components/services/API's.

Edited by FPiette
  • Like 1

Share this post


Link to post

After some searching, I found and tried various code snippets dating back a few years but they did not work because they were missing the code snippets for handling Permissions on Android or at least my phone's version of Android 12.

 

Then, I decided to search around in my samples folder and did find a sample called, "Location" in the C:\Users\Public\Documents\Embarcadero\Studio\22.0\Samples\Object Pascal\Mobile Snippets\Location folder.   I compiled it into my Android phone and it work right from the start.  Thanks, Embarcadero!

 

I also found a couple of youtube videos, below, showing exactly this app in action. 

 

 

 

 

I believe I can complete steps 1, 2, and 6, below. 
 

1. track start and end times
2. track my routes - where I've been or stopped at 
3. show the miles I am traveling during my ride in real-time 
4. show the total miles of my ride 
5. possibly show speed (via calculation) and in real-time 
6. eventually, to show a map of my route in real-time, and show all my stops 

 

 

Share this post


Link to post

For steps 3, 4 and 5, you need to compute the distance between two consecutive GPS coordinates. For that you use the "haversine distance". Here is some code I wrote for the purpose :
 

// Return distance in meters between two points given by their
// latitude/longitude expressed in degrees
function HaversineDist(
    Lat1 : Extended;   // Latitude  of point 1 in degrees
    Lng1 : Extended;   // Longitude of point 1 in degrees
    Lat2 : Extended;   // Latitude  of point 2 in degrees
    Lng2 : Extended)   // Longitude of point 2 in degrees
    : Extended;        // Distance in meters
var
    Dx, Dy, Dz : Extended;
const
    Diameter = 2 * 6372.8 * 1000;   // Meters
begin
    Lng1    := DegToRad(Lng1 - Lng2);
    Lat1    := DegToRad(Lat1);
    Lat2    := DegToRad(Lat2);

    Dz     := Sin(Lat1) - Sin(Lat2);
    Dx     := Cos(Lng1) * Cos(Lat1) - Cos(Lat2);
    Dy     := Sin(Lng1) * Cos(Lat1);
    Result := ArcSin(Sqrt(sqr(Dx) + Sqr(Dy) + Sqr(Dz)) / 2) * Diameter;
end;

 

Once you get the distance between consecutive GPS points, you can sum the distances to get the track length. To get the speed, you divide the distance by time, you get m/s that you can easily convert to the units you like.

 

Share this post


Link to post

When buying third party component be sure to buy with FULL source code. And when you receive it, first thing to do is discard any dcu, bpl, obj, dll and all other binary files and rebuild everything. That way you are sure to have full source code an use that code.

Why? Because one day or another the developer will disappear or stop updating his code for next Delphi release. With full source code, you can simply [most of the time] rebuild the product with the next Delphi version.

  • Like 2

Share this post


Link to post

For step 2, I've added a tmemo control to hold all the data (latitude and longitude) that comes in during my travels. 

 

In addition, I added to the list to include an Index counter (line # 1, 2, 3, .. 200, etc.) and a time stamp entry for each lat/long value, thus. . . 

 

001 - 5:06:03 PM - 0.0101-nnn : 0.0101-nnn
002 - 5:06:09 PM - 0.0102-nnn 0.0102-nnn
003 - 5:06:10 PM - 0.0102-nnn : 0.0103-nnn

 

I just need to find a way to plug those values for the lat/long into a map somehow and have it plot my previous travel route onto a map in my app. 

 

Next, I will research how to do this. 

Share this post


Link to post
14 minutes ago, JohnLM said:

I just need to find a way to plug those values for the lat/long into a map somehow and have it plot my previous travel route onto a map in my app. 

There's a demo on using TMapView and placing markers, here.

Share this post


Link to post
7 hours ago, JohnLM said:

I just need to find a way to plug those values for the lat/long into a map somehow and have it plot my previous travel route onto a map in my app. 

You can use the free OpenStreetMap topographic maps and use their "tile server" to download the tiles. Those are simple JPG images of the map at different zoom factor. Look there : https://wiki.openstreetmap.org/wiki/Tiles.
 

        URL := Format('https://tile.openstreetmap.org/%d/%d/%d.png', [Zoom, TileX, TileY]);

        FSslHttpCli.URL           := URL;
        FSslHttpCli.OnRequestDone := SslHttpCliRequestDone;
        FSslHttpCli.RcvdStream    := TMemoryStream.Create;
        FSslHttpCli.GetASync;

 

Share this post


Link to post

Although this should be possible with Rx11.2, you should be aware that you are touching quite a lot of critical Android stuff,

which needs each a lot of experience and knowledge, like

- sensors and location

- permission handling

- I assume you would like to have background operation

- therefor you would need Android services very likely

 

I am not sure, if you start with a new project with Rx11.2 on Android for the first time.

That's why I think this maybe quite a big task to get a smooth and easy start.

Better try and test each of those problem zones separately, instead of putting it all together in the first try.

Share this post


Link to post

@ Rollo62, Hi, I am starting out slowly.  But I've coded various apps on Android over the years since XE7.  I am not fluent in the Permissions area, but so far in this project, I am okay with what I have working on.  I ride through trails in my area, so I am using that as an opportunity to test my app in order to debug and make any necessary changes/additions. 
 

  • Like 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

×