- iOS 教程
- iOS - 首頁
- iOS - 入門
- iOS - 環境設定
- iOS - Objective-C 基礎
- iOS - 首個 iPhone 應用程式
- iOS - 動作和出口
- iOS - 委託
- iOS - UI 元素
- iOS - 加速計
- iOS - 通用應用程式
- iOS - 攝像頭管理
- iOS - 定位處理
- iOS - SQLite 資料庫
- iOS - 傳送電子郵件
- iOS - 音訊和影片
- iOS - 檔案處理
- iOS - 訪問地圖
- iOS - 應用內購買
- iOS - iAd 整合
- iOS - GameKit
- iOS - 情節提要
- iOS - 自動佈局
- iOS - Twitter 和 Facebook
- iOS - 記憶體管理
- iOS - 應用程式除錯
- iOS 實用資源
- iOS - 快速指南
- iOS - 實用資源
- iOS - 討論
iOS - 定位處理
只要使用者允許應用程式利用核心位置框架訪問資訊,我們就能輕鬆定位 iOS 中的當前位置。
定位處理 – 涉及的步驟
步驟 1 − 建立一個基於檢視的簡單應用程式。
步驟 2 − 選擇您的專案檔案,然後選擇目標,然後再按如下新增 CoreLocation.framework −
步驟 3 − 在 ViewController.xib 中新增兩個標籤,並建立名為 latitudeLabel 和 longitudeLabel 的 ibOutlets。
步驟 4 − 透過依次選擇“檔案 → 新建 → 檔案... → 選擇 Objective C 類”並單擊“下一步”來建立一個新檔案。
步驟 5 − 使用 “子類”為 NSObject 來將類的名稱指定為 LocationHandler。
步驟 6 − 選擇“建立”。
步驟 7 − 如下更新 LocationHandler.h −
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@protocol LocationHandlerDelegate <NSObject>
@required
-(void) didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation;
@end
@interface LocationHandler : NSObject<CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@property(nonatomic,strong) id<LocationHandlerDelegate> delegate;
+(id)getSharedInstance;
-(void)startUpdating;
-(void) stopUpdating;
@end
步驟 8 − 如下更新 LocationHandler.m −
#import "LocationHandler.h"
static LocationHandler *DefaultManager = nil;
@interface LocationHandler()
-(void)initiate;
@end
@implementation LocationHandler
+(id)getSharedInstance{
if (!DefaultManager) {
DefaultManager = [[self allocWithZone:NULL]init];
[DefaultManager initiate];
}
return DefaultManager;
}
-(void)initiate {
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
}
-(void)startUpdating{
[locationManager startUpdatingLocation];
}
-(void) stopUpdating {
[locationManager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:
(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if ([self.delegate respondsToSelector:@selector
(didUpdateToLocation:fromLocation:)]) {
[self.delegate didUpdateToLocation:oldLocation
fromLocation:newLocation];
}
}
@end
步驟 9 − 如下更新 ViewController.h,我們在其中實現了 LocationHandler 委託並建立了兩個 ibOutlets −
#import <UIKit/UIKit.h>
#import "LocationHandler.h"
@interface ViewController : UIViewController<LocationHandlerDelegate> {
IBOutlet UILabel *latitudeLabel;
IBOutlet UILabel *longitudeLabel;
}
@end
步驟 10 − 如下更新 ViewController.m −
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[LocationHandler getSharedInstance]setDelegate:self];
[[LocationHandler getSharedInstance]startUpdating];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
[latitudeLabel setText:[NSString stringWithFormat:
@"Latitude: %f",newLocation.coordinate.latitude]];
[longitudeLabel setText:[NSString stringWithFormat:
@"Longitude: %f",newLocation.coordinate.longitude]];
}
@end
輸出
當我們執行此應用程式時,我們將得到以下輸出 −
廣告