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;

}
4

1 回答 1

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];
于 2013-01-31T09:40:09.817 回答