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 7489559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:16:08+00:00 2026-05-29T15:16:08+00:00

So I am saving the users location on a button push in a NSUserDefault.

  • 0

So I am saving the users location on a button push in a NSUserDefault. But When I call it I get null.

The goal is to get the users location(done) and then save it so when the map is reloaded the pin shows back up where the user once was. Right now it clears and resets the map.

Saving on button push:

 MKCoordinateRegion location1;
 location1.center.latitude =locationManager.location.coordinate.latitude;
 location1.center.longitude= locationManager.location.coordinate.longitude;
 location1.span.longitudeDelta=0.1;
 location1.span.latitudeDelta =0.1;

 MapAnnotation *ann1 =[[MapAnnotation alloc] init];
 ann1.title=@"You Parked Here";
 ann1.subtitle=@"";
 ann1.coordinate= location1.center;
 [mapView addAnnotation:ann1];

 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
 [ud setDouble:location1.center.latitude forKey:@"savedCoordinate-latitude"];
 [ud setDouble:location1.center.longitude forKey:@"savedCoordinate-longitude"];
 [ud setBool:YES forKey:@"savedCoordinate-exists"];
 [ud synchronize]; 

Recalling in viewWillAppear:

 -(void)viewWillAppear:(BOOL)animated{

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
if ([ud boolForKey:@"savedCoordinate-exists"])
{
    CLLocationCoordinate2D savedCoordinate;
    savedCoordinate.latitude = [ud doubleForKey:@"savedCoordinate-latitude"];
    savedCoordinate.longitude = [ud doubleForKey:@"savedCoordinate-longitude"];
    //create annotation object using savedCoordinate and add to map view...
    NSLog(@"%@",savedCoordinate.latitude);

    MKCoordinateRegion location1;
    //location1.center.latitude =savedCoordinate.latitude;
    //location1.center.longitude= savedCoordinate.longitude;
    location1.span.longitudeDelta=0.1;
    location1.span.latitudeDelta =0.1;

    MapAnnotation *ann1 =[[MapAnnotation alloc] init];
    ann1.title=@"You Parked Here";
    ann1.subtitle=@"";
    ann1.coordinate= savedCoordinate;
    [mapView addAnnotation:ann1];
 }  

}

Full .m page:

 #import "LocationTestViewController.h"
 #import "CoreLocation/CoreLocation.h"
 #import "MapAnnotation.h"

 @implementation LocationTestViewController
 @synthesize locationManager;
 @synthesize mapView;
 @synthesize labelText;

 - (void)dealloc
 {
  [mapView release];
  [locationManager release];
  [super dealloc];
 }

 - (void)didReceiveMemoryWarning
{
  // Releases the view if it doesn't have a superview.
  [super didReceiveMemoryWarning];

  // Release any cached data, images, etc that aren't in use.
 }

 #pragma mark - View lifecycle

 /*
 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad
 {
    [super viewDidLoad];

 }*/
 -(void)viewWillAppear:(BOOL)animated{

   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
   if ([ud boolForKey:@"savedCoordinate-exists"])
    {
      CLLocationCoordinate2D savedCoordinate;
      savedCoordinate.latitude = [ud doubleForKey:@"savedCoordinate-latitude"];
      savedCoordinate.longitude = [ud doubleForKey:@"savedCoordinate-longitude"];
      //create annotation object using savedCoordinate and add to map view...
      NSLog(@"%f",savedCoordinate.latitude);

      MKCoordinateRegion location1;
      //location1.center.latitude =savedCoordinate.latitude;
      //location1.center.longitude= savedCoordinate.longitude;
      location1.span.longitudeDelta=0.1;
      location1.span.latitudeDelta =0.1;

      MapAnnotation *ann1 =[[MapAnnotation alloc] init];
      ann1.title=@"You Parked Here";
      ann1.subtitle=@"";
      ann1.coordinate= savedCoordinate;
      [mapView addAnnotation:ann1];
   }  

 }

 - (void)viewDidUnload
 {
   [self setMapView:nil];
   [self setLocationManager:nil];
   [super viewDidUnload];
   // Release any retained subviews of the main view.
   // e.g. self.myOutlet = nil;
 }

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
  }

 - (IBAction)getLocation:(id)sender {

    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter=kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [locationManager startUpdatingLocation];

    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    MKCoordinateRegion region = {{0.0,0.0},{0.0,0.0}};
    region.center.latitude = locationManager.location.coordinate.latitude;
    region.center.longitude = locationManager.location.coordinate.longitude;
    region.span.longitudeDelta = 0.005f;
    region.span.latitudeDelta = 0.005f;
    [mapView setRegion:region animated:YES];
    [mapView setDelegate:sender];



    MKCoordinateRegion location1;
   location1.center.latitude =locationManager.location.coordinate.latitude;
   location1.center.longitude= locationManager.location.coordinate.longitude;
   location1.span.longitudeDelta=0.1;
   location1.span.latitudeDelta =0.1;

   labelText.text = [NSString stringWithFormat:@"LATITUDE: %f", location1.center.latitude];

   NSLog(@"button b4 save: %@", [NSString stringWithFormat:@"LATITUDE: %f", locationManager.location.coordinate.latitude]);

   MapAnnotation *ann1 =[[MapAnnotation alloc] init];
   ann1.title=@"You Parked Here";
   ann1.subtitle=@"";
   ann1.coordinate= location1.center;
   [mapView addAnnotation:ann1];

   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
   [ud setDouble:location1.center.latitude forKey:@"savedCoordinate-latitude"];
   [ud setDouble:location1.center.longitude forKey:@"savedCoordinate-longitude"];
   [ud setBool:YES forKey:@"savedCoordinate-exists"];
   [ud synchronize]; 

   CLLocationCoordinate2D savedCoordinate;
   savedCoordinate.latitude = [ud doubleForKey:@"savedCoordinate-latitude"];
   savedCoordinate.longitude = [ud doubleForKey:@"savedCoordinate-longitude"];
   //create annotation object using savedCoordinate and add to map view...
   NSLog(@"button push, %f",savedCoordinate.latitude);
 }

 - (IBAction)clearPins:(id)sender{
    NSArray *annotations = [mapView annotations];
    for (id annotation in annotations)
 {
       if ([annotation isKindOfClass:[MKUserLocation class]])
       {
          continue;
       }
       [mapView removeAnnotation:annotation];
    }   

 } 

 -(IBAction)goMainMenu{
    [self.parentViewController dismissModalViewControllerAnimated:YES];
 }
 @end

EDIT SOLUTION:

I added this to my button method:

 CLLocation *userLoc = mapView.userLocation.location;
 CLLocationCoordinate2D userCoordinate = userLoc.coordinate;

 NSLog(@"user latitude = %f",userCoordinate.latitude);
 NSLog(@"user longitude = %f",userCoordinate.longitude);

 NSUserDefaults *gLat = [NSUserDefaults standardUserDefaults];
 [gLat setFloat:userCoordinate.latitude forKey:@"latNumber"];

 NSUserDefaults *gLong = [NSUserDefaults standardUserDefaults];
 [gLong setFloat:userCoordinate.longitude forKey:@"longNumber"];


 float myLat=[gLat floatForKey:@"latNumber"];
 NSLog(@"Button Push: lat : %f",myLat);

 float myLong=[gLong floatForKey:@"longNumber"];
 NSLog(@"Button Push: long : %f",myLong);  

May not be the cleanest thing but it gets the job done.

  • 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-29T15:16:09+00:00Added an answer on May 29, 2026 at 3:16 pm

    The %@ string format specifier is for Objective-C objects and can’t be used with floats. Try using the %f operator like this:

    NSLog(@"latitude is %f", savedCoordinate.latitude);
    

    Here’s Apple’s documentation on String Format Specifiers.

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

Sidebar

Related Questions

My Java application is saving stuff in 'user.home' but on windows this does not
Saving a Word 2003 document to XML and then back results in a reduced
One of my website pages allows users to save locations that they've visited. They
I have a feature where I save a file to a location that user
I am created xml docs, but they are by default saving to the project
I was following the tutorial here: http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/ But I can't get past the first
Is it possible to save a WinForms user control(ex: button) to the database? Or
I have seen two different approaches in saving user preferences. APPROACH 1: Serializing them
Users are complaining about an Outlook plug-in my developers created, saying that the verifying
Saving data to Postscript in my app results in a Postscript file which I

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.