您的位置:首页 > 移动开发 > IOS开发

iOS 地图定位

2016-02-18 11:45 387 查看
#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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: