【问题标题】:Storing and recalling user coords with NSUserDefaults使用 NSUserDefaults 存储和调用用户坐标
【发布时间】:2012-02-13 22:11:10
【问题描述】:

所以我将用户位置保存在 NSUserDefault 中的按钮上。但是当我调用它时,我会得到 null。

我们的目标是获取用户的位置(完成),然后保存它,以便在重新加载地图时,pin 会重新显示用户曾经所在的位置。现在它清除并重置地图。

节省按钮按下:

 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]; 

在 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];
 }  

}

完整的 .m 页面:

 #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

编辑解决方案:

我将此添加到我的按钮方法中:

 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);  

可能不是最干净的东西,但它可以完成工作。

【问题讨论】:

  • NSLog(@"%@",savedCoordinate.latitude);似乎 nslog 将无法正常工作
  • 您是否记录了要保存的值?他们是正确的吗?从 userDefaults 中返回的内容是正确的吗?什么是 MapAnnotation?
  • 嗯,我似乎得到了 null 或 0;我怎样才能把我的坐标拉出来?我会发布更多代码。
  • getLocation: 方法中,所有的NSLogs 都说什么?您不能/不应该在调用 startUpdatingLocation 后立即读取位置(它将为零或不可靠)。您应该在 didUpdateToLocation 委托方法中读取/保存位置(并创建注释)。
  • 好吧,getLocation 是一个按钮。它从全尺寸地图开始,然后缩放到用户位置。问题是日志不起作用。或者我应该说我无法让坐标显示在 NSLog 中。我相信我需要帮助拉出坐标。

标签: iphone ios mkmapview


【解决方案1】:

%@ 字符串格式说明符用于 Objective-C 对象,不能与浮点数一起使用。尝试像这样使用%f 运算符:

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

这是 Apple 在 String Format Specifiers 上的文档。

【讨论】:

  • 我注意到使用带有坐标的float 会截断最后几位数字(或四舍五入)...是否有任何其他数字变量可用于存储更多坐标值?
  • 不知道具体情况,推荐给你this answer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
  • 2019-06-26
  • 2021-06-20
  • 1970-01-01
相关资源
最近更新 更多