2012-09-29 86 views
0
#import <UIKit/UIKit.h> 

@interface WhereAmIViewController : UIViewController 

{ 

    IBOutlet UILabel *latLabel; 

    IBOutlet UILabel *longLabel; 
} 

@end 



- (void)viewDidLoad { 

    [super viewDidLoad]; 

    locationManager = [[CLLocationManager alloc] init]; 

    locationManager.delegate = self; 

    locationManager.distanceFilter = kCLDistanceFilterNone; 

    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; 


    [locationManager startUpdatingLocation]; 

} 

- (void)locationManager:(CLLocationManager *)manager 

didUpdateToLocation:(CLLocation *)newLocation 

fromLocation:(CLLocation *)oldLocation 

{ 

int degrees = newLocation.coordinate.latitude; 

double decimal = fabs(newLocation.coordinate.latitude - degrees); 

int minutes = decimal * 60; 

double seconds = decimal * 3600 - minutes * 60; 

NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
        degrees, minutes, seconds]; 

latLabel.text = lat; 

degrees = newLocation.coordinate.longitude; 

decimal = fabs(newLocation.coordinate.longitude - degrees); 

minutes = decimal * 60; 

seconds = decimal * 3600 - minutes * 60; 

NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
        degrees, minutes, seconds]; 

longLabel.text = longt; 

} 
+1

這是什麼問題? (並且請格式化代碼以使其可讀 - 目前正在進行編輯,以解決此問題 – Mark

回答

1
locationManager = [[CLLocationManager alloc]init]; 
locationManager.delegate = self;  

[locationManager startUpdatingLocation]; 

使用委託方法

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation 

    *)newLocation fromLocation:(CLLocation *)oldLocation { 

    CLLocationCoordinate2D coordinate = newLocation.coordinate; 

    [locationManager stopUpdatingLocation]; 
} 

你也可以使用經度緯度&從座標。

latLabel.text = [[NSNumber numberWithDouble:coordinate.latitude] stringValue]; 
longLabel.text = [[NSNumber numberWithDouble:coordinate.longitude] stringValue];