B4A Library Expanded Location Manager Library

Attached is an expanded version of XverhelstX excellent LocationManager library.
It adds new methods and extra functionality to the existing methods. There are many other methods as detailed on Android Dev which can be easily added if required.
The two significant new methods are "findLastLocation" & "addProximityAlert"
findLastLocation is handy when there is no network or gps (bear in mind the position may be way off) it can also return a location from 'passive' provider, this position is updated any time an app requests a position.
ProximityAlert generates an event anytime you cross the boundary as defined by a position and a radius, there are two different events; Enter & Exit.
RequestLocation (Network & GPS) should only be started once each, onResume they do not need to be called again as the Listener remains until the app is killed or the Stop method is called.
esLocation2
Author: Edward Smith
Version: 1
  • ESLocation2
    Events:
    • Locationchanged ( )
    • Providerdisabled ( )
    • Providerenabled ( )
    • Proximityenter ( )
    • Proximityexit ( )
    • Statuschanged ( )
    Methods:
    • EndProximityAlert
      Removes the ProximityAlert pending intent
    • Initialize (EventName As String)
      Common initialisations
      Example:<code>
      Dim MyLocation As ESLocation2
      MyLocation.Initialize("Location")</code>
      Can raise the following Events
      Locationchanged_Location
      Providerdisabled_Location
      Providerenabled_Location
      Statuschanged_Location
      Proximityenter_Location
      Proximityexit_Location
    • addProximityAlert (Radius As Int, Lat As Double, Long As Double, Life As Long)
      Add Proximity Alert
      This method uses both Network and GPS to trigger the Alert, if there is no GPS
      fix then the Network fix will be used and will not be as accurate. This is a
      function of Android and cannot be changed.
      Radius: in meters
      Lat: positive values for north
      Long: negative values for west
      Life: this is the life of the pending Intent in milliseconds, use -1 for no expiration
    • findLastLocation (providerType As String) As List
      Returns the Last Known Location in a 'List' containing:
      provider type
      Time of fix
      Latitude
      Longitude
      Accuracy
      Pass null to get a 'List' of all providers details
      providerType: This can be 'network', 'passive', 'gps' or null
    • requestGPSLocation (mintime As Long, mindistance As Float)
      Starts a GPS Listener for position updates
      See the following Android Dev site <link>requestLocationUpdates|LocationManager | Android Developers, long, float, android.location.LocationListener)</link> for details
      mintime: minimum time interval between location updates, in milliseconds pass 0 for continuous updates
      minDistance: minimum distance between location updates, in meters pass 0 for continuous updates
    • requestNetworkLocation (mintime As Long, mindistance As Float)
      Starts a Network Listener for position updates
      See the following Android Dev site <link>requestLocationUpdates|LocationManager | Android Developers, long, float, android.location.LocationListener)</link> for details
      mintime: minimum time interval between location updates, in milliseconds pass 0 for continuous updates
      minDistance: minimum distance between location updates, in meters pass 0 for continuous updates
    • stopGPSListening
      Stops GPS Listener
    • stopNetworkListening
      Stops Network Listener
    Permissions:
    • android.permission.ACCESS_COARSE_LOCATION
    • android.permission.ACCESS_FINE_LOCATION

v1.1 added, no change to the methods but full annotation added to the @Events.

v1.2 sorted 'findLastLocation(String)' method, this now returns the request list based on the sent parameter e.g. 'network', 'gps', 'network' or null for all.
 

Attachments

  • esLocation2 V1.2.zip
    6.6 KB · Views: 1,874
  • ProximityAlert Demo.zip
    10.9 KB · Views: 1,574
Last edited:

Azhar

Active Member
Licensed User
Longtime User
This is a very good extended suite of added functionality to the location manager.

Thanks heaps for this.

Azhar
 

corwin42

Expert
Licensed User
Longtime User
Thanks for this library.

Just one comment:

Can you please add @Events Annotation with full parameters for the Events that get fired? Without them it is a pain to write the right syntax for the event subs.
 

lagore

Active Member
Licensed User
Longtime User
Version 1.1 added to #1 post, no change to the code but full annotation added to the Events sub so if the sub is created with the auto complete all of the parameters are included (thanks for the pointer Corwin42 I never knew we could do that)
 

bluedude

Well-Known Member
Licensed User
Longtime User
What do the events return?

Hi,

Which data do the proximity events return? That is not in your documentation.
 

lagore

Active Member
Licensed User
Longtime User
There is no data returned on the proximity event. When a proximity event is setup a center and radius is specified, when you cross this radius either the enter or exit event is triggered depending if you are heading towards or away from the center. In these events your code will then handle whatever you want to do.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Hi,

Would it be possible to set multiple proximity alerts at once? If so, how to end a specific proximity alert?
 

lagore

Active Member
Licensed User
Longtime User
I think you could set more then one proximity alert but there is no way to distinguish between the returned intent as they will all have the same application context. I am not sure if the pending intent can be modified to add an indicator, if some of the experts on intents know otherwise they might let us know.
 

viljemto

Member
Licensed User
Longtime User
Hi,

thank you for library. I would like to ask, if somebody has any luck with "Statuschanged"? With me it does not get trigger.

Thanks. With regards.
 

corwin42

Expert
Licensed User
Longtime User
Hi lagore,

Can you please make a change to the findLastLocation method so that if I give a provider as parameter that only this provider will be requested? I want to use only android.permission.ACCESS_COARSE_LOCATION as permissions for my app. But the findLastLocation method always requests last location for all providers and throws a permission denied exception.

BTW: If I see it correct the methed always returns info for all providers, regardless of given parameter.
 
Last edited:

lagore

Active Member
Licensed User
Longtime User
I sorted out the findLastLocation(String) (updated library in first post) it will return the correct list based on the requested parameter. It is not possible to make the permission conditional (I think) when the library is compiled the permission is built into the XML file for that library. If you are only using the 'network' request then you can delete the " ACCESS_FINE_LOCATION " permission from the XML file and it will not throw an exception.
 

lagore

Active Member
Licensed User
Longtime User
What I meant about the permission is in a library the permission is set, you cannot create a library with a modifiable permission, only after with either the manifest editor or take it out of the library XML doc.
 

javiers

Active Member
Licensed User
Longtime User
Sometimes it not shows the position of the cell ...

First, congratulations for your library.

Signal having a 100%, sometimes not displayed the cell location of the tower. Gives no error message.

If the information is provided by the phone tower to which the phone is registered, why not get the location?
 

lagore

Active Member
Licensed User
Longtime User
Unfortunately there is nothing you can do about a cell tower not reporting the position, sometimes even google maps cannot find its current location.
 

javiers

Active Member
Licensed User
Longtime User
Thanks for your answer.
Another question:
What is the difference between network and passive? Both give the same location.
 

lagore

Active Member
Licensed User
Longtime User
Passive is a special location provider, any time an app on your device request a location either GPS or Network, Passive is updated with this location. This is why it always matches your requested position. So Passive is a quick way to get the last requested position without having to start either gps or network bearing in mind the device may have moved away from this location. It is explained in android dev
 

romario11

Member
Licensed User
Longtime User
little sample

heii

can you post a simple example for use this library ¿??
se thanks you !!!
 

andre.astafieff

Member
Licensed User
Longtime User
'm not having results with the code below, what would be the mistake?:sign0013:

B4X:
Sub Process_Globals

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

   Dim Localit As ESLocation2 
   Localit.Initialize("Location")
        Localit.requestNetworkLocation(0,0)
   
   
End Sub
Sub Locationchanged_Location(Longitude As Double, Latitude As Double, Altitude As Double, Accuracy As Float, Bearing As Float, Provider As String, Speed As Float, Time As Long) 
   Msgbox(Longitude,"Longitude")
End Sub
 
Last edited:
Top