I am using core location framework inside my app.The didUpdateToLocation method is called two times when app starts very first time.I allocated location manager instance and initiate it and called startUpdatingLocation. And in didUpdateToLocation i am calling another function in which i am sending the current latitude and longitude to server so how can i avoid multiple calling of this function?
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
[self.mapView setShowsUserLocation:YES];
if( [CLLocationManager locationServicesEnabled])
{
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
locationManager.distanceFilter = 1000.0f;
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"GPS Disabled" message:@"Enable GPS settings to allow the application to search for your current location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
//Here is didUpdateToLocation method
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
self.lat = newLocation.coordinate.latitude;
self.longi = newLocation.coordinate.longitude;
NSLog(@"Updated Location : lat--%f long--%f",self.lat,self.longi);
[self updateUserLocation];
}
//in this function i am sending the latitude and longitude to server.
-(void)updateUserLocation
{
NSString *urlString = [[NSString stringWithFormat:@"http://appifylabs.com/tracemybuddies/index.php/iphone/updateUserLocation/?userId=1&latitude=%@&longitude=%@",self.lat,self.longi] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[conn url_Connection:urlString withActivityView:self.activityView withParentView:self.view];
}
Inside didUpdateToLocation, when oldLocation is received as NULL, return from the function.
It will be NULL when first time your delegate will be called.
Second, you can take the difference of time stamp between currentTime and newLocation.timeStamp. If it is not with in acceptable limit, return from the function.