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,685
  • ProximityAlert Demo.zip
    10.9 KB · Views: 1,396
Last edited:

andre.astafieff

Member
Licensed 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



I found the solution.

Debugging was on my tablet (android 4.0.30, so I had to go into "Settings"> Location Services and enable the options Google Location Service and Location and google search.

I also changed the way of calling the callback function of location.

Sub Location_Locationchanged (The Double Longitude, Latitude As Double, Height As Double, Accuracy The Float The Float Bearing, Provider As String, Speed ​​The Float, The Long Time)
Msgbox (Longitude, "Longitude")
end Sub


Now it works. :sign0060:
 

marcel

Active Member
Licensed User
Hi,

I am new this lib and the location manager. So forgive me when I ask silly questions.

But why need the Location Services enables when you select Network Position Updates in the Demo program. This means that the GPS is still active and consume a lot of power? Correct?

If not how can you see the differences? Or is in my case there no alternative in my neighbourhood and will the GPS selected?
 

Thomas Philip C

Member
Licensed User
HI,

How can i clear an old location that was found . my app seems to record the last location. how would i make the app get the new location instead. (based on network network).

regards,
Tom
 

siddsg

Member
Licensed User
Hello,
I want to be able to detect if the "My Location > Use Wireless Networks" is enabled or not.

Does anyone know how to do that?

Thanks,
Siddharth
 

desbiens

Member
Licensed User
Hello,
On all my phones/tablets I am able to get a proper current wifi location except with one of the galaxy nexus. It gives me my last wifi location which is very far away. How do I get my current wifi location for this phone? All the location settings on the galaxyNexus are ON.
 

romario11

Member
Licensed User
Hi,

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


hi,
can you set multiple proximity alerts ? im trying and i dont know it :(

I think it could be done setting multiple

dim location1 as esLocation
dim location2 as esLocation
dim location3 as esLocation
dim location4 as esLocation
dim location5 as esLocation
dim location6 as esLocation

and
location1.addproximityAlert(meters,lat,long,-1)
location2.addproximityAlert(meters,lat,long,-1)
location3.addproximityAlert(meters,lat,long,-1)
location4.addproximityAlert(meters,lat,long,-1)
location5.addproximityAlert(meters,lat,long,-1)
location6.addproximityAlert(meters,lat,long,-1)

and the same events but this is a great fudge ¿?


:(

i need help
 

hookshy

Well-Known Member
Licensed User
If both network listener and gps listener are enabled, Location_changed event is raised both times from the two providers ?
Listening position in this case would be not so accurate if from time to time you receive a location with a bad accuracy.
Do I have to use lm.stopnetworklistening all the time when I use GPS ??
 

Shay

Well-Known Member
Licensed User
I am getting error on line 171 when trying to run the demo
WHILE GPS IS OFF...

B4X:
Error occurred on line: 171 (main)
java.lang.IndexOutOfBoundsException: Invalid index 11, size is 10


    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
    at java.util.ArrayList.get(ArrayList.java:304)
    at anywheresoftware.b4a.objects.collections.List.Get(List.java:117)
    at com.edsmith.LocationRange.main._updatelocation(main.java:388)
    at com.edsmith.LocationRange.main._activity_create(main.java:347)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:305)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
    at com.edsmith.LocationRange.main.afterFirstLayout(main.java:98)
    at com.edsmith.LocationRange.main.access$100(main.java:16)
    at com.edsmith.LocationRange.main$WaitForLayout.run(main.java:76)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4921)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
    at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
 

lock255

Well-Known Member
Licensed User
I am getting error on line 171 when trying to run the demo
WHILE GPS IS OFF...

B4X:
Error occurred on line: 171 (main)
java.lang.IndexOutOfBoundsException: Invalid index 11, size is 10


    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
    at java.util.ArrayList.get(ArrayList.java:304)
    at anywheresoftware.b4a.objects.collections.List.Get(List.java:117)
    at com.edsmith.LocationRange.main._updatelocation(main.java:388)
    at com.edsmith.LocationRange.main._activity_create(main.java:347)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:305)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
    at com.edsmith.LocationRange.main.afterFirstLayout(main.java:98)
    at com.edsmith.LocationRange.main.access$100(main.java:16)
    at com.edsmith.LocationRange.main$WaitForLayout.run(main.java:76)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4921)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
    at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
I also have your same error, how did you solve it?
 

Shay

Well-Known Member
Licensed User
Don't remember, just don't use this part of the code
(I think there isn't any 11 in this list)
or check list size
 

lock255

Well-Known Member
Licensed User
Don't remember, just don't use this part of the code
(I think there isn't any 11 in this list)
or check list size
I have not used, but accuracy of the coordinates is bad, you know a way to get delel coordinated through the network, very accurate?
 

sunish

Member
Licensed User
I also have your same error, how did you solve it?
The error happens when there is no data available for a particular location provider, the hard coded array index goes out of bounds.
The following code in Update Location should prevent the error.

B4X:
    If myList.Size >=4 Then
    netTimeTxt.Text = DateTime.Date(myList.Get(1)) & " " & DateTime.Time(myList.Get(1))
    netLatTxt.Text = NumberFormat(myList.Get(2), 0, 9)
    netLongTxt.Text = NumberFormat(myList.Get(3), 0, 9)
    netAccTxt.Text = NumberFormat(myList.Get(4), 0, 9)
    Else
    Log("No Net location data available")
    End If
   
    If myList.Size >=9  Then
    passiveTimeTxt.Text = DateTime.Date(myList.Get(6)) & " " & DateTime.Time(myList.Get(6))
    passiveLatTxt.Text = NumberFormat(myList.Get(7), 0, 9)
    passiveLongTxt.Text = NumberFormat(myList.Get(8), 0, 9)
    passiveAccTxt.Text = NumberFormat(myList.Get(9), 0, 9)
    Else
    Log("No Passive location data available")
    End If
       
    If myList.Size >=14  Then
    gpsTimeTxt.Text = DateTime.Date(myList.Get(11)) & " " & DateTime.Time(myList.Get(11))
    gpsLatTxt.Text = NumberFormat(myList.Get(12), 0, 9)
    gpsLongTxt.Text = NumberFormat(myList.Get(13), 0, 9)
    gpsAccTxt.Text = NumberFormat(myList.Get(14), 0, 9)
    Else
    Log("No GPSlocation data available")
    End If
 

Avansys

Member
Licensed User
Does it work if I use it in a service?
 
Last edited:

Peter van Hoof

Member
Licensed User
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

First of all thanks for the excellent library. there seems to be an error in the initialize info. The subroutines called are Location_locationchanged etc this is also incorrect in the tooltip.

Peter van Hoof
 

Peter van Hoof

Member
Licensed 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

I think there is an error in the documentation. If you use Localit.Initialize("Location") the subroutine fired for changed location is Location_Locationchanged instead of Locationchanged_Location

Peter van Hoof
 

Glenn1963

New Member
Licensed User
For findLastLocation, are the results sorted, hopefully by Time of Fix Desc?

Also, can you direct me to some documentation on ArrayLists? ie. the best way to process them?

Thanks!
 
Top