在iOS开发中自动获取当前的位置(GPS定位)
开发环境 xcode5.0
首先我们要引入这个框架CoreLocation.framework
将这个库引进来#import <CoreLocation/CoreLocation.h>
还有他的代理方法 CLLocationManagerDelegate
GPSViewController.h
注意这里的CLLocationManager* locationmanager要设置成全局变量,要不然得不到你想要的结果哦!具体为什么,我现在还不清楚
-
#import <UIKit/UIKit.h>
-
#import <CoreLocation/CoreLocation.h>
-
@interface GPSViewController : UIViewController<CLLocationManagerDelegate>
-
-
@property(nonatomic,retain) CLLocationManager* locationmanager;
-
@end
GPSViewController.m
-
#import "GPSViewController.h"
-
-
@interface GPSViewController ()
-
-
@end
-
-
@implementation GPSViewController
-
-
@synthesize locationmanager;
-
-
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
-
{
-
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
-
if (self) {
-
-
}
-
return self;
-
}
-
-
- (void)viewDidLoad
-
{
-
[super viewDidLoad];
-
-
locationmanager = [[CLLocationManager alloc]init];
-
-
-
-
-
-
-
-
-
-
-
-
[locationmanager setDesiredAccuracy:kCLLocationAccuracyBest];
-
-
locationmanager.delegate = self;
-
NSLog(@"开始定位");
-
-
[locationmanager startUpdatingLocation];
-
-
}
-
<pre code_snippet_id="278028" snippet_file_name="blog_20140406_2_4276648" name="code" class="objc"><pre code_snippet_id="278028" snippet_file_name="blog_20140406_2_4276648" name="code" class="objc">#pragma mark locationManager delegate
-
-
- (void)locationManager:(CLLocationManager *)manager
-
didUpdateToLocation:(CLLocation *)newLocation
-
fromLocation:(CLLocation *)oldLocation
-
{
-
NSLog(@"hello");
-
-
CLLocationCoordinate2D coordinate = newLocation.coordinate;
-
NSLog(@"输出当前的精度和纬度");
-
NSLog(@"精度:%f 纬度:%f",coordinate.latitude,coordinate.longitude);
-
-
[locationmanager stopUpdatingLocation];
-
-
float distance = [newLocation distanceFromLocation:oldLocation];
-
-
NSLog(@" 距离 %f",distance);
-
}
-
-
@end
-
</pre><br>
-
<br>
-
<pre></pre>
-
<pre code_snippet_id="278028" snippet_file_name="blog_20140406_2_4276648" name="code" class="objc"><pre code_snippet_id="278028" snippet_file_name="blog_20140406_2_4276648"></pre><pre code_snippet_id="278028" snippet_file_name="blog_20140406_2_4276648" name="code" class="objc"><pre code_snippet_id="278028" snippet_file_name="blog_20140406_2_4276648"></pre>
-
<pre></pre>
-
<pre></pre>
-
<pre></pre>
-
<pre></pre>
-
<pre></pre>
-
-
</pre></pre></pre>
在iOS开发中自动获取当前的位置(GPS定位),布布扣,bubuko.com
在iOS开发中自动获取当前的位置(GPS定位)
原文:http://blog.csdn.net/eduora_meimei/article/details/23432841