获取我在iphone中的当前位置

时间:2012-09-29 10:15:57

标签: ios

#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 个答案:

答案 0 :(得分: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];
}

你可以使用经度&amp;坐标的纬度。

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