-
16 Jul 2012 7:10 PM #1
Update data based on geolocation
Update data based on geolocation
I'm working on my first app, been on it for a while. I want the app data store to update whenever the mobile device is moving or, if the device is idle, update on a timer. It might seem odd to do both, but the timer could run every 20 minutes or so, while if the device updates when moving it might update multiple times in 20 minutes. I've tried several different things, like watchPosition and setInterval, with no luck.
Here are my current functions from the controller:
On a side note, I want my app to have the option to run in the background like a windows service. I don't know what that is called in terms of iphone or android, but I want my app to be able to pop up under certain conditions with a either the app or a window with a link to open the full app. Any ideas? I could use them.Code:getLocation: function(callback) { if (navigator && navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { callback(position); }, function(error) { // give a warning for error }); } }, getItems: function(location, callback) { var store = Ext.data.StoreManager.lookup('ItemStore'), url = 'http://myapp.co/searchapi.js' + '?distance=20' + '&lat=' + location.coords.latitude + '&long=' + location.coords.longitude; store.getProxy().setUrl(url); store.load(function() { callback(store); }); }, launch: function() { var me = this; Ext.Viewport.setMasked({ message: 'Loading...' }); // get the location... me.getLocation(function (location) { // get Items me.getItems(location, function (store) { // then bind data to list and show it me.getDataList().setStore(store); Ext.Viewport.setMasked(false); }); }); }
Thanks
-
17 Jul 2012 1:59 AM #2
Hey, not sure if this helps. But I build a tiny app where I use the locationUpdate.
Here is the code
Unfortunately, there seems to be a bug with the sencha sdk using the GPS location. So the location is more based on network and wifi. I did a workaround with phoneGap to use the accurate GPS location.Code:geo = Ext.create('Ext.util.Geolocation', { autoUpdate: true, allowHighAccuracy: true, frequency: 5000, timeout: 30000, listeners: { locationupdate: function(geo) { // Do something with the location, I print it on the screen here Ext.getCmp('positionLabel').setValue('Lat: ' + geo.getLatitude() + ' Lon: '+geo.getLongitude()); Ext.getCmp('accspeed').setValue('Acc: '+geo.getAccuracy() + ' Speed: '+geo.getSpeed()); // or set a marker at the current location var map = Ext.getCmp('map'); latlon = new google.maps.LatLng(geo.getLatitude(), geo.getLongitude()); marker = new google.maps.Marker({ position: latlon, map: map.getMap() }); }, locationerror: function(geo, bTimeout, bPermissionDenied, bLocationUnavailable, message) { if(bTimeout){ alert('Timeout occurred.'); } else { alert('Error occurred.'+message); } } } });
I'm not sure if you can realize your app as a background service. But would be happy if anybody has a idea.
-
17 Jul 2012 1:24 PM #3
So do I just drop that chunk into the launch function?
-
17 Jul 2012 11:24 PM #4
Not sure if it runs in the launch or initialize function. Just try it. I have this triggered on a button handler. So the user can decide when the tracking starts and when it stops.


Reply With Quote