课程表

iOS课程

工具箱
速查手册

定位操作

当前位置:免费教程 » 移动开发 » iOS

简介

在IOS中通过CoreLocation定位,可以获取到用户当前位置,同时能得到装置移动信息。

实例步骤

1、创建一个简单的View based application(视图应用程序)。

2、择项目文件,然后选择目标,然后添加CoreLocation.framework,如下所示

CoreLocation_Library_Addition

3、在ViewController.xib中添加两个标签,创建ibOutlet名为latitudeLabel和longtitudeLabel的标签

4、现在通过选择" File-> New -> File... -> "选择Objective C class 并单击下一步

5、把"sub class of"作为NSObject,将类命名为LocationHandler

6、选择创建

7、更新LocationHandler.h,如下所示

  1. #import <Foundation/Foundation.h>
  2. #import <CoreLocation/CoreLocation.h>
  3.  
  4. @protocol LocationHandlerDelegate <NSObject>
  5.  
  6. @required
  7. -(void) didUpdateToLocation:(CLLocation*)newLocation
  8. fromLocation:(CLLocation*)oldLocation;
  9. @end
  10.  
  11. @interface LocationHandler : NSObject<CLLocationManagerDelegate>
  12. {
  13. CLLocationManager *locationManager;
  14. }
  15. @property(nonatomic,strong) id<LocationHandlerDelegate> delegate;
  16.  
  17. +(id)getSharedInstance;
  18. -(void)startUpdating;
  19. -(void) stopUpdating;
  20.  
  21. @end

8、更新LocationHandler.m,如下所示

  1. #import "LocationHandler.h"
  2. static LocationHandler *DefaultManager = nil;
  3.  
  4. @interface LocationHandler()
  5.  
  6. -(void)initiate;
  7.  
  8. @end
  9.  
  10. @implementation LocationHandler
  11.  
  12. +(id)getSharedInstance{
  13. if (!DefaultManager) {
  14. DefaultManager = [[self allocWithZone:NULL]init];
  15. [DefaultManager initiate];
  16. }
  17. return DefaultManager;
  18. }
  19. -(void)initiate{
  20. locationManager = [[CLLocationManager alloc]init];
  21. locationManager.delegate = self;
  22. }
  23.  
  24. -(void)startUpdating{
  25. [locationManager startUpdatingLocation];
  26. }
  27.  
  28. -(void) stopUpdating{
  29. [locationManager stopUpdatingLocation];
  30. }
  31. -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:
  32. (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
  33. if ([self.delegate respondsToSelector:@selector
  34. (didUpdateToLocation:fromLocation:)])
  35. {
  36. [self.delegate didUpdateToLocation:oldLocation
  37. fromLocation:newLocation];
  38.  
  39. }
  40. }
  41.  
  42. @end

9、更新ViewController.h,如下所示

  1. #import <UIKit/UIKit.h>
  2. #import "LocationHandler.h"
  3. @interface ViewController : UIViewController<LocationHandlerDelegate>
  4. {
  5. IBOutlet UILabel *latitudeLabel;
  6. IBOutlet UILabel *longitudeLabel;
  7. }
  8. @end

10、更新ViewController.m,如下所示

  1. #import "ViewController.h"
  2.  
  3. @interface ViewController ()
  4.  
  5. @end
  6.  
  7. @implementation ViewController
  8.  
  9. - (void)viewDidLoad
  10. {
  11. [super viewDidLoad];
  12. [[LocationHandler getSharedInstance]setDelegate:self];
  13. [[LocationHandler getSharedInstance]startUpdating];
  14. }
  15.  
  16. - (void)didReceiveMemoryWarning
  17. {
  18. [super didReceiveMemoryWarning];
  19. // Dispose of any resources that can be recreated.
  20. }
  21.  
  22. -(void)didUpdateToLocation:(CLLocation *)newLocation
  23. fromLocation:(CLLocation *)oldLocation{
  24. [latitudeLabel setText:[NSString stringWithFormat:
  25. @"Latitude: %f",newLocation.coordinate.latitude]];
  26. [longitudeLabel setText:[NSString stringWithFormat:
  27. @"Longitude: %f",newLocation.coordinate.longitude]];
  28.  
  29. }
  30.  
  31. @end

输出

当我们运行该应用程序,会得到如下的输出:

locationOutput

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号