AP102 2016/5/18 MAP

AP102 2016/5/18 MAP

Linked Frameworks and Libraries

Corelocation.framework與定位有關
MapKit.framework與地圖有關

將MapViewr加入ViewController

Reset to Suggested Constraints

Segment Control 單選選擇=>類似Android 的Radio Button

右方選Segment Control –> Segments 選3個

改變Segment 的上面的文字直接點擊
(點不到可以稍微提高一點點點擊)

然後置中排版定位

開始拉IBoutlet

對Map View拉一個

@property (weak, nonatomic) IBOutlet MKMapView *mainMapView;

上面Segment Control的拉
- (IBAction)mapTypeChanged:(id)sender {}

用一個value changed

十幾個touch event 會遇到的機率實在太低
幾乎90%都是value changed和touch up inside

mapview上面按右鍵會出現delegate 代表透過protocal 來個某個人(viewcontroller)來做溝通。

但目前加入了兩個framework需要被import進來

而”” 代表的是自己的類別方法
<>代表是蘋果內建的方法

另外因為剛剛有delegate給viewcontroller需要在引入兩個delegate

@interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>;

建立一個變數

@interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>
{
    CLLocationManager*locationManager;
};

要先取得用戶的同意

兩種授權規範

requestWhenInUseAuthorization
requestAlwaysAuthorization

[CLLocationManager new];
代表著
[CLLocationManager allo] init];
意思是一樣的

但是如果是initWith….就不能這樣用

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    locationManager = [CLLocationManager new];
    //IOS7.0不支援以下方法
    //如果有支援則執行IOS8.0以上的方法
    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
        [locationManager requestWhenInUseAuthorization];//IOS 8 之後才有的授權機制大改過
        //IOS7.0不支援這個方法
    }

}

用這種判斷方式較好
如果用IOS版本來判斷9.3.2跟9.3.1
很難作浮點數的比較

然後來到Plist
Target Info
或者左邊直接點選info.plist

加入
NSLocationAlwaysUsageDescription
或者是
NSLocationWhenInUseUsageDescription

記得前面也要改成Always的方式

@property的方式可以改變
寫作的style

{

}這是等於是一種ViewController的一種區域變數是一種Swift的一種方法
也可以放在@implementation內

為了讓C相容於.h
讓.h跟.m都可以放
降低.h的重要性
在swift內甚至綜合在一起 .swift

馬的,簡單來說拉!
就是要你早一點下班!

@property可以玩得很深很深!超深!

前面為什麼要_?
_代表

以前一定要寫synthesize
才可以使用property

synthesize是什麼意思
搭配property
去使用property該如何對應這參數
@synthesize locationManager=_locationManager2
對外= _ 對內

對外叫做

以ARC有加self,跟沒加self.是沒差
已經四年多了
未必需要self.了可使用 { }
來定義區域變數了

甚至@synthesize 也可以不加了
ARC會自動加

 locationManager.desiredAccuracy
extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);

//用在GPS 有更高的更新頻率 一秒一次,但開車不夠。
//PAPAGO被罵死,因為等級不夠。

extern const CLLocationAccuracy kCLLocationAccuracyBest;
// 鐵定會開啟GPS 手機會有點燙 啟用GPS晶片會比較耗電
//一秒鐘
extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
// wifi 定位,可能會開啟GPS (但是這是蘋果內定)
extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
// 使用cell定位
extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
// 使用cell定位
extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
//交友服務  不需要很精確  

locationManager.desiredAccuracy= kCLLocationAccuracyBest;
就是一種精度的選擇

 locationManager.desiredAccuracy= kCLLocationAccuracyBest;
    locationManager.activityType= CLActivityTypeAutomotiveNavigation;
    locationManager.delegate=self;
    [locationManager startUpdatingLocation];

locationManager.activityType= CLActivityTypeAutomotiveNavigation;
活動的類型

目前選擇是車輛的導航

typedef NS_ENUM(NSInteger, CLActivityType) {
    CLActivityTypeOther = 1,
    CLActivityTypeAutomotiveNavigation, // for automotive navigation
    //車輛用
    CLActivityTypeFitness,              // includes any pedestrian activities
    //健身跑步用
    CLActivityTypeOtherNavigation       // for other navigation cases (excluding pedestrian navigation), e.g. navigation for boats, trains, or planes
    //火車 船 飛機
};

//回報位置給viewcontroller
locationManager.delegate=self;

//開始回報位置
[locationManager startUpdatingLocation];
startUpdatingHeading<–這是羅盤方法

做一個標記pragma mark

#pragma mark - CLLocationManagerDelegate Methods
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
   CLLocation * currentLocation = locations.lastObject;
    NSLog(@"Current Location: %.6f %.6f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
}

typedef struct {
CLLocationDegrees latitude;
CLLocationDegrees longitude;
} CLLocationCoordinate2D;

CLLocationDegrees只是一個double

@property(readonly, nonatomic) CLLocationDistance altitude;
//也可以取得海拔
但垂直的海拔誤差高,甚至達到50米~100米
@property(readonly, nonatomic) CLLocationAccuracy horizontalAccuracy;
@property(readonly, nonatomic) CLLocationAccuracy verticalAccuracy;
@property(readonly, nonatomic) CLLocationDirection course
__OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_2_2) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
//角度與前進方向
@property(readonly, nonatomic) CLLocationSpeed speed __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_2_2) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
//速度 APPLE取得的平滑速度從這裡直接取得最簡單
@property(readonly, nonatomic, copy, nullable) CLFloor *floor __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_8_0);
//只有特定的區域才能取得樓層
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_3_2);
//算出兩個CLLocation 的直線距離
//非常難算

看兩個點的路徑規劃
要看路怎麼走
使用MKRoute這個方式

https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKRoute_class/

Google的還可以A到B到C
APPLE只有單點

Comments

Popular posts from this blog

MEGA 暫存檔案刪除

IOS GCD多執行緒

利用CMD指令強制刪除