Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-01-2012, 07:45 PM   #1 (permalink)
Registered Member
 
mouser58907's Avatar
 
Join Date: Feb 2010
Posts: 188
mouser58907 is on a distinguished road
Default Single GPS reading help

In my app I only need a single GPS reading to get their location within a few 100m or so. My solution to this was to start the standard updates, and let the GPS run for 5 seconds and terminate it with a timer.

I recently found there is a flaw in this method though. During the first run, it passes the test for isLocationServicesEnabled, then prompts the user asking if they want to allow GPS. I handled the case if the user says no fine, but if they wait longer than 5 seconds and hit yes, my GPS is already disabled and my code will never get called.

Is there a better way to organize my code?

Thanks
__________________
My Apps:Pee Prank (free)
My Company: Pulsar Productions

If I helped you, download my App or tell a friend! Thanks.
mouser58907 is offline   Reply With Quote
Old 02-01-2012, 07:54 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by mouser58907 View Post
In my app I only need a single GPS reading to get their location within a few 100m or so. My solution to this was to start the standard updates, and let the GPS run for 5 seconds and terminate it with a timer.

I recently found there is a flaw in this method though. During the first run, it passes the test for isLocationServicesEnabled, then prompts the user asking if they want to allow GPS. I handled the case if the user says no fine, but if they wait longer than 5 seconds and hit yes, my GPS is already disabled and my code will never get called.

Is there a better way to organize my code?

Thanks
Getting a reasonable GPS reading is frankly a pain in the a$$.

Here's what you have to do:

Create a location manager and ask it to begin updates.

In the delegate method for the location manager, first check the date stamp of the locations it gives you. If they are more than a couple of seconds older than now ( [NSDate date] ), reject them. You have to do that because location services caches very stale GPS readings, and will give you a reading that could be HOURS old when you first start location updates.

Then check the horizontal accuracy reading. If it is negative, reject it (that indicates an invalid reading.)

Then make sure the accuracy reading is <= 100 meters. If the value is > 100 meters (or whatever your desired accuracy) reject it.

If you get through all those tests, stop location updates.

Bear in mind that it frequently takes 15, 30, or more seconds to get a reading that's accurate to 100 meters. The GPS in iOS devices sucks, quite frankly.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-01-2012, 08:04 PM   #3 (permalink)
Registered Member
 
mouser58907's Avatar
 
Join Date: Feb 2010
Posts: 188
mouser58907 is on a distinguished road
Default

Thanks, Duncan. This seems like a much cleaner solution. I appreciate your help.
__________________
My Apps:Pee Prank (free)
My Company: Pulsar Productions

If I helped you, download my App or tell a friend! Thanks.
mouser58907 is offline   Reply With Quote
Old 02-01-2012, 08:23 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

Maybe i'm just getting lucky but my app finds the current location in less than a 10th of a second with accuracy set to 100 meters :/
__________________
Check out my apps

RickSDK is offline   Reply With Quote
Old 02-01-2012, 09:19 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by RickSDK View Post
Maybe i'm just getting lucky but my app finds the current location in less than a 10th of a second with accuracy set to 100 meters :/
You can set the desired accuracy to 100 meters, but that doesn't mean that's what you get. Check the things I outlined. I bet the reading you get within 1/10 second is the cached reading from the last time the GPS was active. It can be off by multiple kilometers.

I've spent weeks testing this stuff, and there is no way you are getting an accurate reading in 1/10 second. Not unless you're using a 4 or 4s and Apple has completely replaced the GPS hardware in those devices and is using a much higher grade unit (which I seriously doubt).

Try downloading the iPhone version of our geo-location app, "Save My Place" (it's only 99 cents) and watch the location and accuracy readings it displays. It shows the accuracy reported from the GPS as new readings come in. It will display the accuracy in feet or meters, or with a qualitative reading (excellent, good, fair, or poor).

Alternately, log the age and accuracy of the GPS readings you get.

Add this code to your locationManager:didUpdateToLocation:fromLocation: delegate method:

Code:
NSTimeInterval readingAge = [NSDate timeIntervalSinceReferenceDate] - 
  [[newLocation.timestamp timeIntervalSinceReferenceDate];
CLLocationAccuracy horizontalAccuracy = newlocation.horizontalAccuracy;
NSLog(@"GPS reading is %.2f seconds old, accuracy = %.2f", readingAge, horizontalAccuracy);
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-01-2012, 10:19 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

my phone is an iphone 4
__________________
Check out my apps

RickSDK is offline   Reply With Quote
Old 02-02-2012, 06:36 AM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by RickSDK View Post
my phone is an iphone 4
Like I said, I bet you a bottle of good single-malt scotch that you're not getting an accurate reading in 1/10 second after waking up the GPS. I bet you're getting an out-of-date reading that was cached. Either that, or the GPS is already running (like if you are showing the blue dot on the map.)
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-02-2012, 03:28 PM   #8 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

hmmm... the location info seems to be pretty accurate.

In fact if you open your generic iphone map app it will pinpoint your exact location (with a blue dot) in less than a second, usually accurate to within about 5 ft. So i'm not sure why an app using the same api's would take up to 30 seconds to get the same info.

maybe your phone is broke :/
__________________
Check out my apps

RickSDK is offline   Reply With Quote
Old 02-02-2012, 03:40 PM   #9 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by RickSDK View Post
hmmm... the location info seems to be pretty accurate.

In fact if you open your generic iphone map app it will pinpoint your exact location (with a blue dot) in less than a second, usually accurate to within about 5 ft. So i'm not sure why an app using the same api's would take up to 30 seconds to get the same info.

maybe your phone is broke :/
Do me a favor. Add the code I posted to your app, and then run it and watch what happens.

I bet you get an old reading to start with, and then readings with a low accuracy for quite a few seconds before the accuracy improves.

Another test:

Turn on your phone and open the maps app. Tap the "show my location" button and wait for the blue dot to show up. Zoom WAY, WAY in in the map, to the point where its almost as far as it will zoom in.

Press the home button to exit maps and turn your phone off.

Now, the next time you travel someplace in your car, fire up your phone and re-open the maps app. Watch carefully to see what the blue dot does.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-02-2012, 04:15 PM   #10 (permalink)
Registered Member
 
apatsufas's Avatar
 
Join Date: Jan 2011
Location: Thessaloniki, Greece
Posts: 121
apatsufas is on a distinguished road
Default

Quote:
Originally Posted by RickSDK View Post
hmmm... the location info seems to be pretty accurate.

In fact if you open your generic iphone map app it will pinpoint your exact location (with a blue dot) in less than a second, usually accurate to within about 5 ft. So i'm not sure why an app using the same api's would take up to 30 seconds to get the same info.

maybe your phone is broke :/
The iPhone uses besides GPS readings, information from your wifi and cellular connections (this is called A-GPS or Assisted GPS).

A-GPS uses wifi and cellular network information to get fast and accurate information about you position.

The problem is that there are conditions in wich A-GPS won't work and then you'll have to rely on the Standalone GPS which can be quite slow even up to a few minutes depending on conditions.

If you are travelling fast or if you don't have wifi and you don't have a strong cellular signal A-GPS won't work.

That's why you can't depend on it and you always should assume that you are using the Standalone GPS.
__________________
SQLed - Your Database Manager on the go

iAZConverter - Converts everything from A to Z
apatsufas is offline   Reply With Quote
Old 02-02-2012, 04:23 PM   #11 (permalink)
Token farm animal
 
sneaky's Avatar
 
Join Date: Apr 2011
Posts: 321
sneaky is on a distinguished road
Default

From my own testing, and I have done a fair bit, I will have to go with Duncan on this one.

Sure, you'll get locations immediately as soon as you ask for them but they are no good. They are too old and too inaccurate. If you get an accurate hit that soon it means that the gps was running recently already. Such as you sitting at your desk testing the app without actually moving.

Gps apps need to be road tested. Build a debug viewcontroler for your app and spend a few hours driving around and collecting data. Stop the gps and open the app again 20 miles down the road, inside a building, you will not get a location in just a few sec, it will take a while.
sneaky is offline   Reply With Quote
Old 02-02-2012, 04:46 PM   #12 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by sneaky View Post
From my own testing, and I have done a fair bit, I will have to go with Duncan on this one.

Sure, you'll get locations immediately as soon as you ask for them but they are no good. They are too old and too inaccurate. If you get an accurate hit that soon it means that the gps was running recently already. Such as you sitting at your desk testing the app without actually moving.

Gps apps need to be road tested. Build a debug viewcontroler for your app and spend a few hours driving around and collecting data. Stop the gps and open the app again 20 miles down the road, inside a building, you will not get a location in just a few sec, it will take a while.
What he said.

Although to be fair, inside a building is a very GPS-hostile environment.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-02-2012, 09:08 PM   #13 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Jimmy586 View Post
Maybe i'm just getting lucky but my app finds the current location in less than a 10th of a second with accuracy set to 100 meters :/
__________________
It gives you a location, but it is probably not giving you an accurate location. To repeat, just because you ask for an accuracy of 100 meters, that doesn't mean that's what you get. Have you checked the timestamp and the horizontal accuracy that you're getting back from the location manager? And have you tested it when you somewhere not in range of a known WiFi base station?

I've tested this extensively on a variety of devices, and you do not get sub 100 meter accuracy in a tenth of a second unless it's using WiFi hotspots. Cell triangulation is not ever accurate to 100 meters, and GPS is slow to get a fix. Sometimes very slow.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
gps

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 414
16 members and 398 guests
Atatator, chiataytuday, dre, FrankWeller, imac74, ipodphone, jeroenkeij, kukat, LunarMoon, MAMN84, n00b, PowerGoofy, QuantumDoja, Retouchable, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,675
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Retouchable
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:03 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0