代码改变世界

ios中地图定位

2013-08-20 20:50  甘超波  阅读(346)  评论(0编辑  收藏  举报

 

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>


@interface ViewController : UIViewController<CLLocationManagerDelegate>
@property (retain, nonatomic) IBOutlet UITextField *latTxt;
@property (retain, nonatomic) IBOutlet UITextField *lontTxt;
@property (retain, nonatomic) IBOutlet UITextField *heighttxt;

@end




#import "ViewController.h"

@interface ViewController ()

//定位管理
@property(nonatomic,retain)CLLocationManager *locationManager;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.locationManager=[[[CLLocationManager alloc] init] autorelease];
    _locationManager.delegate=self;
    _locationManager.desiredAccuracy=kCLLocationAccuracyBest;//设备使用电池的。
    _locationManager.distanceFilter=1000.0;
    
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
//开始定位
    [_locationManager startUpdatingLocation];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear: animated];
    //停止定位
    [_locationManager stopUpdatingLocation];
}

#pragma mark -locationManager delegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    NSLog(@"--->%@",locations);
    CLLocation *location=[locations lastObject];
    self.latTxt.text=[NSString stringWithFormat:@"%3.5f",location.coordinate.latitude];
    self.lontTxt.text=[NSString stringWithFormat:@"%3.5f",location.coordinate.longitude];
    self.heighttxt.text=[NSString stringWithFormat:@"%3.5f",location.altitude];
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"--error-->%@",error.localizedDescription);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_locationManager release];
    [_latTxt release];
    [_lontTxt release];
    [_heighttxt release];
    [super dealloc];
}