B4J Question [ABMaterial] [Browser side-GPS-Location] How possible can be to use GPS through a browser + quality ?

Magma

Expert
Licensed User
Longtime User
Hi there...

my latest tries and thoughts at webapps coming to "full" our day :)

So in a scenario - that we have a server and keeps "tracking" of users locations (that changing realtime)... without the users having android apps but only browser supports ofcourse running HTML5/webapps... (i think the most can do)...

How it is is possible using ABMaterial and B4j Jetty to get the client location every second or two...

is just a scenario - not a ready project...
at the side of server... running a b4j project with ABMaterial, jServer and a page builded properly that will include somehow a javascript (? is it possible at ABMaterial?) like this and refresh (may be with a timer from b4j asking for it) every 1-2 seconds...

the javascript needed to included at the page:
B4X:
<script>
var x = document.getElementById("inp1");     'coords will be get into input box...

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude +
  "<br>Longitude: " + position.coords.longitude;
}
</script>

1. So here comes the question ... as i understand the BANano is closer to those solutions - but can we pass this javascript with ABMaterial somehow ?
2. Anyones know the quality(accurate) of Location will get with the java at a browser...
is somehow like FusedLocation (if we have open GPS or not using at all the GPS)... can we use it every second or will have any problem - anyone tried it ?

Thanks in advance
 

alwaysbusy

Expert
Licensed User
Longtime User
Probably the easiest way to do this would be writing a small ABMBANano Library. This is a library written in BANano, where BANano generates the 'glue' code for ABM. A full tutorial is here: https://www.b4x.com/android/forum/t...aking-banano-libraries-for-abmaterial.126828/

The relevant code in BANano would be something like this (untested, follow the full tutorial in the first link and how to use it in ABM!):
B4X:
#Event: NewLocation(lat as double, lng as double)

Sub Class_Globals
    Private BANano As BANano 'ignore
    Private mEventName As String
    Private MyTimer as Timer
End Sub

'Initializes the object, setting the event
Public Sub Initialize(eventName As String)
    mEventName = eventName
End Sub

Public Sub Start()
     MyTimer.Initialize("MyTimer", 2000)
     MyTimer.Enabled = true
End Sub

public Sub MyTimer_Tick()
     GetLocation
End Sub

Sub GetLocation()
    Dim args As List
    Dim trackSuccess As BANanoObject = banano.CallBack(Me, "locationFound", args)
    Dim trackError As BANanoObject = banano.callback(Me, "locationError", args)
   
    banano.GeoLocation.GetCurrentPosition(trackSuccess, trackError)
End Sub

Sub locationFound(position As BANanoGeoPosition)   'ignoredeadcode
    Log("locationFound")
    Dim lat As Double = position.Latitude
    lat = banano.parseFloat(lat)
    
    Dim lng As Double = position.Longitude
    lng = banano.parseFloat(lng)

    BANano.RaiseEventToABM(mEventName & "_NewLocation", Array("lat","lng"), Array(lat, lng), "")
End Sub

Sub locationError(error As Int)                'ignoredeadcode
    Log("locationError: " & error)
End Sub

Alwaysbusy
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Wow you are very fast ! :)

....So the best way is creating a library when you want to uses some javascripts ?
or is that...
https://www.b4x.com/android/forum/threads/abmaterial-b4js-03-inline-javascript.90305/#content
in that case you are saying actually must you "banano"... for getting api of geolocation like that:
B4X:
    Dim args As List
    Dim trackSuccess As BANanoObject = banano.CallBack(Me, "locationFound", args)
    Dim trackError As BANanoObject = banano.callback(Me, "locationError", args)
  
    banano.GeoLocation.GetCurrentPosition(trackSuccess, trackError)
Like a javaobject ... hmmmm

..there are many examples and different views...

In case you suggest... i create the library... and get a .b4xlib file... i am adding the file to libraries and then.. at the ABMaterial use BANano library too and my new library... or only the new lib ?
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
I would use BANano to do this. B4JS is not as powerful compared to BANano.

or only the new lib
Follow the full tutorial, but you do not need BANano any more then, as the B4J code will already be transpiled to pure javascript by the
BANano.BuildAsB4XlibForABM("K:\_ONETWO210203\Work2020\Objects\www", "1.0") command. You only have to run this once to make the ABMBANano lib (in Release mode!), and then just include this one in your ABM project. Just follow the instructions in the .txt file BANano will generate next to the lib.

Alwaysbusy
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Thank you very much... i will make some "cases" to see how all these work --- i am not having any project to run so i have the time to search it.

How about the accuration of GeoLocation (using it at HTTP)...
1) is it accurate ?
2) have speed - getting fast the coords ?
3) any other api of it... getting the speed (m/s)... or something else ?
4) navigator.geolocation.watchPosition ... difference ?
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
is somehow like FusedLocation (if we have open GPS or not using at all the GPS)... can we use it every second or will have any problem - anyone tried it ?
You have now tabled a question I have always been afraid to ask - considering the knowledge required - first, to pose such a question for a qualified / simple response.
When using a B4A app (for example), you get all the GPS stuff u can eat (every second - or less - fused / base / however (if GPS is ON)). However, (the question is), on an Android "Browser" (ie Chrome) app (BANano / ABM) , can one get close to this? I assume the browser window must be foreground / active, since the concept of browser Services (as background services - always running) are not available...?

Very interesting indeed....
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
ofcourse.. must be in foreground ... or you must use with some api per browser (that is the bad)... to wake up - wake lock...

but my question is more about "how accurate" - because i am feeling.. that may be browser check the ip too... so a mobile device not use only GPS coors but IP coords too :-( that is I am afraid... or when using GPS enabled device there is no problem ?

Is more browser/client side problem but is "A problem"
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Accuracy will probably be determined in this order:

1. GPS: if the device has GPS capabilities and if it was able to get a location from the satellites
2. Mobile Network Location: if the device is using e.g. 4G
3. Wi-Fi Positioning System: Some Wi-Fi spots have location services capabilities, but I haven't seen a lot in the field which can
4. IP Address Location: what you will get from a PC, but can be the ISP router IP. E.g on my PC at home, location is about 1200m off.

BANanoGeoPosition has an Accuracy property, which I think returns the accuracy in meters.

Alwaysbusy
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
To add to what AB said, I have used this for a train tracking app. Its very important that your device has GPS otherwise it will pick the location of your ISP. Im in East London and mine in Cape Town at times, 1000KM away. Lol. Still baffled why my laptop reports CT at times. If your devices also looses a data connection, well RSA has some very poor data connections, then you are at odds for that time.

Anyway, what I like about the BANanoGeo stuff was that besides a timer you can add, it has functionality built in to track LatLon changes. This is the strong point of the app I worked on as I dont need a timer, but each time the LatLon changes I just record it, fire a firebase message after.

All the best.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
To add to what AB said, I have used this for a train tracking app. Its very important that your device has GPS otherwise it will pick the location of your ISP. Im in East London and mine in Cape Town at times, 1000KM away. Lol. Still baffled why my laptop reports CT at times. If your devices also looses a data connection, well RSA has some very poor data connections, then you are at odds for that time.

Anyway, what I like about the BANanoGeo stuff was that besides a timer you can add, it has functionality built in to track LatLon changes. This is the strong point of the app I worked on as I dont need a timer, but each time the LatLon changes I just record it, fire a firebase message after.

All the best.
Nice.... Got a snippet one could hack into a ABM app?
Thx..
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Nice.... Got a snippet one could hack into a ABM app?
Thx..
I havent done BANano to ABM, however, as an example, getting the current location:

B4X:
Dim position As BANanoGeoPosition
    Dim error As Int
    Dim poscb As BANanoObject = banano.CallBack(Me, "foundLocation", Array(position))
    Dim errcb As BANanoObject = banano.CallBack(Me, "didnotFindLocation", Array(error))
    'we have to get the current location
    Dim geopos As BANanoGeoLocation
    geopos.GetCurrentPosition(poscb, errcb)

and

B4X:
Sub foundLocation(position As BANanoGeoPosition)            'ignoreDeadCode
    Dim lat As String = position.Latitude
    Dim lng As String = position.Longitude
End Sub

Sub didnotFindLocation(error As Int)                    'ignoreDeadCode

End Sub

Its really well documented on the BANanoDocs


All the best..
 
Upvote 0
Top