AP102 2016/5/18-2

AP102 2016/5/18-2 在map view點選然後右側打開user location的選項
按住option加上左鍵可以旋轉地圖

如果沒有出現在模擬器裡面在debug–>location–>切換
none 跟custom location彼此切換即可打開
預設給予一個經緯度
在xcode中是顛倒的
24.986525緯度
121.515312精度

C–>viewcontroller
m–>locationManager(取得位置)
V–>MapView

MapView的delegate指向viewcontroller
viewdidload指向viewcontroller
要用這兩個協定來溝通
MKViewDelegate
CLLocationManager

這兩個其實是protocol,是一種溝通的管道
IOS底層拿到locationManager然後拿到delegate
delegate如果有實作startUpdatingLocation就回傳
假設沒支援就跳過

會日文才能跟日本人(LocationManager)溝通
才能溝通。
protocol就是懂得如何跟LocationManager溝通
delegate是要指定與誰溝通

如果沒有protocol也compiler可以過,但Xcode會出現錯誤訊息。或者可以使用block,用起來輕鬆寫意,但是有些還是無法取代。

接著來實作上方segment control的按鈕選項功能
- (IBAction)mapTypeChanged:(id)sender {
    NSInteger targetIndex = [sender selectedSegmentIndex];

    switch(targetIndex){
        case 0:
            _mainMapView.mapType = MKMapTypeStandard;
             break;
        case 1:
             _mainMapView.mapType = MKMapTypeSatellite;
             break;
        case 2:
             _mainMapView.mapType = MKMapTypeHybrid;
            break;
        default:
            break;

    }
}

下方segment control 追蹤模式
- (IBAction)trackingModeChanged:(id)sender {

    NSInteger targetIndex = [sender selectedSegmentIndex];

    switch(targetIndex){
        case 0:
            _mainMapView.userTrackingMode = MKUserTrackingModeNone;
            break;
        case 1:
            _mainMapView.userTrackingMode = MKUserTrackingModeFollow;
            //追蹤你的方向
            break;
        case 2:
            _mainMapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
            //追蹤你的方向與面對的方向
            break;
        default:
            break;
}
}
在模擬器裡面的Debug 內的Location可以選擇
Apple
City bicycle ride
City Run
#pragma mark - CLLocationManagerDelegate Methods
//polocal

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

    CLLocation * currentLocation = locations.lastObject;//拿到一個Array給你
    //所以拿到lastObject是最新的,系統有空閒的時候一有位置出現就回報
    //如果系統底層處於忙碌不回報,當他有空時候再回報,一次把多個位置回報


    NSLog(@"Current Location: %.6f %.6f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);

    //Make the region change just once
    //GCD
    //dispatch_once_t 就是一個Long型別而已
    //準備好一個changeRegionOnceTaken變數

    static dispatch_once_t changeRegionOnceTaken;
    dispatch_once(&changeRegionOnceTaken, ^{

        MKCoordinateRegion region = _mainMapView.region;
        region.center = currentLocation.coordinate;
        region.span.latitudeDelta=0.01;
        region.span.longitudeDelta=0.01;
        [_mainMapView setRegion:region animated:true];
    });
}

看看是否有移動與縮放
這個就是一個MapView與ViewController的溝通
%.6f代表著只顯示小數點以下六位數
%.06f =>1.234000 會補足
%02.6f => 01.234
以上這是C語言的語法
也是JAVA的語法

#pragma mark -
-(void) mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    MKCoordinateRegion region = mapView.region;
    NSLog(@"center changed to : %.6f %.6f",region.center.latitude,region.center.longitude);

}
最後加上一段大頭針的程式碼
CLLocationCoordinate2D annotationCoodinate =
        currentLocation.coordinate;
        annotationCoodinate.latitude += 0.0005;
        annotationCoodinate.longitude += 0.0005;

        MKPointAnnotation * annotation =[MKPointAnnotation new];
        annotation.coordinate=annotationCoodinate;
        annotation.title = @"KFC";
        annotation.subtitle=@"Good to Eat";
        [_mainMapView addAnnotation:annotation];
完整程式碼
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

    CLLocation * currentLocation = locations.lastObject;//拿到一個Array給你
    //所以拿到lastObject是最新的,系統有空閒的時候一有位置出現就回報
    //如果系統底層處於忙碌不回報,當他有空時候再回報,一次把多個位置回報


    NSLog(@"Current Location: %.6f %.6f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);

    //Make the region change just once
    //GCD
    //dispatch_once_t 就是一個Long型別而已
    //準備好一個changeRegionOnceTaken變數

    static dispatch_once_t changeRegionOnceTaken;
    dispatch_once(&changeRegionOnceTaken, ^{
        //MKCoordinateRegion可以寫入與讀書
        //region代表地圖所移動的中心

        MKCoordinateRegion region = _mainMapView.region;
        //設定region的中心
        region.center = currentLocation.coordinate;
        //設定縮放比例span latitudeDelta和longitudeDelta的比例
        region.span.latitudeDelta=0.01;
        region.span.longitudeDelta=0.01;
        [_mainMapView setRegion:region animated:true];

        CLLocationCoordinate2D annotationCoodinate =
        currentLocation.coordinate;
        annotationCoodinate.latitude += 0.0005;
        annotationCoodinate.longitude += 0.0005;

        MKPointAnnotation * annotation =[MKPointAnnotation new];
        annotation.coordinate=annotationCoodinate;
        annotation.title = @"KFC";
        annotation.subtitle=@"Good to Eat";
        [_mainMapView addAnnotation:annotation];
    });
}

Comments

Popular posts from this blog

MEGA 暫存檔案刪除

IOS GCD多執行緒

利用CMD指令強制刪除