How to get GPS location before app initialisation? - iphone

Have a simple app that gets gps location. I would like to get a gps location before the app initialises. It seems for me that GPS location is updated asynchronously and I do get the app screen on the background loading while the question pops up: "allow appliction to use your current location?"
Well, how can I make a callback (sorry, coming from Ruby / JS background) to wait until I get the current location.
The code:
- (void)viewWillAppear:(BOOL)animated
{
// Set the main view to utilize the entire application frame space of the device.
// Change this to suit your view's UI footprint needs in your application.
CLController = [[CoreLocationController alloc] init];
CLController.delegate = self;
[CLController.locMgr startUpdatingLocation];
self.view.frame = [[UIScreen mainScreen] applicationFrame];
[super viewWillAppear:animated];
}
in CoreLocationController.m
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(#"Update location");
if([self.delegate conformsToProtocol:#protocol(CoreLocationControllerDelegate)]) {
[self.delegate locationUpdate:newLocation];
[self.locMgr stopUpdatingLocation];
# => wait initialisation until this point and than continue

You really just need to get your app up and running.
There are two things I would try (simultaneously) to handle the delay:
misdirection! Start with another screen open. Get the user interested in something else. One tap's worth of delay might be all you need to get a fix particularly if this is not a first run. On the first run when you know it will take a little time, run a little tutorial that will occupy the time.
start with the last location - this may not be right for you if inaccurate position information could be harmful but you could store the last location received when the app exits and use it as your current position when you start the app again. Of course you want to indicate that the location is a guess at best so you might want to draw your own position dot with uncertainty circle, indicating that the whole map is the uncertain area - until you get your first GPS fix and then you can begin drawing real position updates.
I think an artificial delay where you do nothing while waiting for the location to update is going to be a worse user experience than accepting that it will take a short time and trying to do as much as you can in that time.

Related

stopUpdatingLocation is not working in iOS 7

I have developed both iPhone & iPad application which supports for iOS 5 and iOS 6
in that application i have grabbed user current location using CLLocationManager.
when i want to stop updating receiving GPS coordinates. I have called stopUpdatingLocation
method to stop calling didUpdateToLocation method. It was woking completely fine with iOS 5 and 6 But unfortunately its not working with iOS 7.
Seems that stopUpdatingLocation method is not working .Any particular reason. ??
i had a keep a variable to monitor the life cycle of didUpdateToLocation method and stop executes it.
One thing might be possible is,
Your current location blue dot moving on your map is because you set the MKMapView's showsUserLocation to YES. It will track until you set it to NO.
Second thing,
You might be setting below thing to stop calling that method
locationManager = nil;
That does not stop monitoring, but what it does, not to refer the location manager, So now its not possible to stop monitoring.
Instead add below code & then see what happen
[locationManager stopUpdatingLocation];
locationManager=nil;
Hope it helps..
#tdevoy - Here is sample
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
latitude = location.coordinate.latitude;
longtitude = location.coordinate.longitude;
//stops updating current user location update
[locationManager stopUpdatingLocation];
}

iphone significant location event not related to cell change

i have been using the significant event location manager on ios, but it seems it does not based on cell change as claimed.
i used a simple application utilising significant location event, but i could not get a repeatable, consistent or sensitive response from the ios sdk.
i plotted the route (blue line), the cell towers(place mark) and a 1000m2 grid(blue square) on the map below.
map showing route
the route was 5000m in distance.
i drove it 3 times.
test1. received 2 sig events
test2. none
test3. received 1 sig events
before u complain that my test is too small, i have been monitoring other test routes for days and all show the inconsistent shape.
i was expecting the sig event to be based on cell tower switching. so i used a jailbreak app called 'signal' to identify what is the active cell. (NB.it is surprising which cell is active. Not what i would expect.)
From monitoring the 'signal' application, the cells switched around 6-7 times from what i noticed.
yet i did not received 6-7 sig events. So i cant see any correlation between cell switching and significant events.
so i have the following questions
Q1. what is the significant event trigger?
Q2. why are the result unreliable/inconsistent.
Q3. how can i get make my app receive consistent and sensitive significant event to 500m?
This is the code that is running in the test app
-(void)initLocationManager {
if (locationManager == nil) {
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[locationManager stopUpdatingLocation];
[locationManager startMonitoringSignificantLocationChanges];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self initLocationManager];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(#"%#", newLocation);
[[NSNotificationCenter defaultCenter]
postNotificationName:#"PositionUpdate" object:nil];
}
-lp
Significant Location changes are determined by iOS and there is nothing you can do to change their granularity directly. Note that it's only in iOS 4 that only the cell tower locations are used. Future versions of the operating system will improve this.
But as a workaround you could switch on normal CoreLocation position updates when the app get's woken/started by a significant location change. And then once you have the perfect result, disable Location Monitoring again to allow the app to go back to hibernation.
I just did a 3 kilometer test walk in fairly-central San Francisco with my app, and got zero Significant Change location events as well. I have a toggle in my app to change to normal polling with kCLLocationAccuracyHundredMeters and got 40+ events over the same distance.
Echoing the sentiments of various other answers herein and elsewhere, I'm holding off on Significant Change until iOS 5. I think the best thing to do right now is roll-your-own business logic to poll the old Core Location way, and ratchet frequency down gradually over time (or something) to be battery friendly.
Seems like in iOS 4, Significant Change is better suited for being able to tell which end of your commute you're at than which block (or even zip code) you're on.
I'm not sure if you have already looked at this - but the WWDC 2010 session 115 (Using Core Location in iOS) might give you a better idea of the the significant location change API.

MapView snaps back to original location - help!

I have a mapView with custom annotations and just ran into a problem on a beta tester's iPhone. The mapView won't let the user move to any location.. as soon as you try to move, it snaps right back to the original coordinates.
Any idea why? It doesn't happen in the simulator, and I notice it a little bit on my own device... but it is a consistent problem on another device.
Thanks so much!
static BOOL haveAlreadyReceivedCoordinates = NO;
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if(haveAlreadyReceivedCoordinates) {
return;
}
haveAlreadyReceivedCoordinates = YES;
CLLocationCoordinate2D loc = [newLocation coordinate];
[mapView setCenterCoordinate:loc];
}
Icky put you on the right track. You shouldn't be constantly resetting the map's center to whatever you get back from the location manager. The location manager will send you all sorts of updates.
You may wish to only use the update once and then ignore future updates (by setting some kind of flag), or you may wish to center the map on the user's location only when the user presses a button. In the latter case the only thing you need to do in didUpdateToLocation: is store the new location into some member variable.
The reason you don't see this in the simulator is that the location hardware simulator isn't constantly updating the way the real one is.

iPhone SDK: Track users location using GPS

I have a few questions about CoreLocation and GPS.
First, what method in core location is used to continually get the users current coordinates? And at what interval should these be retrieved?
Second, should these coordinates be pushed into a NSMutableArray each time they are received, so that the array of coordinates will represent the users path?
Thanks, just wanting to get started getting me mind around this.
A very abbreviated version:
First, adopt the <CLLocationManagerDelegate> protocol in your .h, and #import <CoreLocation/CoreLocation.h>.
Then in .m go:
- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D here = newLocation.coordinate;
NSLog(#"%f %f ", here.latitude, here.longitude);
}
Your -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation method will get pinged every time Core Location has something to say to you, which should happen every few seconds. those CLLocation objects contain info about accuracy, so you can screen for good points in that method.
Be sure to call [locationManager stopUpdatingLocation] and then [locationManager release] at some point!
Good luck finding yourself!
The best way to is read the CLLocationManager Class Reference, which links to several example projects. The short version:
Set the delegate property to a class that will receive location updates.
Implement the CLLocationManagerDelegate protocol in the delegate.
Call the appopriate methods to start updating location and/or heading.
You are able to define what range is acceptable for accuracy as well as how often you wish to receive automatic updates (based on a distance from last point mechanism). Also you can just turn off the location manager and turn it back on at will thru some use of a timer.
As for saving the locations to build a path, its not that simple. You will continually get GPS locations at first until the desired accuracy is achieved, and for any points in the future you may get more than one that is inaccurate before you get a good location. So building a list of these points will basically just be a list of their path, along with a lot of extra points.
You could solve this by saving only those points that have the accuracy you desire, but its an imperfect world in this respect.
Best case I would suggest you keep two lists, one is the path and the other is a running list of locations where you are comparing until you get a highly accurate location, then putting that on your path list.
Some of the example projects do things along these lines, do check them out.
You will have to do the following:
If device cannot access internet
Get co-ordinates from GPS device
Send these co-ordinates via SMS
Receive and decode SMS message at the SMS gateway you have to configure to receive info from device.
Update the info on the application database or any other store you are using
Update the position on map with latest info
If device can access internet
Get co-ordinates from GPS device
Connect to application server (may be some service) and upload information
Update the info on the application database or any other store you are using
Update the position on map with latest info

Will location manager find the current location repeatedly after it finds the current location in iPhone?

I am new to iPhone development. I want to know the working process of the CLLocationManager in determining the current location. I have used the below code for the button event to find the current location.
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self; // Tells the location manager to send updates to this object
[locationManager startUpdatingLocation];
IN
- (void)locationManager: (CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{ }
I have determined the lat and long values and loaded the current location.
It is working fine. If after updating to the new location, will it be once again called to get the current location? If iPhone is not able to find the current location, will then once again search for current location?
I want to know for both cases
If iPhone determines the current location.
If iPhone doesn't able to determine the current location.
According to the CoreLocationManager reference
The location service returns an
initial location as quickly as
possible, returning cached information
when available. After delivery of the
initial event notification, the
CLLocationManager object may deliver
additional events if the minimum
threshold distance (as specified by
the distanceFilter property) is
exceeded or a more accurate location
value is determined.
That seems to indicate that (1) you may received cached location information while it's still trying to determine your current location, (2) you may receive multiple updates as it determines the location more accurately.