在iOS中的数据库中存储和更新Lat Long值

时间:2014-04-07 11:01:02

标签: ios objective-c database

我在我的应用程序中使用Google Maps iOS SDK。如果用户长按一个位置获取Lat Longs的地址并将它们存储在一个数组中并将该数组加载到UITableView中,我编写以下代码以获取特定位置的纬度和经度。

- (void)viewDidLoad
{
 [super viewDidLoad];
 CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
 UIScrollView  *scroll=[[UIScrollView alloc] initWithFrame:fullScreenRect];

 [self.view addSubview:scroll];
 scroll.contentSize=CGSizeMake(320,1000);
 GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:17.3600
                                                        longitude:78.4760 zoom:9 ];
 map = [GMSMapView mapWithFrame:CGRectMake(0,0, self.view.frame.size.width,390) camera:camera];
 [scroll addSubview:map];
 self->map.delegate = self;

 tab = [[UITableView alloc]initWithFrame:CGRectMake(0, 405, self.view.bounds.size.width, 250) style:UITableViewStylePlain];
 [scroll addSubview:tab];

 tab.delegate = self;
 tab.dataSource = self;
 markerArray = [[NSMutableArray alloc]init];
 addressArray = [[NSMutableArray alloc]init];
}
- (void)mapView:(GMSMapView *)mapViewdidLongPressAtCoordinate(CLLocationCoordinate2D)coordinate
 {
  if([markerArray count]>=5)
  {
    UIAlertView *al = [[UIAlertView alloc]initWithTitle:@"Sorry" message:@"U Have Already Added 5 Favourite Locations" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [al show];
  }
  else
  {
   NSLog(@"%f",coordinate.latitude);
   NSLog(@"%f",coordinate.longitude);

   l1 = coordinate.latitude;
   l2 = coordinate.longitude;

   GMSMarker *marker = [[GMSMarker alloc] init];
   marker.position = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
   marker.appearAnimation = YES;
   marker.map = mapView;
   [markerArray addObject:marker];
   [self location];
   }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
 return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [addressArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *cellIdentifier = @"Cell";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

 if(!cell)
 {
    cell = [[UITableViewCell alloc ]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
 }
 cell.textLabel.text = [addressArray objectAtIndex:indexPath.row];
 return cell;
}

现在我的任务是我想在用户点击提交按钮后在本地存储这些Lat Longs。并且如果用户想要更新我想要更新(添加/删除)来自数据库的那些点和与那些Lat long相关的标记,并将它们再次保存在数据库中。 任何人都可以帮助我。

1 个答案:

答案 0 :(得分:1)

要存储5个lat / long值的数组:

[[NSUserDefaults standardUserDefaults] setObject:markeyArray forKey:YOUR_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];

然后当你想要检索它们时:

NSArray *myArray = [[NSUserDefaults standardDefaults] objectForKey:YOUR_KEY];

至于从坐标获取地址,你应该考虑反向地理编码。