Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
wantoun
Posts: 165
Registered: ‎10-04-2010
My Device: P'9982 & Z10
My Carrier: alfa Lebanon

HTML5 GeoLocation Check Interval

My app needs to be  pulling the device's location frequently. What I was doing at first is setting this with a Javascript interval ( setInterval() ), the user would have the option to set this check interval, and the app would pull this using this function:

navigator.geolocation.getCurrentPosition(geoSuccess, geoFail, options);

With this method though, the timestamp of the GeoLocation pull was not updating unless I close the app and reopen it. It would keep the same location all the time.

 

I'm trying the Watcher function now:

navigator.geolocation.watchPosition(geoSuccess, geoFail, options);

This works perefctly, and it's updating the timestamp as the location is updated. I'm not so sure this is great for the battery though. The Location icon is always showing on the device (next to the battery icon on the top status bar).

 

I'm just looking for advise here as to what's optimal for a geolocation app, and how should I go ahead with this!?

 

Thanks.

Wadi
Please use plain text.
BlackBerry Development Advisor (Retired)
mimendoza
Posts: 64
Registered: ‎06-04-2012
My Device: Blackberry Z10
My Carrier: Bell

Re: HTML5 GeoLocation Check Interval

I would suggest to use getCurrentPosition since you can use this in combination with setInterval, which I just tested and the timestamp did change each time it pulled the geolocation information.

watchPosition will execute the callback each and every time the location changes, which doesn't seem like what you want your app to do - and yes, this would undesirably consume device resources.

I used and modified the example code here to show the timestamp (https://developer.mozilla.org/en-US/docs/DOM/window.navigator.geolocation.getCurrentPosition) and executed it on the device, in combination with setInterval() and was successful.

What does your geoSuccess function look like?
----------------------------------------------------------------------------------
- Did this post help solve the issue? Click on "Accept as Solution"
- Was the content of this post helpful? Please give it a "Thumbs Up"

developer.blackberry.com
@BlackBerryDev
Please use plain text.
Developer
wantoun
Posts: 165
Registered: ‎10-04-2010
My Device: P'9982 & Z10
My Carrier: alfa Lebanon

Re: HTML5 GeoLocation Check Interval

Did you test this on a browser or an actual device? it's the device that doesn't update the timestamp.

 

Here's my geoSuccess function:

function geoSuccess(position) {
    var coordinates = position.coords;
    var time = new Date(position.timestamp);
    var myLat = coordinates.latitude;
    var myLong = coordinates.longitude;
    window.localStorage.setItem('lastCoord', position.timestamp);
    window.localStorage.setItem('myLat', myLat);
    window.localStorage.setItem('myLong', myLong);
    myLat = myLat+'';
    myLong = myLong+'';
    var locationInfo = "("+myLat.substring(0, 6)+","+myLong.substring(0, 6)+") @ "+getDateString(time);
    log(locationInfo);
    if (document.getElementById('locationInfo') != null) {
        document.getElementById('locationInfo').setAttribute('placeholder',locationInfo);
    }
}

 

Wadi
Please use plain text.
BlackBerry Development Advisor (Retired)
mimendoza
Posts: 64
Registered: ‎06-04-2012
My Device: Blackberry Z10
My Carrier: Bell

Re: HTML5 GeoLocation Check Interval

I've tested it on a device and using the console log and it worked successfully

 

Can you try to log the output of each variable before you store them or do any manipulation with them?

 

For example, before you use local storage: console.log(coordinates); console.log(time); console.log(myLat); console.log(myLong);

 

----------------------------------------------------------------------------------
- Did this post help solve the issue? Click on "Accept as Solution"
- Was the content of this post helpful? Please give it a "Thumbs Up"

developer.blackberry.com
@BlackBerryDev
Please use plain text.
BlackBerry Development Advisor (Retired)
mimendoza
Posts: 64
Registered: ‎06-04-2012
My Device: Blackberry Z10
My Carrier: Bell

Re: HTML5 GeoLocation Check Interval

I would suggest to also run the example at the bottom of this page (https://developer.mozilla.org/en-US/docs/DOM/window.navigator.geolocation.getCurrentPosition) on your device along with setinterval and edit it to show the timestamp.

----------------------------------------------------------------------------------
- Did this post help solve the issue? Click on "Accept as Solution"
- Was the content of this post helpful? Please give it a "Thumbs Up"

developer.blackberry.com
@BlackBerryDev
Please use plain text.
BlackBerry Development Advisor
oros
Posts: 1,523
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: HTML5 GeoLocation Check Interval

Just to add to this; battery-wise, the most expensive operations for a GPS chip tend to be when going from cold to hot (i.e. a period of inactivity, followed by a a GPS request where the chip needs to search for satellites, etc.)

If a GPS location isn't requested within a certain amount of time (can vary from 10s to 3 minutes) then the GPS chip goes cold and on the next request, need to fire back up.

Once the GPS chip is hot, the battery power to sustain retrieving positions is minimal compared to the usage in going from cold to hot.

Please note that this information is based on the functionality in the legacy BBOS devices, though I believe it is general GPS chip behaviour and not dependent on the OS. For example, on BBOS, it was more battery efficient to request a position every 10 seconds, than every 3 minutes, due to the "warm-up" usage of the chip itself.

Perhaps a GPS guru could comment on this further. But it's something to keep in mind; you may want to try out both approaches and get a sense of how much battery you're going through to determine the best approach.

Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Please use plain text.
Developer
wantoun
Posts: 165
Registered: ‎10-04-2010
My Device: P'9982 & Z10
My Carrier: alfa Lebanon

Re: HTML5 GeoLocation Check Interval

Thanks mimendoza and oros and both your inputs.

 

I'm currently sort of giving both a test drive, but it would be great if I can a more definitive answer from someone who knows this stuff.

 

The JavaScript setInterval is working but it doesn't seem like it's updating all the time. What I'm thinking (and I might be wrong) is that say the user set the interval to 5minutes. The app tries to get the location, if it fails, the next attemps would be in 10 minutes,, and so on... maybe this is why I somtimes see it as not updating. I'm not sure.

 

Also, does anyone know if the HTML5 geolocation is brough from GPS or AGPS (cell towers)? Does the device determine this?

Wadi
Please use plain text.
New Developer
georgemck
Posts: 34
Registered: ‎12-28-2010
My Device: Red Z10, DevAlpha C, Bold 9900, PlayBook
My Carrier: Sprint, AT&T

Re: HTML5 GeoLocation Check Interval

[ Edited ]

Geolocation starts with AGPS on cellphones first. It needs the cell towers to orient the device to the satellites in the sky. It takes 60 to 120 seconds for the satellite and the device's GPS to start working. Using AGPS speeds the process up a lot. 

 

You can also use a 3rd party WiFi service like http://loki.com/findme (Loki/SkyHook), which is web technology. 

 

You can also use the cellphone carrier's network API services. That's usually what the police do. You can ping the cellphone towers and get a pretty good fix on the geolocation without bothering the handset. For us, however, we need to get permission from the handset to use this kind of feature, usually it's a text message. It might be free for developers for testing so long as you do not make too many location requests per hour. AT&T and Sprint definitely have APIs for this. Neustar provides a common API for many different carriers so you don't have to sign up and pay each one separately.

 

 

More info:

Loki is based on http://www.skyhookwireless.com/location-technology/sdk.php and it will work with just WiFi so long as the wifi router exists in the LatLong database of routers. You can add the router yourself to the database, too. The iPhone launched with SkyHook, later Apple created their own service and stopped using it. Google copied their model of driving around geolocating wifi routers. http://en.wikipedia.org/wiki/Skyhook_Wireless

 

 

Onward to BBJam Asia 2013!
twitter/github: @georgemck http://www.bbdevla.com
Please use plain text.
Developer
peardox
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: HTML5 GeoLocation Check Interval

I've been playing with this for a while too

 

My test app is REALLY wasteful as I send every reading to a server (which was great for analysis after a test)

 

Here are my thoughts....

 

Start off with an initial reading then adjust the update timings based on what the user is doing

 

For example....

 

I leave home in a car, stop, walk to a coffee shop, drink coffee, walk around a bit shopping, stop for a snack, return to car, drive home, vege and watch TV for 3 hours

 

You need to place max + min limits of course

 

The while concept works on distance and time

 

When I start the trip I'm moving fairly fast so I want regular updates

I start walking - updates can drop

I drink coffee - I'm not moving

Shopping - I move a bit but POI may be of interet so potentially higher than car-park to coffee shop

Snack - more zero movement

Car home - fast again

Vege in front of TV - slow

 

You can therefore adjust the GPS to the activility of the user

 

Add to this that the accelerometer is always on and you can account for undesired events to adjust the timings again

 

To calculate the distance in metres between to readings this is good

 

function latdist(lat1, lon1, lat2, lon2)
	{
	var R = 6371; // km
	var dLat = (lat2-lat1) * Math.PI / 180;
	var dLon = (lon2-lon1) * Math.PI / 180;
	var dlat1 = lat1.toRad();
	var dlat2 = lat2.toRad();

	var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
			Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(dlat1) * Math.cos(dlat2); 
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
	return R * c;
	}

 

The only problem you really have is that GPS 'bounces around' - try a simple app with direct readings, sit outside and watch it - you jump all over the place

 

There are solutions to this, of course

 

 




Click the like button if you find my posts useful!
Please use plain text.
Developer
wantoun
Posts: 165
Registered: ‎10-04-2010
My Device: P'9982 & Z10
My Carrier: alfa Lebanon

Re: HTML5 GeoLocation Check Interval

@mimendoza I've tried printing the variables like you suggested. I've noticed that the geoSuccess function isn't being triggered unless the app is opened and that's causing the problem. (at least that's the only thing I was able to pin point)

 

If I use the Geo Watcher, it works like a charm and the values are printed repeatedly, even when the app is minimized (Active Frame).

 

If I use the Javascript setInterval, nothing gets executed untill I open the app. Then I see a flood of variables being printed with the same timestamp & coordinates, but when the app is minimzied in an Active Frame nothing happens.

I have set the permission to run_when_backgrounded (the watcher wouldn't be working I guess if it wasn't)

 

Sample Output: I changed from Watcher to setInterval @ 12:51 and then minimized the app untill 13:45

May 21, 12:51: Started Location Watcher with ID 5
May 21, 12:51: (33.892,35.506) @ May 21, 12:51
May 21, 12:51: Stopped watcher with ID:5
May 21, 12:51: Started Check Interval with ID 13 to 600000
May 21, 13:45: (33.895,35.502) @ May 21, 13:45
May 21, 13:45: (33.895,35.502) @ May 21, 13:45
May 21, 13:45: (33.895,35.502) @ May 21, 13:45
May 21, 13:45: (33.895,35.502) @ May 21, 13:45
May 21, 13:45: (33.895,35.502) @ May 21, 13:45

 

 

 

@georgemck thanks for that info, I was wondering if maybe I can restrict the Geolocation to ONLY use AGPS as an accurate GPS location is not very important for my app.

 

@peardox thanks for your input on this, I appreciate this but keeping track on the user's movement isn't vital for my app - no POIs are being used.

Wadi
Please use plain text.