繁体   English   中英

获取我在iPhone中的当前位置

[英]to get my current location in iphone

#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;

}
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];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM