每次点击按钮时如何让GPS获取坐标?

时间:2014-07-29 13:14:23

标签: objective-c camera gps

我正在创建一个应用程序,允许用户拍照并接收与图片位置一致的GPS坐标,显示在cameraView的标签中。然后将图片,时间/日期和GPS位置转移到另一个视图,以便用户放置标题和描述并允许他们保存事件。弹出窗口警告它们已保存,当他们单击保存时,视图将返回到摄像机视图,其中字段现在已清除并准备好下一张图片。 我的问题是,当用户返回摄像机视图,或者甚至停留在第一个视图上并想要拍摄多张照片时,标签中不再放置GPS坐标?我无法弄清楚为什么。它就像它只能发生一次?

我设置了cameraView,所以当用户点击按钮时,每次都会检索坐标吗?

这是我在第一个控制器上的mainView.h文件

//摄像机视图值的标签     IBOutlet UILabel * DateTimeText;

IBOutlet UILabel *LongitudeText;
IBOutlet UILabel *LatitudeText;

NSData *image;
NSData *passBackImage;



 Here is the mainView.m file



//When the user hits the "take a picture button"
-(IBAction)onMoreSnapsClick:(id)sender{


 UIImagePickerController *pickerControl = [[UIImagePickerController alloc] init];

// If all's good, start up the camera
    if (pickerControl != nil)
       {
          pickerControl.sourceType = UIImagePickerControllerSourceTypeCamera;
          pickerControl.delegate = self;
          pickerControl.allowsEditing = true;


    // Camera turns on
    [self presentViewController:pickerControl animated:true completion:nil];


    //Retrieve co-ordinates when picture is taken
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    //Format date
    _date = [[NSDateFormatter alloc] init];
    [_date setDateFormat:@"dd-MM-yyyy HH:mm"];


    //Date into a string
    _dateString = [_date stringFromDate:[NSDate date]];
    [self->DateTimeText setText:_dateString];

    [_date stringFromDate:[NSDate date]];

    //Update the location
    [locationManager startUpdatingLocation];        

   }
   } 


   - (NSString*) getDateTime

      {
       NSDateFormatter *formatter;


       formatter = [[NSDateFormatter alloc] init];
       [formatter setDateFormat:@"dd-MM-yyyy HH:mm"];

       _dateString = [formatter stringFromDate:[NSDate date]];

       return _dateString;
      }

      //Setting up the delegate for the location finder

    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
      {
          NSLog(@"didFailWithError: %@", error);
         UIAlertView *errorAlert = [[UIAlertView alloc]
           initWithTitle:@"Error" message:@"Failed to Get Your Location"            

          delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
         [errorAlert show];
       }

       - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:            
      (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
        {
           NSLog(@"didUpdateToLocation: %@", newLocation);
           CLLocation *currentLocation = newLocation;


   _snapLatitude =[NSString stringWithFormat:@"%.8f",currentLocation.coordinate.latitude];


 _snapLongitude =[NSString stringWithFormat:@"%.8f",currentLocation.coordinate.longitude];


           if (currentLocation != nil) {

               LongitudeText.text = _snapLatitude;
               LatitudeText.text = _snapLongitude;


         }


        }


           //Also attached to my button-clears the fields of the previous picture coords                                         
       -(IBAction)onClick:(UIButton*)sender
           {

             DateTimeText.text = nil;
             LongitudeText.text =nil;
             LatitudeText.text = nil;
             _cameraMainView = nil;
           } 

       //Creating the objects for the images

  - (void)imagePickerController:(UIImagePickerController *)pickerControl 

           didFinishPickingMediaWithInfo:(NSDictionary *)info

       {
       //When finished, access the stored images
        _cameraMainView = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

         image = UIImagePNGRepresentation(_cameraMainView);

    // if everything's okay......
             if (_cameraMainView != nil)

    // assign the images to the properties
        {
          _imageView.image = _cameraMainView;

    // allow editing and create the delegate
           pickerControl.delegate = self;
           pickerControl.allowsEditing = true;

       }



[pickerControl dismissViewControllerAnimated:true completion:nil];


  }


   -(IBAction)snapsDetails:(id)sender
  {


       [self performSegueWithIdentifier:@"snapSegue" sender:self];


  }


  - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
     {

         if ([segue.identifier isEqualToString:@"snapSegue"])
     {
           SnapAddTextViewController *toSnapView = segue.destinationViewController;

              toSnapView.timeDate = _dateString;
              toSnapView.snapViewLongitude = _snapLongitude;
              toSnapView.snapViewLatitude =_snapLatitude;
              toSnapView.snapActualImage =[[UIImage alloc] initWithData:image];



}

}

有人能告诉我为什么在拍摄第一张照片时只需要一组坐标,之后没有坐标吗?我仍然在学校,并欣赏代码结构和整洁的宽容。我通常很好,因为事情正确地组合在一起,谢谢.a

0 个答案:

没有答案