在iOS开发中自动获取当前的位置(GPS定位)
2014-04-11 11:28
537 查看
在iOS开发中自动获取当前的位置(GPS定位)
开发环境 xcode5.0
首先我们要引入这个框架CoreLocation.framework
将这个库引进来#import <CoreLocation/CoreLocation.h>
还有他的代理方法 CLLocationManagerDelegate
GPSViewController.h
注意这里的CLLocationManager* locationmanager要设置成全局变量,要不然得不到你想要的结果哦!具体为什么,我现在还不清楚
[objc] view
plaincopy
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface GPSViewController : UIViewController<CLLocationManagerDelegate>
@property(nonatomic,retain) CLLocationManager* locationmanager;
@end
GPSViewController.m
[objc] view
plaincopy
#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) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
locationmanager = [[CLLocationManager alloc]init];
//设置精度
/*
kCLLocationAccuracyBest
kCLLocationAccuracyNearestTenMeters
kCLLocationAccuracyHundredMeters
kCLLocationAccuracyHundredMeters
kCLLocationAccuracyKilometer
kCLLocationAccuracyThreeKilometers
*/
//设置定位的精度
[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>
开发环境 xcode5.0
首先我们要引入这个框架CoreLocation.framework
将这个库引进来#import <CoreLocation/CoreLocation.h>
还有他的代理方法 CLLocationManagerDelegate
GPSViewController.h
注意这里的CLLocationManager* locationmanager要设置成全局变量,要不然得不到你想要的结果哦!具体为什么,我现在还不清楚
[objc] view
plaincopy
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface GPSViewController : UIViewController<CLLocationManagerDelegate>
@property(nonatomic,retain) CLLocationManager* locationmanager;
@end
GPSViewController.m
[objc] view
plaincopy
#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) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
locationmanager = [[CLLocationManager alloc]init];
//设置精度
/*
kCLLocationAccuracyBest
kCLLocationAccuracyNearestTenMeters
kCLLocationAccuracyHundredMeters
kCLLocationAccuracyHundredMeters
kCLLocationAccuracyKilometer
kCLLocationAccuracyThreeKilometers
*/
//设置定位的精度
[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定位)
- 在iOS开发中自动获取当前的位置(GPS定位)
- iOS开发问题:系统提示框是否同意获取当前位置提示框自动消失问题
- iOS开发笔记(使用Google Map Service获取用户当前位置信息)
- iOS项目开发实战——使用CoreLocation获取当前位置信息
- iOS项目开发实战——使用CoreLocation获取当前位置信息
- iOS 系统提示框是否同意获取当前位置提示框自动消失
- 微信小程序开发之真机测试 地图定位 map API 无法获取当前位置的问题
- [IOS地图开发系类]4、MKMapview上添加MKAnnotationView,标记当前位置
- 获取当前位置信息-ios
- Android开发之GPS获取当前位置信息
- ios开发——获取当前设备信息
- iOS 光标默认获取当前文本框输入位置
- IOS使用高德地图获取当前位置信息
- 微信小程序开发之真机测试 地图定位 map API 无法获取当前位置的问题
- IOS获取当前地理位置文本
- iOS开发——定位获取经纬度坐标,并反编码获得地理位置信息(具体地址,国家、省、市等)
- iOS获取当前设备的型号、分辨率、运营商以及地理位置
- IOS获取当前地理位置文本
- iOS开发 获取当前视图最顶层的ViewController