I have written an app which uses GPS data. On my old phone (Android 8.1, bought in 2018) it works perfectly. But I have recently bought a new phone (Android 13) and sometimes (not always) the GPS location data is wrong by up to about half a mile. Google maps always gets the location right, so why can't App Inventor and my new phone be more reliable? Any help much appreciated!
It can. Your Android 13 is using the 'fused' Provider introduced in Android 10 which uses cell tower and WIFI location in addition to gps hardware (not as accurate) to determine coordinatesby default when used with the LocationSensor. You probably noticed that your old app doesn't immediately show the gps icon it shows immediately on your Android 8.
Always? Not really D(won't get into that).
Google uses gps all the time and uses all the satellite constellations available on the new phone to improve Accuracy. The MIT location sensor only makes use of the original satellite constellation. LocationSensor Accuracy can be improved on newer devices if the developer provides code to force gps and always use gps hardware instead of automatically switching between fused and gps providers which in some instances defaults to fused.
Set LocationSensor.Provider to gps and immediately Lock the Provider might work for you. You may need this advice GPS with Android 12
Many thanks for the suggestion which helps because I don't get any big (half mile) errors. However when I installed the new version I got a Runtime Error: "uid 10355 does not have android.permission.ACCESS_COARSE_LOCATION or android.permission,ACCESS_FINE_LOCATION." When I ended the application and started it again it kept on changing the location every few seconds by up to about 40 m. I think the the location permission for both versions of the app is set to "Use precise location" although the toggle switch lacks the normal blue bit so I'm not sure. How can I persuade it to give me a more precise location?
It would really help if you provided a screenshot of your relevant blocks, so we can see what you are trying to do, and where the problem may be.
To get an image of your blocks, right click in the Blocks Editor and select "Download Blocks as Image". You might want to use an image editor to crop etc. if required. Then post it here in the community.
Taifun
Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.
As Taifun suggested, share your Blocks. What needs to be coded depends on what you coded.
What did you set the LocationSensor.TImeInterval to?
'by up to 40 m' is the expected behavior. You might read the section in Using the Location Sensor regarding Accuracy. What Accuracy you get depends on the number of satellites used in the satellite fix, whether you are inside or outside, whether it is raining etc. The most likely usual accuracy is about 10 for most devices, ranging from about 2 on occasion to 40 or 50.
????
Don't use the LocationSensor, use one of the extensions perhaps.
or
Google Maps and the Location Sensor (running simultaneously to achieve better Accuracy) - #5 by SteveJG might work for you but it is an awkward way to use your app.
At your suggestion I added these blocks:
Looking at the code (which I wrote some time ago) I've set both time and distance interval to 0: I can't remember why.
As far as I can tell at the moment the blocks above prevent the big (half mile) errors I was getting with the original version of the app - which is the main problem. But I would like to understand why the app works perfectly with Android 8.1 but not Android 13. I'll have another look at the section on the location sensor.
Great.
Google did not implement the 'fused' LocationSensor Provider until about Android 10. Android 8 did not have that feature. It works the way it does is that is house Google wants users to make use of the 'fused' feature. in apps about Android 10+. Fused Location Provider API | Google for Developers . Your old app is six years old. Time and coding rules march on.
Regarding compiling your app for use on Android 13, and building the apk, you will have to have some Blocks like
to allow Android to set the Location permissions. You also need a Clock.TImerEnabled to true Block in your Screen1.Initialize event handler.
You only can set the provider to gps snd lock it if it is available in Available Providers
https://ai2-appinventor-mit-edu.ezproxy.canberra.edu.au/reference/components/sensors.html#LocationSensor
AvailableProviders
List of available service providers, such as gps or network. This information is provided as a list and in text form.
directly in Screen.Initialize it still might be too early to do that...
Taifun
gps is always available if a device has gps hardware. Virtually all newer Androids do.
On an Android 13 the available Providers will probably be
And what happens, if gps is deactivated in the settings?
Taifun
I've just been out on my bike with both versions of the app running (with and without the two extra blocks). Both versions tracked the road I was riding along to within 2 m for several miles. I suspect the problems arise in particular places. Thanks very much for all your suggestions which I will study in detail when I have time, and probably make a third version of the app with the facility for the user to change the location settings.
While you are testing you might try this as well
I am trying to get permission for FineLocation but have not succeeded. The blocks below produce a runtime error and the user is not asked to give permission. The same happens if I put the Call Screen 1 AskForPermission block in the Initialise block. What am I doing wrong??
What I do
Remember, you must BUILD your app toe get FineLocation permission. Depending on what ever bells and whistles your app uses, it will need additional code.
I've tried lots of things but nothing works. The best solution is the first from SteveJG which just involved adding two blocks to the initialise block:
It did produce a runtime error when first opened but it was ok next time. And the location jiggles around by up to about 40m - feels like it's coarse location not fine location. I still can't see how to get FineLocation and none of my efforts resulted in the user being asked for permission as the block implied should happen.
Presumably at some stage the AppInventor team will fix this problem? As one of my kind advisors said programming moves on, but it's just a pity that the motion is in the direction of a deterioration not an improvement!
Many thanks to everyone for their help.
This is the way the gps works. Read about Accuracy in Using the Location Sensor. What you get depends on how many satellites are used by the gps to provide a location fix.
Did you build the app? You need to build the app. During the install process the user is given the opportunity to use coarse or fine. Subsequent use, the setting is remembered.
I don't believe there is a problem. The LocationSensor uses one satellite constellation for a satellite fix. Your phone can use multiple constellations but an app built with the present LocationSensor component uses only one. To use more satellites, you can use an extension. As mentioned previously, Accuracy depends on many factors. Typically Accuracy is 10 m , occasionally 2 and up to 40 m or so.
Not happy, filter out satellite fixes that are more than 20 or whatever.
In addition to all have been already said, please consider that there is a very good extension made by @Juan_Antonio ( ), called "com.KIO4_NmeaMessage.aix"
(You can find it on his "KIO4"web site).
I've used it to get raw data from the GPS sensor as fast as possible (the related accuracy depends anyway on how many satellites are linked).
By using this extension and by parsing the NMEA183 message you can extract the data you need (i.e. Lat, Lon, Time,.......).
Best wishes.
Yes, but the error is occasionally up to half a mile! I've got one comment and two questions.
(1) My app works perfectly with Android 8.1, but not Android 13? I appreciate that Android 13 does something differently, but it seems a pity that it's worse than the previous version.
(2) There is an AskForPermission block which I have tried to use. But I, as the user, have never been asked for permission, which suggests to me this block is not working.
(3) Why isn't there a SetPermission block in App Inventor?