It looks like you're new here. If you want to get involved, click one of these buttons!
#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
#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
Replies
Try creating a method that responds to a button to do this and see how it goes.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeThe 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.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome.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
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeDuncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomelocationManager: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.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome