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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值