Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

CLLocation latitude How Do I convert to a comparable float

kweekskweeks Posts: 7New Users
edited June 2012 in iPhone SDK Development
I have been trying to get UILabel latitude (latit) into a format that I can use to compare with a static lat number. Like this if (latit >= 37.785800 && latit

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h> //import audio player framework
#import <CoreLocation/CoreLocation.h>



@interface ViewController : UIViewController<CLLocationManagerDelegate>{


AVAudioPlayer *audioPlayer; //define audio player

}

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) IBOutlet UILabel *latitude;
@property (strong, nonatomic) IBOutlet UILabel *longitude;
@property (strong, nonatomic) IBOutlet UILabel *horizontalAccuracy;
@property (strong, nonatomic) IBOutlet UILabel *verticalAccuracy;
@property (strong, nonatomic) IBOutlet UILabel *altitude;
@property (strong, nonatomic) IBOutlet UILabel *distance;
@property (strong, nonatomic) CLLocation *startLocation;



- (float) latitudeFloat;

- (IBAction)resetDistance;

- (IBAction)avPause: (id)sender;

- (IBAction)avRestart: (id)sender;

- (IBAction)avPlay: (id)sender;

@end

ViewController.m-----------------------------------

#import \"ViewController.h\"

@implementation ViewController

@synthesize longitude, latitude, horizontalAccuracy, verticalAccuracy, altitude, distance;
@synthesize locationManager, startLocation;

-(void)resetDistance
{
startLocation = nil;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
startLocation = nil;



//-----------------------Begin Audio Section-----------------------------------------------

float value2 = 37.785800;
float value3 = 37.785900;
float latit;
float latitudeFloat = [latitude.text floatValue];

latit = [latitude.text floatValue];


NSLog(@\"This is latitude.text:%f\", latitude);
NSLog(@\"This is my float: %f \n\nAnd here again: %.6f\", latitudeFloat, value2);
NSLog(@\"%d\",latitude);
NSLog(@\"x = %d\", latitude.text);
NSLog(@\"x = %f\",latit);


//if (theNumber >= SOME_MINIMUM_VALUE && theNumber <= SOME_MAXIMUM_VALUE)
if (latit >= latit && latit <= latit) {

// if (audio2 == value) {


NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@\"%@/01 Dawn.mp3\", [[NSBundle mainBundle] resourcePath]]]; // setup and select audio file to play

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -0;

[audioPlayer play];}



if (1 != 1) {


NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@\"%@/06 - Georgiana.mp3\", [[NSBundle mainBundle] resourcePath]]]; // setup and select audio file to play

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -0;

[audioPlayer play];}


audioPlayer.volume = 1.0; // 0.0 - no volume; 1.0 full volume

// audioPlayer.currentTime = 10; // jump to the 10 second mark
// [audioPlayer pause];
// [audioPlayer stop]; // Does not reset currentTime; sending play resumes
}



//-----------------------END Audio Section----------------------------------------------------










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

- (void)viewWillAppear: (BOOL)animated
{
[super viewWillAppear:animated];
}

- (void)viewDidAppear: (BOOL)animated
{
[super viewDidAppear:animated];
}

- (void)viewWillDisappear: (BOOL)animated
{
[super viewWillDisappear:animated];
}

- (IBAction)avPause: (id)sender {
[audioPlayer stop];
}

- (IBAction)avRestart: (id)sender {
[audioPlayer play]; audioPlayer.currentTime = 0;
}

- (IBAction)avPlay: (id)sender {
[audioPlayer play];
}



- (void)viewDidDisappear: (BOOL)animated
{
[super viewDidDisappear:animated];
}

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

#pragma mark -
#pragma mark CLLocationManagerDelegate

-(void)locationManager: (CLLocationManager *)manager
didUpdateToLocation: (CLLocation *)newLocation
fromLocation: (CLLocation *)oldLocation
{
NSString *currentLatitude = [[NSString alloc]initWithFormat:@\"%+.6f\", newLocation.coordinate.latitude];
latitude.text = currentLatitude;



NSString *currentLongitude = [[NSString alloc]initWithFormat:@\"%+.6f\", newLocation.coordinate.longitude];
longitude.text = currentLongitude;

NSString *currentHorizontalAccuracy = [[NSString alloc]initWithFormat:@\"%+.6f\", newLocation.horizontalAccuracy*3.28];
horizontalAccuracy.text = currentHorizontalAccuracy;

NSString *currentAltitude = [[NSString alloc] initWithFormat:@\"%+.6f\", newLocation.altitude*3.28];
altitude.text = currentAltitude;

NSString *currentVerticalAccuracy = [[NSString alloc]initWithFormat:@\"%+.6f\",newLocation.verticalAccuracy*3.28];
verticalAccuracy.text = currentVerticalAccuracy;

if (startLocation == nil)
self.startLocation = newLocation;

CLLocationDistance distanceBetween = [newLocation
distanceFromLocation:startLocation];

NSString *tripString = [[NSString alloc]initWithFormat:@\"%f\", distanceBetween];
distance.text = tripString;
}
-(void)locationManager: (CLLocationManager *)manager
didFailWithError: (NSError *)error
{
NSLog(@\"Location error\");

}
@end
Post edited by kweeks on

Replies

  • sneakysneaky Posts: 349Registered Users
    edited June 2012
    I have tried to convert it to a float: latit = [latitude.text floatValue]; NSLog shows latit is 0.000000.
    You are doing this in the viewDidLoad. This is run immediately as your app is coming alive. you don't have anything in your textfield, that's why you get a 0.

    Try creating a method that responds to a button to do this and see how it goes.
  • Duncan CDuncan C Posts: 8,135Tutorial Authors, Registered Users
    edited June 2012
    kweeks;438841 said:
    I have been trying to get UILabel latitude (latit) into a format that I can use to compare with a static lat number. Like this if (latit >= 37.785800 && latit

    #import <UIKit/UIKit.h>
    #import <AVFoundation/AVFoundation.h> //import audio player framework
    #import <CoreLocation/CoreLocation.h>



    @interface ViewController : UIViewController<CLLocationManagerDelegate>{


    AVAudioPlayer *audioPlayer; //define audio player

    }

    @property (strong, nonatomic) CLLocationManager *locationManager;
    @property (strong, nonatomic) IBOutlet UILabel *latitude;
    @property (strong, nonatomic) IBOutlet UILabel *longitude;
    @property (strong, nonatomic) IBOutlet UILabel *horizontalAccuracy;
    @property (strong, nonatomic) IBOutlet UILabel *verticalAccuracy;
    @property (strong, nonatomic) IBOutlet UILabel *altitude;
    @property (strong, nonatomic) IBOutlet UILabel *distance;
    @property (strong, nonatomic) CLLocation *startLocation;



    - (float) latitudeFloat;

    - (IBAction)resetDistance;

    - (IBAction)avPause: (id)sender;

    - (IBAction)avRestart: (id)sender;

    - (IBAction)avPlay: (id)sender;

    @end

    ViewController.m-----------------------------------

    #import \"ViewController.h\"

    @implementation ViewController

    @synthesize longitude, latitude, horizontalAccuracy, verticalAccuracy, altitude, distance;
    @synthesize locationManager, startLocation;

    -(void)resetDistance
    {
    startLocation = nil;
    }

    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
    }

    #pragma mark - View lifecycle

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];
    startLocation = nil;



    //-----------------------Begin Audio Section-----------------------------------------------

    float value2 = 37.785800;
    float value3 = 37.785900;
    float latit;
    float latitudeFloat = [latitude.text floatValue];

    latit = [latitude.text floatValue];


    NSLog(@\"This is latitude.text:%f\", latitude);
    NSLog(@\"This is my float: %f \n\nAnd here again: %.6f\", latitudeFloat, value2);
    NSLog(@\"%d\",latitude);
    NSLog(@\"x = %d\", latitude.text);
    NSLog(@\"x = %f\",latit);


    //if (theNumber >= SOME_MINIMUM_VALUE && theNumber <= SOME_MAXIMUM_VALUE)
    if (latit >= latit && latit <= latit) {

    // if (audio2 == value) {


    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@\"%@/01 Dawn.mp3\", [[NSBundle mainBundle] resourcePath]]]; // setup and select audio file to play

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -0;

    [audioPlayer play];}



    if (1 != 1) {


    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@\"%@/06 - Georgiana.mp3\", [[NSBundle mainBundle] resourcePath]]]; // setup and select audio file to play

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -0;

    [audioPlayer play];}


    audioPlayer.volume = 1.0; // 0.0 - no volume; 1.0 full volume

    // audioPlayer.currentTime = 10; // jump to the 10 second mark
    // [audioPlayer pause];
    // [audioPlayer stop]; // Does not reset currentTime; sending play resumes
    }



    //-----------------------END Audio Section----------------------------------------------------










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

    - (void)viewWillAppear: (BOOL)animated
    {
    [super viewWillAppear:animated];
    }

    - (void)viewDidAppear: (BOOL)animated
    {
    [super viewDidAppear:animated];
    }

    - (void)viewWillDisappear: (BOOL)animated
    {
    [super viewWillDisappear:animated];
    }

    - (IBAction)avPause: (id)sender {
    [audioPlayer stop];
    }

    - (IBAction)avRestart: (id)sender {
    [audioPlayer play]; audioPlayer.currentTime = 0;
    }

    - (IBAction)avPlay: (id)sender {
    [audioPlayer play];
    }



    - (void)viewDidDisappear: (BOOL)animated
    {
    [super viewDidDisappear:animated];
    }

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

    #pragma mark -
    #pragma mark CLLocationManagerDelegate

    -(void)locationManager: (CLLocationManager *)manager
    didUpdateToLocation: (CLLocation *)newLocation
    fromLocation: (CLLocation *)oldLocation
    {
    NSString *currentLatitude = [[NSString alloc]initWithFormat:@\"%+.6f\", newLocation.coordinate.latitude];
    latitude.text = currentLatitude;



    NSString *currentLongitude = [[NSString alloc]initWithFormat:@\"%+.6f\", newLocation.coordinate.longitude];
    longitude.text = currentLongitude;

    NSString *currentHorizontalAccuracy = [[NSString alloc]initWithFormat:@\"%+.6f\", newLocation.horizontalAccuracy*3.28];
    horizontalAccuracy.text = currentHorizontalAccuracy;

    NSString *currentAltitude = [[NSString alloc] initWithFormat:@\"%+.6f\", newLocation.altitude*3.28];
    altitude.text = currentAltitude;

    NSString *currentVerticalAccuracy = [[NSString alloc]initWithFormat:@\"%+.6f\",newLocation.verticalAccuracy*3.28];
    verticalAccuracy.text = currentVerticalAccuracy;

    if (startLocation == nil)
    self.startLocation = newLocation;

    CLLocationDistance distanceBetween = [newLocation
    distanceFromLocation:startLocation];

    NSString *tripString = [[NSString alloc]initWithFormat:@\"%f\", distanceBetween];
    distance.text = tripString;
    }
    -(void)locationManager: (CLLocationManager *)manager
    didFailWithError: (NSError *)error
    {
    NSLog(@\"Location error\");

    }
    @end

    The latitude and longitude values you get back from the location manger are doubles. You should use doubles, not floats, to do math on latitudes and longitudes. You need the precision of doubles to do distance calculations.

    As Sneaky suggested, the location manager is asynchronous. You ask it to start updating your location, and it starts sending you location updates at some time in the future.

    You have to wait for it to call you back. Even when it does call you back, you have to check the results that it gives you. The first reading from the GPS is usually a cached reading from the last time you used it, and may be hours or days old, and very wrong. You have to check the timestamp on the location readings and reject readings that are more than a couple of seconds old. Even after you get actual GPS readings, the first readings can be off by kilometers. It takes a while for the GPS to settle down and give reasonable readings. You have to check the horizontal accuracy reading and make sure it is decent. How accurate depends on your application. You certainly want it to be 500 meters or less in any case.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • kweekskweeks Posts: 7New Users
    edited June 2012
    Hello, I made a method as you suggested and i am getting a complier error on the method as i convert the UILabel to a float. error is Assigning to UILabel from incompatible type float. Here are my two files again would you look through and make sure I didn't mess up the method and completed your recommendation correctly. I have spent the good part of the day trying to get it to work correctly.


    .h------------------------------------------

    #import
    #import //import audio player framework
    #import

    @interface ViewController : UIViewController{


    AVAudioPlayer *audioPlayer; //define audio player

    }

    @property (strong, nonatomic) CLLocationManager *locationManager;
    @property (strong, nonatomic) IBOutlet UILabel *latitude;
    @property (strong, nonatomic) IBOutlet UILabel *longitude;
    @property (strong, nonatomic) IBOutlet UILabel *horizontalAccuracy;
    @property (strong, nonatomic) IBOutlet UILabel *verticalAccuracy;
    @property (strong, nonatomic) IBOutlet UILabel *altitude;
    @property (strong, nonatomic) IBOutlet UILabel *distance;
    @property (strong, nonatomic) CLLocation *startLocation;

    @property (strong, nonatomic) CLLocation *latit;


    - (IBAction)startGPS: (id)sender;

    - (IBAction)resetDistance;

    - (IBAction)avPause: (id)sender;

    - (IBAction)avRestart: (id)sender;

    - (IBAction)avPlay: (id)sender;

    @end





    .m------------------------------------------
    #import "ViewController.h"

    @implementation ViewController

    @synthesize longitude, latitude, horizontalAccuracy, verticalAccuracy, altitude, distance;
    @synthesize locationManager, startLocation;
    @synthesize latit;

    -(void)resetDistance
    {
    startLocation = nil;
    }

    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
    }

    #pragma mark - View lifecycle

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];
    startLocation = nil;



    //-----------------------Begin Audio Section-----------------------------------------------
    // CLLocation *currentLat;


    float value1 = 37.785600;
    float value2 = 37.785900;







    //if (theNumber >= SOME_MINIMUM_VALUE && theNumber <= SOME_MAXIMUM_VALUE) run audio file

    if (latit >= value1 && latit <= value2) {



    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/06 - Georgiana.mp3", [[NSBundle mainBundle] resourcePath]]]; // setup and select audio file to play

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -0;

    [audioPlayer play];}



    if (1 != 1) {


    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/06 - Georgiana.mp3", [[NSBundle mainBundle] resourcePath]]]; // setup and select audio file to play

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -0;

    [audioPlayer play];}


    audioPlayer.volume = 1.0; // 0.0 - no volume; 1.0 full volume

    // audioPlayer.currentTime = 10; // jump to the 10 second mark
    // [audioPlayer pause];
    // [audioPlayer stop]; // Does not reset currentTime; sending play resumes
    }



    //-----------------------END Audio Section----------------------------------------------------










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

    - (void)viewWillAppear: (BOOL)animated
    {
    [super viewWillAppear:animated];
    }

    - (void)viewDidAppear: (BOOL)animated
    {
    [super viewDidAppear:animated];
    }

    - (void)viewWillDisappear: (BOOL)animated
    {
    [super viewWillDisappear:animated];
    }

    // Method to convert UILabel Latitude to float---------------------------
    - (IBAction)startGPS: (id)sender{
    latit = [latitude.text floatValue];
    }





    - (IBAction)avPause: (id)sender {
    [audioPlayer stop];
    }

    - (IBAction)avRestart: (id)sender {
    [audioPlayer play]; audioPlayer.currentTime = 0;
    }

    - (IBAction)avPlay: (id)sender {
    [audioPlayer play];
    }


    - (void)viewDidDisappear: (BOOL)animated
    {
    [super viewDidDisappear:animated];
    }

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

    #pragma mark -
    #pragma mark CLLocationManagerDelegate

    -(void)locationManager: (CLLocationManager *)manager
    didUpdateToLocation: (CLLocation *)newLocation
    fromLocation: (CLLocation *)oldLocation
    {
    NSString *currentLatitude = [[NSString alloc]initWithFormat:@"%+.6f", newLocation.coordinate.latitude];
    latitude.text = currentLatitude;

    NSString *currentLongitude = [[NSString alloc]initWithFormat:@"%+.6f", newLocation.coordinate.longitude];
    longitude.text = currentLongitude;

    NSString *currentHorizontalAccuracy = [[NSString alloc]initWithFormat:@"%+.6f", newLocation.horizontalAccuracy*3.28];
    horizontalAccuracy.text = currentHorizontalAccuracy;

    NSString *currentAltitude = [[NSString alloc] initWithFormat:@"%+.6f", newLocation.altitude*3.28];
    altitude.text = currentAltitude;

    NSString *currentVerticalAccuracy = [[NSString alloc]initWithFormat:@"%+.6f",newLocation.verticalAccuracy*3.28];
    verticalAccuracy.text = currentVerticalAccuracy;

    if (startLocation == nil)
    self.startLocation = newLocation;

    CLLocationDistance distanceBetween = [newLocation
    distanceFromLocation:startLocation];

    NSString *tripString = [[NSString alloc]initWithFormat:@"%f", distanceBetween];
    distance.text = tripString;
    }
    -(void)locationManager: (CLLocationManager *)manager
    didFailWithError: (NSError *)error
    {
    NSLog(@"Location error");
    }
    @end
  • kweekskweeks Posts: 7New Users
    edited June 2012
    Ok I got it working now how do i create a way to start the method and have it continually run? so that it does the conversion and then the comparison and plays the audio when the if statement is actioned?
  • Duncan CDuncan C Posts: 8,135Tutorial Authors, Registered Users
    edited June 2012
    kweeks;438927 said:
    Hello, I made a method as you suggested and i am getting a complier error on the method as i convert the UILabel to a float. error is Assigning to UILabel from incompatible type float. Here are my two files again would you look through and make sure I didn't mess up the method and completed your recommendation correctly. I have spent the good part of the day trying to get it to work correctly.

    --snip--

    • Use code tags.
    • What line is generating an error, and what is the exact error?
    • Why are you converting everything to strings? You should save the coordinates as CGCoordinate2D values, and only covert to string for display.
    • Unless you're using ARC, you're leaking memory like crazy. Every object you create with alloc/init must be released when you are done with it. (If you are using ARC it's OK as is, because when the variable goes out of scope the system releases the object for you.)
    • You're still not checking the location you get to make sure it isn't old, and that it is accurate enough for our purposes. Your location manager delegate method should reject locations that are old, or have a poor horizontal accuracy.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • Duncan CDuncan C Posts: 8,135Tutorial Authors, Registered Users
    edited June 2012
    kweeks;438939 said:
    Ok I got it working now how do i create a way to start the method and have it continually run? so that it does the conversion and then the comparison and plays the audio when the if statement is actioned?
    Once you ask for location updates to start the system will call your

    locationManager:didUpdateToLocation:fromLocation:

    method over and over until you tell the location manager to stop location updates.

    Do a search in the Xcode docs for the LocateMe demo app, and take a look at that source code. Everything I've told you is spelled out in black and white in that sample app.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.