Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6631219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:30:54+00:00 2026-05-25T22:30:54+00:00

How can I make my application update location only when a button is pressed?

  • 0

How can I make my application update location only when a button is pressed?

I have a button named “REFRESH”. Everytime this button is pressed, I want to show my user their location. For example, 51 Bourke Street, Victoria.

However, I do not want to update my location regularly. I want to update its location only when the button is pressed, to save battery power.

What do you think? Am I doing it correctly?

I have these classes:

  • VoteViewController.h and VoteViewController.m
  • CoreLocationController.h and CoreLocationController.m

This is what I have:

VoteViewController.h class

@interface VoteViewController : UIViewController <CoreLocationControllerDelegate> 
{
    CoreLocationController *coreController;
}

- (void)locationUpdate:(CLLocation *)location; 
- (void)locationError:(NSError *)error;
- (void)geoReverseAddress:(MKPlacemark *)placeMark;
- (IBAction)refreshButtonPressed;

VoteViewController.m class

- (void)viewDidLoad
{
    [super viewDidLoad];
    coreController = [[CoreLocationController alloc] init];
    coreController.delegate = self;
}

- (IBAction)refreshButtonPressed
{
    NSLog(@"Refresh Button pressed");
    label.text = [NSString stringWithString:@""];
    [coreController.locationManager startUpdatingLocation];
}

- (void)locationUpdate:(CLLocation *)location 
{
    comments.text = [location description];
    [coreController.locationManager stopUpdatingLocation];
}

- (void)locationError:(NSError *)error 
{
    comments.text = [error description];
    [coreController.locationManager stopUpdatingLocation];
}

- (void)geoReverseAddress:(MKPlacemark *)placeMark
{
    label.text = [NSString stringWithFormat:@"%@ %@, %@", [placeMark subThoroughfare], 
[placeMark thoroughfare], [placeMark locality]]; 
}

CoreLocationController.h class

@protocol CoreLocationControllerDelegate <NSObject>

@required
- (void)locationUpdate:(CLLocation *)location;
- (void)locationError:(NSError *)error;
- (void)geoReverseAddress:(MKPlacemark *)placeMark;

@end

@interface CoreLocationController : NSObject <CLLocationManagerDelegate, MKReverseGeocoderDelegate>
{
    CLLocationManager *locationManager;
    id delegate;

    MKReverseGeocoder *reverse;
}

@property(nonatomic, retain) CLLocationManager *locationManager;
@property(nonatomic, retain) id delegate;
@end

CoreLocationController.m class

-(id) init
{
    self = [super init];

    if (self != nil)
    {
        self.locationManager = [[[CLLocationManager alloc] init] autorelease];
        self.locationManager.delegate = self;
        self.locationManager.distanceFilter = kCLHeadingFilterNone;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"Update location");
    [self.delegate locationUpdate:newLocation];

    reverse = [[MKReverseGeocoder alloc] initWithCoordinate:[newLocation coordinate]];
    reverse.delegate = self;
    [reverse start];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    [self.delegate locationError:error];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    [self.delegate locationError:error];
    [reverse cancel];
    [reverse release];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    [self.delegate geoReverseAddress:placemark];
    [reverse cancel];
    [reverse release];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:30:54+00:00Added an answer on May 25, 2026 at 10:30 pm

    When you first fire up CLLocationManager, you’re very likely to get one stale location from the last time it ran. Once that’s out of the way, you’re going to start getting very inaccurate locations while the device uses WiFi sniffing and cell triangulation, while the GPS looks for a fix.

    So in your didUpdateToLocation method, you probably want to throw away the first hit, and then test the .horizontalAccuracy value of your newLocation object for a low enough value to trust.

    Apart from that, I don’t see anything bad about what you’ve sent here. I’m not sure I’d go to the trouble of wrapping the location fetching work in its own class, I’d probably just do that out in my viewController. But that’s a style choice. If you’re reusing this functionality elsewhere, what you’ve got here is obviously the way to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make so that my application can have only one instance running
I want to make an console application of c which can run other applications
SUMMARY How can I make my GUI application run on windows startup on a
How can I make a SQL Compact 2008 application for the desktop? I know
I'm using Ruby on Rails to make an application where businesses can create accounts
I am building an application as a library, but to make sure I can
I have a plain XCode CoreData enabled iPhone/iPad navigation application. In this I have
I have created a C# Windows Forms application which I've attempted to make as
I want to know if i can create a custom google maps application,on which
How can I make a has_many through work with multiple database connections? I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.