Switch GPS provider

Hello from Italy! (Sorry fo my bad English :grin:)

I've created an app for loading geotagged photo on my server, all is running fine but I've problem with GPS fix

If I select "network" as provider the app fix quickly but with the default "gps" provider it takes a lot of time to fix.

My questions: is goog to set "network" as default gps provider? (I will find a provider named "network" on every android device? On my device when I call availableProviders the result is: "gps" - "passive" - "gps" - "network". For sure I can check the presence of network provider before switching to it)

It will be better use "network" for fixing and then switch to "gps" to obtain better accuracy? If yes, is it possible to use two location sensors setting one on "gps" and the other on "network" and use the position with better accuracy?

Thanks a lot!

No. The accuracy of gps data is automatically determined by your GPS hardware and the number of satellites your gps can receive at any instance although you can force gps to run.

The time it takes to get a satellite fix is determined by your gps hardware, whether you have an unobstructed view of the satellites, weather conditions etc. The initial fix may take 20 to 60 seconds, subsequent fixes about every 20 seconds (or perhaps a bit less depending on your hardware).

This is probably because the LocationSensor is using WIFI to provide a position. The most accurate geocoordinates are provided by the gps + on some devices. My recommendation is not to set a Provider but let the software 'decide'.

Have you read Using the Location Sensor. The document explains it all. :slight_smile:

I don't know if you can run two instances of the LocationSensor simultaneously. Why don't you try and let us know what you find. I expect the gps will generally indicate better accuracy (that is what the literature says :wink: )

Thanks a lot!! I will going on testing..

Ok, is possible to use 2 locationsensors at same time with different providers:

So I can use the network provider (very quick fix) and then switch to default gps provider (better accuracy) when it has a fix.

Now my problem is: if I turn off the sensors

And then I ask for latlng:

The sensors send me the last fixed psotion but in my app I really need to have the opportunity to disable the location.sensors in a way that If I call the latlng they send to me the standard nofix 0,0 value.

A sort of in app reset for the locationsensors!


I do not think you can zero out the last known coordinate. See the LocationSensor documentation.
The most recently available latitude or longitude value in degrees will be reported to 5 decimal places. If no value is available, 0 will be returned. Your app provides a 'most recently available longitude or latitude has been set' so you do not get 0. There is no Method to set/reset the LocationSensor.Latitude or Longitude to 0 .

Why? To report coordinates of 0,0 do not use NETWORK_gps.Latitude/Longitude after you disabled a sensor. Simulate coordinates of 0,0 by setting a variable myLatitude or my Longitude within the LocationSensor1.LocationChanged event. Use the myLatitude/myLongitude varibles as your app's reported coordinate( instead of the last reported coordinate indicated by the LocationSensor). That is, set each of myLatitude/myLongitude to 0 after your LocationSensor1 or 2.enabled is set to false. Use the simulated coordinates for your strange requirements; not the LocationSensor.Latitude / Longitude Blocks.


My app works this way: when I take a photo I ask sensor for position and upload pics and latlng to my server.

The GPS provider when fixed offer the best accuracy but it cannot fix indoor and in some situations can take a lot of time to fix. So I decided to use two sensor at sdame time (gps and network) When I ask for the position my function choose the provider (gps or network) with best accuracy value. If I am outdoor the GPS fix and offer the most accurate position, indoor where the gps provider can't fix, the Network provider provide accetable position.

BUT when I go indoor and the GPS provider loose fix it continue to provide the The most recently available latitude or longitude and accuracy value and my function continue to choose this old position because has better accuracy compared to the "real" network position.

So I need to stop a non fixed provider to send to me the last availvabe position/accuracy because even If i turn off the sensor with the gps provider when I turn it on again it re-start to provide the most recently available latlng but this position could be miles away from the real position until the gps provider fix again, and if I am indoor it can be a serius issue.

So my requiremets are not so strange.. :grin:

Post #5 describes a solution. :slight_smile: Did you try it?

Thanks for your help, I'm doing a lot of experiment and my app works quite well now.
Now I'm testing StatusChanged and I found some strange behaviour.

From the help:
StatusChanged(text provider, text status)
Called when the status of the service provider changes.

When the GPS provider fix it call StatusChanged (status: AVAILABLE.)
The fact is that StatusChanged is called continuously and stop only if I go indoor (no GPS signal)
But the last called StatusChanged reports status as AVAILABLE.

If I go Outdoor the GPS start to call StatusChanged with this status sequence:
status:TEMPORARILY_UNAVAILABLE -> status: AVAILABLE and the it continue to call StatusChanged

It seems that when the GPS provider can "see the sky" start to continuosly cal StatusChange and stop when it loos the sky-view

Any ideas?

You must do the experimenting. What happens depends on how you coded your Blocks and you did not share them.

Locking the Provider and simultaneous use of the StatusChanged block probably requires a lot of experimentation and might result in 'conflicts' I can only guess at.

If you Lock the provider, I expect you must unlock it before before you should expect a StatusChanged event to trigger. If you set gps and locked the provider to gps and you 'lose the sky-view', StatusChanged probably will not trigger. Your app will attempt to obey the logic rules you established. If your app is depending on using WIFI and you move outside and your status is not locked, the LocationSensor should allow the gps to function as soon as it records a LocationChanged event; however even then I suspect there will be some delay before the gps will provide an updated coordinate. The device has to obtain a satellite fix before it will again allow the gps coordinates to post. This may happen in a few seconds or may take a minute.

No one can provide you with a specific reason for the behavior you observe without seeing what you coded. Even having a copy of your app might not provide a guess as to what you might do to get the performance you hope. Chane things and TEST your app's behavior. What happens w.r.t. StatusChanged depends on what is coded; your device's gps hardware (how many channels the gps receiver has, how sensitive the receiver, the TimeInterval) and possibly the Android version.

Keep on experimenting. Can you do what you want? Maybe. A simple solution is to provide a Button that enables the gps only when you are outside and which can be used to toggle and set a different provider when you are inside manually.

The MIT LocationSensor can only do what it does. Third party developers posted several extensions that allow the developer to monitor other things using versions of LocationSensor that are able to use features of the gps constellations that became available only after the MIT Location control was added to the original App Inventor sometime around 2012/13. One of these extensions measures signal strength from the satellites. How might this feature be useful? .When the signal strength is low it may mean your user moved indoors, if the signal strength gets low, switch providers perhaps and another provider might provide less accurate coverage. Another extension can be used with more satellite constellations. If the gps receiver can read the information from more satellite constellations, it may be able to provide more accurate coordinates and possibly the ability to render location changes more rapidly or utilize weaker satellite signals to provide a satellite fix.

Experiment some more! The alternative is to switch to a professional compiler like Android Studio that can give you access to the newer features of the satellites.

You might find this link useful https://www.google.com/search?q=use+a+gps+indoors&rlz=1C1CHBF_enUS887US887&oq=use+a+gps+indoors&aqs=chrome..69i57.5088j0j7&sourceid=chrome&ie=UTF-8