#import "SampleViewController.h"
#define GPS_MEASURING_TIME 3.0
#define GPS_DESIRED_ACCURACY 100.0
#define GPS_UNADOPTABLE_ACCURACY 2000.0
- (void)viewWillAppear:(BOOL)animated {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
bestEffortAtLocation = nil;
locationManager.desiredAccuracy = GPS_DESIRED_ACCURACY;
[locationManager startUpdatingLocation];
[self performSelector:@selector(stopUpdatingLocation:)
withObject:nil
afterDelay:GPS_MEASURING_TIME];
NSLog(@"locationManager startUpdatingLocation");
statusLable.text = @"GPS 取得中...";
[activityIndicator startAnimating];
[super viewWillAppear:animated];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"newLoc: %@", newLocation.description);
if (-[newLocation.timestamp timeIntervalSinceNow] > 5.0) return;
if (newLocation.horizontalAccuracy > GPS_UNADOPTABLE_ACCURACY) return;
if (bestEffortAtLocation == nil ||
newLocation.horizontalAccuracy < bestEffortAtLocation.horizontalAccuracy) {
[bestEffortAtLocation autorelease];
bestEffortAtLocation = [newLocation retain];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
[activityIndicator stopAnimating];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"GPS Error"
message:@"Error"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)stopUpdatingLocation:(NSObject *)args {
[locationManager stopUpdatingLocation];
[activityIndicator stopAnimating];
if (bestEffortAtLocation == nil) {
statusLabel.text = @"GPS 取得失敗";
return;
}
statusLabel.text = @"GPS 取得完了";
float longitude = bestEffortAtLocation.coordinate.longitude;
float latitude = bestEffortAtLocation.coordinate.latitude;
....
....
}
コメント
コメントを投稿