iOS 學習(Whereami)

只能完成定位(自己當前位置),以及在當前位置加上備註的功能

iOS 8 以上要設置相應的參數
Model


//
//  BNRMapPoint.h
//  Whereami
//
//  Created by lee on 16/1/1.
//  Copyright © 2016年 ltybrp. All rights reserved.
//


#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface BNRMapPoint : NSObject<MKAnnotation>
-(id) initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *) t;
@property (nonatomic, readonly) CLLocationCoordinate2D  coordinate;
@property (nonatomic, copy) NSString *title;
@end
//
//  BNRMapPoint.m
//  Whereami
//
//  Created by lee on 16/1/1.
//  Copyright © 2016年 ltybrp. All rights reserved.
//

#import "BNRMapPoint.h"

@implementation BNRMapPoint
@synthesize  coordinate, title;
-(id) initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t{
    self = [super init];
    if (self) {
         coordinate = c;
        [self setTitle:t];
    }
    return  self;
}

-(id) init{
    return [self initWithCoordinate:CLLocationCoordinate2DMake(43.09, -83.09) title:@"My Home"];
}

@end

Controler

//
//  ViewController.h
//  Whereami
//
//  Created by lee on 15/12/30.
//  Copyright © 2015年 ltybrp. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <corelocation/CLLocationManagerDelegate.h>
#import <MapKit/MapKit.h>
#import "BNRMapPoint.h"

@interface ViewController : UIViewController<CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet MKMapView *worldView;

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@property (weak, nonatomic) IBOutlet UITextField *locationTitleFiled;

-(void) findLocation;
-(void) foundLocation : (CLLocation *) loc;
@end

//
//  ViewController.m
//  Whereami
//
//  Created by lee on 15/12/30.
//  Copyright © 2015年 ltybrp. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _locationManager = [[CLLocationManager alloc]init];
   // [_worldView setMapType: MKMapTypeSatelliteFlyover];/*MKmapView形式, 衛星 MKMapTypeSatellite  標準 MKMapTypeStandard 衛星➕街道 MKMapTypeSatelliteFlyover*/
    [_locationManager requestWhenInUseAuthorization];
    [_worldView setShowsUserLocation:YES];
    //view 會以藍色圓點的形式顯示出自己的位置
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

}

-(void) viewWillAppear:(BOOL)animated{
     [super viewWillAppear:animated];

}
-(void) viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
//顯示出放大版地圖
-(void) mapView:(MKMapView *)mapView 
didUpdateUserLocation:(MKUserLocation *)userLocation{
    CLLocationCoordinate2D loc = [userLocation coordinate];
    //主要功能函數
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [_worldView setRegion:region animated:YES];
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
    [self findLocation];
   // NSLog(@"textFildShouldReturn");
    [textField resignFirstResponder];
    return YES;
}

-(void) findLocation{
   // NSLog(@"findLocation");
    [_locationManager startUpdatingLocation];
    [_activityIndicator startAnimating];
    [_locationTitleFiled setHidden:YES];

}

-(void) foundLocation:(CLLocation *)loc{
   // NSLog(@"SDFS");
    CLLocationCoordinate2D coord = [loc coordinate];
    BNRMapPoint * mp = [[BNRMapPoint alloc] initWithCoordinate:coord title:[_locationTitleFiled text]];
     NSLog(@"%@", _locationTitleFiled.text);
     [_worldView addAnnotation:mp];
    // [_worldView setCenterCoordinate:loc.coordinate animated:YES];
     [_locationTitleFiled setText:@""];
     [_activityIndicator stopAnimating];
     [_locationTitleFiled setHidden:NO];
     [_locationManager startUpdatingLocation];
}

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

    NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
    if(t < -180)
        return ;
    [self foundLocation:newLocation];
    [_locationManager stopUpdatingLocation];
}
@end
發佈了448 篇原創文章 · 獲贊 3 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章