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.
How developers use kml and gpx files to distribute/display map tracks is Using GPX & KML files for hiking | Hike Navigation
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
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.
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.
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.
You may want to Delete existing kml and gpx files from the Android Documents folder. Use a File manipulation app (I use My Files);
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.
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.
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.
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.
to display tracks on App Inventor
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:
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.
- 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
Capture 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.
- 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.
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:
Taifun File extension App Inventor Extensions: File | Pura Vida Apps . (I unsuccessfully tried using the File component Legacy/Shared Scoped method.)
[FREE]DjGPXTool extension [FREE]DjGPXTool extension - Extensions - Kodular Community (this is a Kodular extension that works with App Inventor; be aware the example extension aia pointed to does not work with App Inventor.
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.
The app was only tested on a Samsung A13, Android 13. How it will perform with different Android versions and devices is unknown.
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.
Are there bugs? I hope not but one never knows. I did not code all error control possible.
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).