Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

GPS Location Update Frequency
Started by gjaman Jul 07 2013 09:35 PM

8 replies to this topic
gps events
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

gjaman

[GLOBAL: userInfoPane.html]
gjaman
  • Enthusiast

  • 35 posts
  • Corona SDK

Does anyone know how often or when gps location events are generated from the GPS hardware?

 

I usually get one as soon as I add the event listener, but didn't see any documentation indicating when to expect subsequent updates.  If it is based on changes in coordinate, does anyone know the amount of change required to trigger an update?

If I want to make sure I have the correct location, do I need to add the event handler right before needing the info?

 

Thanks!

 

 



[TOPIC: post.html]
#2

FoxB

[GLOBAL: userInfoPane.html]
FoxB
  • Enthusiast

  • 37 posts
  • Corona SDK

I normally do not post on forums, but I was stuggling with this issue as well. I've spend hours on it already >.<

 

I have done a test in the simulator to update its GPS more than just once, looked promising, but I still have to test it on my Android. Each time you press the button its GPS gets updated.

 

Anyway:

Try using this technic http://docs.coronalabs.com/api/type/EventListener/addEventListener.html

with gettings it longtitude http://docs.coronalabs.com/api/event/location/longitude.html

 

I've read on the internet that you should not update its GPS too often, it might drain your battery.

 

I'll give you a start: ;-)

 

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

local locationHandler = function( event )
    -- Check for error (user may have turned off Location Services)
    if event.errorCode then
        native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} )
        print( "Location error: " .. tostring( event.errorMessage ) )
    else
        print('GPS location update')
    end
end



-- Events
local addListener1, addListener2        -- forward references

-- create a large button
local rect = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
rect:setFillColor( 128, 64, 64 )

-- state1
function state1Cb( event )
    print("state1")
    Runtime:removeEventListener( "location", locationHandler )
    Runtime:addEventListener( "location", locationHandler )
    
    rect:removeEventListener( "tap", state1Cb )
    timer.performWithDelay( 1, addListener2 )   -- ** Do this instead
    return true   
end

-- state2
function state2Cb( event )
    print("state2")
    Runtime:removeEventListener( "location", locationHandler )
    Runtime:addEventListener( "location", locationHandler )
    
    rect:removeEventListener( "tap", state2Cb )
    timer.performWithDelay( 1, addListener1 )   -- ** Do this instead
    return true   
end

function addListener2( )
    rect:addEventListener( "tap", state2Cb )
end

function addListener1()
    rect:addEventListener( "tap", state1Cb )
end

-- start
addListener1()  -- add first listener



[TOPIC: post.html]
#3

gjaman

[GLOBAL: userInfoPane.html]
gjaman
  • Enthusiast

  • 35 posts
  • Corona SDK

Looks like you seen the same thing I did:  In the simulator you see the location events triggered as soon as the event handler is added.  To trigger another event you must remove and re-add it.  I tried just re-adding, but that doesn't work - it needs to be removed first.

 

I just did some testing on my iOS device and it looks like while the application is running on a device, location events are triggered regularly and always when the app is launch or wakes from a suspended state.

 

I haven't tested yet - I'm curious if location services are disabled when the eventListener is removed? 



[TOPIC: post.html]
#4

FoxB

[GLOBAL: userInfoPane.html]
FoxB
  • Enthusiast

  • 37 posts
  • Corona SDK

Looks like you seen the same thing I did:  In the simulator you see the location events triggered as soon as the event handler is added.  To trigger another event you must remove and re-add it.  I tried just re-adding, but that doesn't work - it needs to be removed first.

 

I just did some testing on my iOS device and it looks like while the application is running on a device, location events are triggered regularly and always when the app is launch or wakes from a suspended state.

 

I haven't tested yet - I'm curious if location services are disabled when the eventListener is removed? 

 

:-)

 

I just wrote a piece of code that updates your GPS every 3 seconds

 

#1: Implement this example http://docs.coronalabs.com/api/event/location/longitude.html

 

#2: Use this piece of code:

-----

local function updateGPS()
    -- Reload GPS location
    Runtime:removeEventListener( "location", locationHandler )    
    Runtime:addEventListener( "location", locationHandler )    

    -- Update again
    timer.performWithDelay( 3000, updateGPS )
end
-- Update GPS
Runtime:addEventListener( "location", locationHandler )    
-- Update GPS every 3 seconds
updateGPS()

-----

 

NOTE: It might drain your battery to update every 3 seconds... So update it less often ;)

 

I have just tested it on my Nexus 4 and works VERY nicely!

 

I do not think the location services keep enabled when you remove it tho... haven't tested that one either.



[TOPIC: post.html]
#5

ebookren

[GLOBAL: userInfoPane.html]
ebookren
  • Enthusiast

  • 52 posts
  • Enterprise

I was wondering if, when you first start your app, getting GPS data is delayed a few seconds. I have been playing around with the GPS sample that;s included in the SDK.  The Runtime:addEventListener( "location", locationHandler ) takes a long time before it fires the first time. After that it seems to fire at regular intervals. 

 Any way to get the first location event to happen right after the app launches?

 

 Chris



[TOPIC: post.html]
#6

FoxB

[GLOBAL: userInfoPane.html]
FoxB
  • Enthusiast

  • 37 posts
  • Corona SDK

I was wondering if, when you first start your app, getting GPS data is delayed a few seconds. I have been playing around with the GPS sample that;s included in the SDK.  The Runtime:addEventListener( "location", locationHandler ) takes a long time before it fires the first time. After that it seems to fire at regular intervals. 

 Any way to get the first location event to happen right after the app launches?

 

 Chris

 

If you ask me it depends totally on the device ... I had Android devices in my paw *coughs* hand where they instantly get the GPS location (but I think Android gets it from cache).

 

If you have a very cheap device, it might take longer because the GPS sensor might be a budget one and doesn't have the newer Android versions.

 

I tested it on my Nexus 4 and I get my GPS data within seconds. It also depends a lot if I'm inside or outside ofcourse ;)



[TOPIC: post.html]
#7

Dev-Ze.rocks

[GLOBAL: userInfoPane.html]
Dev-Ze.rocks
  • Contributor

  • 211 posts
  • Corona SDK

I used your "piece of code" for some time now in an android app. Yesterday I removed it and now I am using it "normal" like, I switch the gps eventlistener on at the start of my app and when a valid signal is found I store the location data and remove it again. (I think it saves resources and battery, but I dont know this for sure and I just need the position at the start of the app)

 

Anyway, the result is, that a valid signal is found within maximum! 30 secs (mostly under 10 secs) what was before more like 1 to 2 minutes.

I tested this with a galaxy s4 mini and a motorola razr, with identical results.

(the update interval for the s4 mini is about every second for the razr it is about every three seconds, AFTER the first signal was found, before there is no output at all from the eventlistener on android)

 

I think this switching on and off of the eventlistner (ie adding and removing) interferes with the build in update intervall of the eventlistener and thus you need to be "lucky" to find a signal within the time the eventlistener is added.

 

Just to share my experience, maybe somebody will find it helpful.

 

Cheers,

Felix



[TOPIC: post.html]
#8

FoxB

[GLOBAL: userInfoPane.html]
FoxB
  • Enthusiast

  • 37 posts
  • Corona SDK

Hi Felix,

 

Thanks for your reaction. 

 

I have tested the code on the Galaxy Nexus S and Nexus 4 and my GPS wasn't updated automaticly. I had to use the code to keep the GPS uptodate.

 

If I remember well, Samsung uses his own version of Android (after Nexus S) and made big OS changes. So it MIGHT be possible that some Smartphones update the GPS automaticly, and some don't.

 

Perhaps it'd be even better to have the code check if your GPS gets updated automaticly after 30 seconds. If not; then update it automaticly with a script.



[TOPIC: post.html]
#9

Dev-Ze.rocks

[GLOBAL: userInfoPane.html]
Dev-Ze.rocks
  • Contributor

  • 211 posts
  • Corona SDK

hei Sneeuwvos,

 

this sounds like a good idea.

I will wait what my testers say, if they will get a valid gps signal within an acceptable amount of time.

 

Felix




[topic_controls]
[/topic_controls]