2

Working on application that needs to update current location in every second.

for that i am using CoreLocation framework.

My code is like

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
{

}
@property (nonatomic, retain) CLLocationManager *locationManager;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    [locationManager startMonitoringSignificantLocationChanges];
    [locationManager retain];
}

and implementing it's Delegate methods

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
   CLLocation *locB = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];

    NSLog(@"New location is >>>>>> %@",locB);

}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@">>>>>>>>>> error :%@", [error description]);

}

My Problem is that when i am running this code into simulator then it's working fine and updating location every second but when i run it into device then it shows location updating 3 or 4 times only.

I need this code working in both iphone 4 & iphone 5.

I have not found any solution for that.

Actually i need to count distance or count steps..for that i can also accelerometer but it's not giving perfect solution.

Please Help...

Thanks !

Sarafaraz Babi
  • 480
  • 6
  • 18
  • hey use CLLocationManagerDelegate Protocol Reference for this .. :) – Paras Joshi Jan 01 '13 at 12:57
  • see this link dude.. http://stackoverflow.com/questions/6516967/how-to-find-your-current-location-with-corelocation see the second answer from that.. – Paras Joshi Jan 01 '13 at 12:59
  • you want to get the location in background mode also?? – Paras Joshi Jan 01 '13 at 13:13
  • @ParasJoshi : sorry for late reply..i am using CLLocationManagerDelegate Protocol ???? I think i am using that one. – Sarafaraz Babi Jan 01 '13 at 13:59
  • NO i dont want it in background. – Sarafaraz Babi Jan 01 '13 at 14:00
  • Hello friends I found link http://support.apple.com/kb/HT5467 that says "GPS accuracy varies depending on the number of visible GPS satellites. Locating all visible satellites can take several minutes, with accuracy gradually increasing over time." It means i can't update my location every second.. – Sarafaraz Babi Jan 02 '13 at 12:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22057/discussion-between-paras-joshi-and-sarafaraz-babi) – Paras Joshi Jan 02 '13 at 12:45
  • see this answer dude.. http://stackoverflow.com/questions/2866292/iphone-sdk-track-users-location-using-gps – Paras Joshi Jan 02 '13 at 13:16

2 Answers2

1

My impression that it does not update position all the time and only does it for 3-5 times in order to provide some number of alternatives that you can use to find the best one. Try to use [locationManager startUpdatingLocation] to re-start positions generation and [locationManager stopUpdatingLocation]; to stop in in your tear down code.

Cynichniy Bandera
  • 5,991
  • 2
  • 29
  • 33
  • Thanks for quick reply..I am using [locationManager startUpdatingLocation] but not working. – Sarafaraz Babi Jan 01 '13 at 14:01
  • try to stop it before calling startUpdatingLocation ;) – Cynichniy Bandera Jan 01 '13 at 14:13
  • Alternatively you could start updating, create background thread and with timing you need (1s) check for [location_manager location] property and ignore any updates sent to delegate. You could also save old position to later know if its changed. If changed - send a msg selector, whatever to main thread (important) to update your UI. – Cynichniy Bandera Jan 01 '13 at 14:22
  • can i have any demo please.?b'coz can't understand what to do...it's quite simple thing and it should be run but creates problem... – Sarafaraz Babi Jan 02 '13 at 05:06
  • sorry don't have time to create a demo. Search for some examples on this site. I remember I saw some of them. – Cynichniy Bandera Jan 02 '13 at 10:35
  • I used this thing myself but not in a way you need. In my case I need position only once. – Cynichniy Bandera Jan 02 '13 at 13:57
  • Hummm...if you wants position then it gives ones very fine...but location updates is based on GPS Settelite and it could take 1 min or 10 min also...so that's the Problem. – Sarafaraz Babi Jan 04 '13 at 05:30
0

You can not run the application in background. You have to register it as an app that runs in the background. See the section Implementing Long-Running Background Tasks in this Link

ManagingYourApplicationsFlow

UPDATE:

See this answer

iPhone SDK: Track users location using GPS

Community
  • 1
  • 1
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70