🛤️ How to import, create and use gpx and kml track files to App Inventor Map

How to import, create and use gpx and kml track files to App Inventor Map

GPX / KML File Reader/Creator provides a way to import map route displays made with Garman GPS devices or Web pages that publish kml hiking routes (hiking tracks etc.). App Inventor uses a LineString to display a similar track. A gpx or kml file can be 'converted' to a set of location 'points' and loaded to a LineString and saved so it can be used for future use in the device's Documents folder.

(gpx file from Django extension example)

(kml file from Django extension example)


How developers use kml and gpx files to distribute/display map tracks is Using GPX & KML files for hiking | Hike Navigation

Other interesting links KML'nizi Barındırmak için Google Sites'ı Kullanma  |  Keyhole Markup Language  |  Google for Developers
https://www.alltrails.com/


This tutorial explains

  • how to download a gpx or kml file to your computer and use it in App Inventor with a free extension.
  • how to copy a gpx or kml file from your Assets to the shared Document folder on your Android 10 or higher.
  • how to capture and save a track as a LineString track and convert it to a gpx or kml file that can be shared using the LocationSensor
  • how to create and save a track using the Map screen to create a List of Points by selecting coordinates from the Map
  • how to read a gpx or kml track file and display it on the Map as a LineString

:railway_track: The GPX / KML File Reader/Creator example uses the DjGPXTools extension 1/1/2021 version 2.

To avoid problems experimenting with the example app: DO NOT run the aia from the Companion aia. BUILD the apk using Build>Android App (.apk) (see below).


The Designer Screen

This is the Designer layout. It shows the controls and extensions used to construct the Project.


menu
The Tools ----------

The tools to copy, view files, create files and capture files are as shown above and described below.
Create Tools and Capture Tools toggle these two tools visible or hidden as needed.


copy

How track files created by outside sources can be moved to the example GPX / KML File Reader/Creator app.

Copy track files from Assets you provide to the system Downloads folder.

  1. You may want to Delete existing kml and gpx files from the Android Documents folder. Use a File manipulation app (I use My Files);

  2. You may want to Restart your Android. You need to clear the Documents folder of unwanted kml gpx files if you did not follow the advice above and created instances of the kml or gpx track file in Documents using another app or a version of this app using Companion. If you do not clear the documents some files in Assets will not be replicated if a file by the same name exists. Those files consequently will not be recognized by the app. If the app attempts to add the file from Assets the file will appear in Documents but will not be recognized by the app. When you later build the apk, the example app will not be able to read the files in Documents with the compiled program because the files are created with Companion. To be able to read stored files, your compiled app must first create them. It cannot access Documents folder files created using other apps or Companion. Only the files that are created by the app are recognized by the app. This behavior is a result of adhering to Google's security policy regarding files not created by the app you code and build.

  3. add your kml and gpx files to the Project's Media Assets. Only then they can be copied into Documents using the app. Three example files are already in Assets giving you a head start. Drag and drop several of your outside kml files into Media to make your outside files available to your app. Sorry, you can not merely copy kml/gpx files into the Documents folder and expect them to be usable with the the compiled app. Usable files must be created with the compiled app.

  4. use the Copy Assets to Documents button to copy all the gpx and kml files you placed in your Assets to the Documents folder. Otherwise, your ListPicker will not have any track files to select until you copy klm or gpx Assets there unless you use Create or Capture options and store the files created by those tools.

  5. at this point you can use your device's file explorer app to check what files are now written to the Documents folder. Only the files 'created' using the compiled app can be read by the app.


    My File capture of Documents folder.

  6. Use
    pick to display tracks on App Inventor

Pick GPX or KML File to Display using a ListPicker

![dRoute|225x500]

====================

Another way to get kml or gpx files created by other apps to work with Django's extension is solved in Part II of this tutorial How to import, create and use gpx and kml track files using App Inventor Map Part II Use SAF extension to select kml/gpx files stored in your Documents folder

  • Kml or gpx files dragged to the Documents folder using USB to transfer the files are not recognized by the app this tutorial creates.

  • several links discuss the SAF extension which might be feasible to read outside kml/gpx files:

See KML File storage - #8 by Anke
How do you read an "alien" file from Download under SDK 30? - #4 by Alexandre_Gorine
How do you read an "alien" file from Download under SDK 30?


create

Create a new kml or gpx file with the example app---------------

The Map1.DoubleTapAtPoint block can be used to generate a LineString by adding coordinates or adapt the code to also use a Point List you create using outside data to make a LineString. The LineString is saved as a kml or gpx file to Documents folder and can be viewed using the ListPicker.

doubleTap

  • Enable the DoubleTapAtPoint feature using the Switch. The Create LS switch must be ON to add coordinates. The switch needs to be OFF when not in use.
  • Select the coordinates you want using double tap to create a LineString.
  • Enter a name for the track. The file will not save if there isn't a file name entered.
  • Use one of the Create buttons to save the track as a kml or gpx ( the track name must not be empty.)

An example is the Washington.kml example


captureCapture a kml or gpx file track using the LocationSensor and LineString-----------

The LineString is populated by gps coordinates as the Android device changes location (or thinks it is changing location). This feature can be used to store the routes/tracks in your version of the app. It can SAVE the routes you make while using your gps and saving them as a kml or gpx file that your app can return to.

captureTool

  • Start/Stop enables/disables the gps. It will take a while to start generating the track. The GPS needs to provide at least two satellite fixes and location changes.
  • GPS as gpx creates a gpx from the track made so far (the LineString ). Enter track name to provide a name for the file. The button will not save the track if it is unnamed.
  • GPS as kml creates and saves a kml from the track made so far (the the LineString) if the enter track name contains a name for the file.

blocksToCapture Blocks to Capture from your gps


Exporting/Importing Garmin gpx or kml files.-------------------

A way to acquire Garmin gpx files for download to your computer is described in


The tutorial example app is possible because of the availability of two extensions:

How Django's__Extension works:

-ReadGPX/ReadKML: enter the full path of your GPX or KML file (e.g. storage/emulated/0/yourfile.gpx)
-GPXRead/KMLRead: returns a list of lat/lng pairs.
-CreateGPX/CreateKML: fileName ( e.g. /storage/emulated/0/yourTrack.gpx)

  • name: Name of your track
  • points: enter the list of your lat/lng coordinates
    -GPXCreated/KMLCreated: returns: path to the written GPX or KML file. result: as String. size: amount of coordinates.

Testing ------------------------------

The app was only tested on a Samsung A13, Android 13. How it will perform with different Android versions and devices is unknown.


Questions -------------------

  • Can all gpx or kml files others publish be rendered into a Points file and LineString? No. Some might be too complex for the extension algorithms.

  • Can the SAF extension or the File control be used to build something similar? Yes, see Part II of this tutorial.

  • How do you build your own app to use Django's extension? Download both Taifun's and Django's extensions; start a Project; import the extensions and start programming. You possibly do not need all the features available in the example.

  • What happens if you do not copy external tracks to Documents? Essentially nothing, you can still use the tools to create and capture tracks.

  • Will you have to turn your device off after you manually delete files from Documents created either using Companion or from an outside source? Probably. This is true when using my Samsung A13; it might not be required using your Android. For some reason, deleting files using My Files fails to remove them until a device reset. :cry: :cry:

  • Are there bugs? :beetle: I hope not but one never knows. I did not code all error control possible.


Credits:-------------

Thanks to Taifun and Django for their extensions.


I hope developers can make use of the techniques used in the tutorial in their Hiking and tracking apps.


AIA for GPX / KML File Reader/Creator app.**
GPX_KML_DemoDocuments.aia (103.7 KB)

To avoid problems experimenting with the example app: DO NOT run the aia from the Companion aia. BUILD the apk using Build>Android App (.apk) (see above).


Regards,
SteveJG

6 Likes

Thanks for the demo;
My question is; it would be possible in the part of choosing a file from the documents folder; choose another kml file other than the assets; for example a file that I have received or made;
Thanks

Welcome Luis.

If you have another file that you want to 'choose' from the Documents folder the file (or a copy), place the file in Assets prior to compiling the example app. To gain access to the file at the present time the only way is to include it in the Assets and then create the apk. The app doesn't access the Assets, it copies the files in assets into the Documents folder and it only reads files there that have been 'created' within the app. Blame it on Google.

  1. Copy your kml and gpx files to the Project's Media Assets. Only then they can be copied into Documents using the app. Three example files are already in Assets giving you a head start. Drag and drop several of your outside kml files into Media to make your outside files available to your app. Sorry, you can not merely copy kml/gpx files into the Documents folder and expect them to be usable with the the compiled app. Usable files must be created with the compiled app.
  2. use the Copy Assets to Documents button to copy all the gpx and kml files you placed in your Assets to the Documents folder. Otherwise, your ListPicker will not have any track files to select until you copy klm or gpx Assets there unless you use Create or Capture options and store the files created by those tools..

I am trying to get the example to read any file placed in Documents using the SAF extension. So far I have not be successful. :cry:

Copy your file to Assets; compile and use the Copy Assets to Documents to place a copy of the file into Documents. If you already have the file in Documents, rename the copy you put in Assets because otherwise the app will not be able to read it. Do it. What happens?

Right; I do that and it works great; I also tried to open another file that is not in the assets but without success; if you can; show us please; thanks for attention.

working on it. :wink: should be possible in a week or two. How to import, create and use gpx and kml track files using App Inventor Map Part II Use SAF extension to select kml/gpx files stored in your Documents folder

1 Like

Hi Steve thank you very much for this tutorial.
Do you know how to send the created linestring in app inventor (when creating an itinerary in the app) to an external server with a PUT function? My goal is to be able to collect itineraries created by the user to analyse them later in ArcGIS. For this, I need to be able to download itineraries from my application users.
Thanks in advance!

Welcome Niki.

The Created kml or gpx is a file copied to your Android documents folder (*.kml or *.gpx).
You might be able to use that file in the Documents using something like this example by Tim to move the file to your php server. The example shows how to move a png file, perhaps it will work with a kml or gpx.

Hi Steve,
Thank you for your hint, I will try this out ! :slight_smile:

1 Like