只能完成定位(自己当前位置),以及在当前位置加上备注的功能
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