fb_license

技術標籤

@selector (1) 初使化區塊 (1) 物件 (1) 物件導向 (2) 型別 (4) 封裝 (1) 流程控制 (1) 陣列 (3) 推論型別 (2) 實機測試 (1) 蓋索林(gasolin) (1) 模組 (1) 憑證 (1) 轉型 (1) 羅康鴻 (121) 類別 (1) 變數 (5) Accelerometer (1) ActiveRecord (1) Activity (1) AFNetworking (1) alloc (1) Android (3) Animation (1) App (1) App ID (1) APP上傳 (1) ASP.NET (1) AVAudioPlayer (1) block (1) C# (2) class (1) CLLocationManager (1) CLLocationManagerDelegate (1) CMMotionManager (4) Controller (1) delegate (1) DELETE語法 (1) Device Motion資料 (1) Dialog (1) DropDownList (1) dynamic language (2) Facebook SDK (9) FBRequest (5) FBRequestConnection (2) FMDB (1) Gesture Recognizers (6) GROUP BY (2) Gyro (1) HAVING (1) IBAction (1) IBOutlet (1) id (3) inheritance (1) init (1) Insert (1) instance variable (1) Interface Builder (1) iOS (70) iOS idea (7) iOS Introduction (1) Layout (1) Magnetometer (1) Menu (2) Method (2) MKMapView (1) MKPointAnnotation (1) MS SQL (5) Nil (1) NSArray (1) NSDictionary (1) NSError (1) NSFileManager & .plist (1) NSMutableArray (1) NSMutableDictionary (1) NSNotificationCenter (1) NULL (1) object (2) Objective-C (16) Objective-C idea (1) ORDER BY (1) Parameter (1) property (1) protocol (2) Provisioning (1) Proximate Sensor (1) Q and A (2) R類別 (1) Rails (9) RESTful SOA (9) Ruby (8) Scene (1) SEELECT (1) Segue (2) SEL (1) SELECT語法 (1) Shake (1) Simulator (1) SOA (8) SQL (6) SQL Server (5) SQL函數 (1) SQL彙總函數 SQL (1) SQLite (1) Storyboard (1) Style (1) Swift (1) Table (1) target & action (1) Theme (1) Toast (1) TRUNCATE TABLE語法 (1) UIActionSheet (1) UIActionSheetDelegate (1) UIActivityIndicatorView (1) UIAlertView (1) UIBarButtonItem (1) UIButton (1) UICollectionView (1) UICollectionViewDataSource (1) UIControl (9) UIDatePicker (1) UIImage (1) UIImagePickerController (2) UIImagePickerControllerDelegate (2) UIImageView (1) UILabel (1) UILongPressGestureRecognizer (1) UINavigationController (2) UIPanGestureRecognizer (1) UIPinchGestureRecognizer (1) UIProgressView (1) UIResponder (1) UIRotationGestureRecognizer (1) UISegmentedControl (1) UISlider (1) UIStepper (1) UISwipeGestureRecognizer (1) UISwitch (1) UITabBarController (1) UITableView (1) UITableViewDataSource (1) UITapGestureRecognizer (1) UITextField (1) UITextFieldDelegate (1) UITextView (2) UITextViewDelegate (1) UIToolBar (1) UIView (8) UIWebView (1) UPDATE語法 (1) var (2) VB.NET (7) View (4) WHERE子句 (1) XML (1)

2014/05/13

[iOS] 取得所在位置 CLLocationManager & CLLocationManagerDelegate


我是CLLocationManager,我可以取得手機所處地理相關的資訊,包含所在座標


我是CLLocationManagerDelegate,我定義了處理CLLocationManager的委外工作,以處理接收地理資訊後的工作


CLLocationManager:「那我們合作吧,我取得資訊後,在委派給實作CLLocationManagerDelegate的物件,以處理取得地理資訊後的工作。」







第1步:建立一個名為"CLLocationManager_CLLocationManagerDelegate"的"Single View Application"專案。



第2步:加入Label以顯示經緯度

  • 加入Label,並設立Outlet - locationLabel



第3步:加入CoreLocation.framework函數庫
  • 選取專案
  • 選取TARGETS的CLLocationManager_CLLocationManagerDelegate專案
  • 選Build Phases
  • 選Link Binary with Libraries
  • 並點選"+"號
  • 搜尋CoreLocation
  • 選取CoreLocation.framework後點選"Add"


第4步:引用<CoreLocation/CoreLocation.h>
  • 於ViewController.m檔中透過#import引用CoreLocation.h檔
ViewController.m
#import <CoreLocation/CoreLocation.h>

第5步:實作CCLocationManagerDelegate
  • 於ViewController.h實作CCLocationManagerDelegate協定

ViewController.h @interface ViewController : UIViewController <cllocationmanagerdelegate> @property (strong, nonatomic) IBOutlet UILabel *locationLabel; @end
  • 於ViewController.m實作locationManager:didUpdateLocations:方法,以在取得經緯度後顯示經緯度資訊
ViewController.m
//透過委派,實作經緯度更新 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { //第0個位置資訊,表示為最新的位置資訊 CLLocation * location = [locations objectAtIndex:0]; //取得經緯度資訊,並組合成字串 NSString * str = [[NSString alloc] initWithFormat:@"緯度:%f, 經度:%f" , location.coordinate.latitude , location.coordinate.longitude]; //顯示在label上 [self.locationLabel setText:str]; }

第6步:取得CCLocationManager物件並委派
  • 於ViewController.h宣告locationManager實體變數,以在放CCLocationManager物件
ViewController.h
//實作CLLocationManagerDelegate協定 @interface ViewController : UIViewController <CLLocationManagerDelegate> { CLLocationManager * locationManager; } @property (strong, nonatomic) IBOutlet UILabel *locationLabel; @end
  • 於ViewController.m中,建立CCLocationManager物件,並存放在locationManager實體變數中
  • 並委派工作予self - ViewController
  • 傳送startUpdatingLocation訊息,開始取得經緯資訊
ViewController.m
- (void)viewDidLoad { [super viewDidLoad]; //建立CLLocationManger, //並存於locationManager實體變數中 locationManager = [[CLLocationManager alloc] init]; //委派予self locationManager.delegate = self; //傳送startUpdatingLocation訊息, //開始更新訊息 [locationManager startUpdatingLocation]; }


第7步:執行
  • 執行後在Simulaotor上選Debug,在選Location\City Run,表示會以走路的方式模擬手機所在位置,如此就可取得位置變動的資訊,接著即可看到經緯度隨時間一再的變更






檔案連結:CLLocationManager_CLLocationManagerDelegate.zip



更多CLLocationManager的資訊:
更多CLLocationManagerDelegate的資訊
  • https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html