二維碼生成 地圖




#import "AppDelegate.h"

#import "GDViewController.h"

#import

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

? ? [AMapServices sharedServices].apiKey = @"Your Key";




? ? // Override point for customization after application launch.

? ? return YES;

}




ViewController.m



#import "ViewController.h"

#import "oneViewController.h"

#import "twoViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? // Do any additional setup after loading the view, typically from a nib.

? ? oneViewController *one = [oneViewController new];

? ? UINavigationController *oneNav = [[UINavigationController alloc]initWithRootViewController:one];

? ? oneNav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"哈哈" image:[UIImage imageNamed:@"1.png"] selectedImage:[UIImage imageNamed:@"2.png"]];

? ? twoViewController *two = [twoViewController new];

? ? UINavigationController *twoNav = [[UINavigationController alloc]initWithRootViewController:two];

? ? two.tabBarItem =[[UITabBarItem alloc]initWithTitle:@"呵呵" image:[UIImage imageNamed:@"2.png"] selectedImage:[UIImage imageNamed:@"1.png"]];

? ? self.viewControllers=@[oneNav,twoNav];


}

@end






oneViewController.h

#import "oneViewController.h"

#import "twoViewController.h"

#import "threeViewController.h"

#import "fourViewController.h"

#import "GDViewController.h"

@interface oneViewController ()

@end

@implementationoneViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? // Do any additional setup after loading the view.

? ? self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"哈哈" style:UIBarButtonItemStylePlain target:self action:@selector(right)];

? ? self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"登陸" style:UIBarButtonItemStylePlain target:self action:@selector(left)];

? ? self.view.backgroundColor = [UIColor redColor];


? ? UISearchBar *sea = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];

? ? sea.placeholder = @"請(qǐng)輸入你想要的商品";

? ? self.navigationItem.titleView = sea;

? ? sea.backgroundColor = [UIColor whiteColor];


}

-(void)left{

? ? fourViewController *four = [fourViewController new];

? ? [self presentViewController:four animated:YES completion:nil];

}

-(void)right{

? ? threeViewController *three = [threeViewController new];

? ? [self.navigationController pushViewController:three animated:YES];

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

? ? // Get the new view controller using [segue destinationViewController].

? ? // Pass the selected object to the new view controller.

}

*/

@end



threeViewController


#import "threeViewController.h"

#import "GDViewController.h"

@interface threeViewController ()

@end

@implementationthreeViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? // Do any additional setup after loading the view.

? ? self.view.backgroundColor = [UIColor whiteColor];

? ? self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"地圖" style:UIBarButtonItemStylePlain target:self action:@selector(MapView:)];

}

-(void)MapView:(id)send{

? ? GDViewController *gd = [GDViewController new];

? ? [self presentViewController:gd animated:YES completion:nil];

}



?fourViewController


#import "fourViewController.h"

#import "smewmViewController.h"

#import "scewmViewController.h"

@interface fourViewController ()

- (IBAction)SMEWM:(id)sender;

- (IBAction)BackPress:(id)sender;

- (IBAction)LoginPress:(id)sender;

@property (weak, nonatomic) IBOutlet UITextField *TextFile;

@property (weak, nonatomic) IBOutlet UITextField *TextFile1;

- (IBAction)SCEWM:(id)sender;

@end

@implementationfourViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? // Do any additional setup after loading the view.

? ? self.view.backgroundColor = [UIColor whiteColor];



}

/*

?#pragma mark - Navigation

?

?// In a storyboard-based application, you will often want to do a little preparation before navigation

?- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

?// Get the new view controller using [segue destinationViewController].

?// Pass the selected object to the new view controller.

?}

?*/

- (IBAction)SMEWM:(id)sender {

? ? smewmViewController *sm = [smewmViewController new];

? ? [self presentViewController:sm animated:YES completion:nil];

}

- (IBAction)BackPress:(id)sender {

? ? [self dismissViewControllerAnimated:YES completion:nil];

}

- (IBAction)LoginPress:(id)sender {


}

- (IBAction)SCEWM:(id)sender {

? ? scewmViewController *sc = [scewmViewController new];

? ? [self presentViewController:sc animated:YES completion:nil];


}

@end





scewmViewController

#import "scewmViewController.h"

#import "QRCodeGenerator.h"

@interface scewmViewController ()

{

? ? UITextField*field;

? ? UIButton*btn;

? ? UIImageView*imgVV;

}

- (IBAction)Back:(id)sender;

@end

@implementationscewmViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? self.view.backgroundColor = [UIColor whiteColor];

? ? //文本框的位置

? ? field = [[UITextField alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 44)];

? ? //提示文字

? ? field.placeholder = @"請(qǐng)輸入內(nèi)容";

? ? field.borderStyle = UITextBorderStyleRoundedRect;

? ? //添加到視圖

? ? [self.view addSubview:field];


? ? //按鈕的位置

? ? btn = [[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 180, 200, 44)];

? ? //為按鈕設(shè)置文字

? ? [btn setTitle:@"點(diǎn)擊生成二維碼" forState:UIControlStateNormal];

? ? //為按鈕設(shè)置背景顏色

? ? btn.backgroundColor = [UIColor lightGrayColor];

? ? //給按鈕添加一個(gè)點(diǎn)擊方法

? ? [btn addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];

? ? [self.view addSubview:btn];


}

-(void)abc{

? ? //? ? 設(shè)置UIImageView的位置

? ? imgVV = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 300, 200, 200)];

? ? //為UIImageView設(shè)置二維碼圖片

? ? imgVV.image = [QRCodeGenerator qrImageForString:field.text imageSize:imgVV.bounds.size.width];

? ? //把圖片添加到視圖

? ? [self.view addSubview:imgVV];

? ? // Do any additional setup after loading the view.

}

/*

?#pragma mark - Navigation

?

?// In a storyboard-based application, you will often want to do a little preparation before navigation

?- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

?// Get the new view controller using [segue destinationViewController].

?// Pass the selected object to the new view controller.

?}

?*/

- (IBAction)Back:(id)sender {

? ? [self dismissViewControllerAnimated:YES completion:nil];

}

@end




smewmViewController

#import "smewmViewController.h"

#import

#define KMainW [UIScreen mainScreen].bounds.size.width

#define KMainH [UIScreen mainScreen].bounds.size.height

@interface smewmViewController ()<AVCaptureMetadataOutputObjectsDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, strong) AVCaptureDevice *device;

@property (nonatomic, strong) AVCaptureSession *session;

@property (nonatomic, strong) AVCaptureVideoPreviewLayer *preview;

@property (nonatomic, weak) UIImageView *line;

@property (nonatomic, assign) NSInteger distance;

@end

@implementationsmewmViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? [self initInfo];

? ? //創(chuàng)建控件

? ? [self creatControl];


? ? //設(shè)置參數(shù)

? ? [self setupCamera];


? ? //添加定時(shí)器

? ? [self addTimer];

}

-(void)viewWillDisappear:(BOOL)animated

{

? ? [super viewWillDisappear:animated];


? ? [self stopScanning];


}

-(void)initInfo

{

? ? //背景色

? ? self.view.backgroundColor = [UIColor blackColor];


? ? //導(dǎo)航標(biāo)題

? ? self.navigationItem.title = @"二維碼/條形碼";


? ? //導(dǎo)航右側(cè)相冊(cè)按鈕

? ? self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"相冊(cè)" style:UIBarButtonItemStylePlain target:self action:@selector(photoBtnOnClick)];


}

-(void)creatControl

{

? ? CGFloatscanW =KMainW*0.65;

? ? CGFloatpadding =10.0f;

? ? CGFloatlabelH =20.0f;

? ? CGFloattabBarH =64.0f;

? ? CGFloatcornerW =26.0f;

? ? CGFloatmarginX = (KMainW- scanW) *0.5;

? ? CGFloatmarginY = (KMainH- scanW - padding - labelH) *0.5;

? ? //遮蓋視圖

? ? for(inti =0; i <4; i++) {

? ? ? ? UIView*cover = [[UIViewalloc]initWithFrame:CGRectMake(0, (marginY + scanW) * i,KMainW, marginY + (padding + labelH) * i)];

? ? ? ? if(i ==2|| i ==3) {

? ? ? ? ? ? cover.frame=CGRectMake((marginX + scanW) * (i -2), marginY, marginX, scanW);

? ? ? ? }

? ? ? ? cover.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];

? ? ? ? [self.viewaddSubview:cover];

? ? }

? ? //掃描視圖

? ? UIView*scanView = [[UIViewalloc]initWithFrame:CGRectMake(marginX, marginY, scanW, scanW)];

? ? [self.viewaddSubview:scanView];


? ? //掃描線

? ? UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scanW, 2)];

? ? [self drawLineForImageView:line];

? ? [scanViewaddSubview:line];

? ? self.line= line;

? ? //邊框

? ? UIView*borderView = [[UIViewalloc]initWithFrame:CGRectMake(0,0, scanW, scanW)];

? ? borderView.layer.borderColor = [[UIColor whiteColor] CGColor];

? ? borderView.layer.borderWidth=1.0f;

? ? [scanViewaddSubview:borderView];


? ? //掃描視圖四個(gè)角

? ? for(inti =0; i <4; i++) {

? ? ? ? CGFloatimgViewX = (scanW - cornerW) * (i %2);

? ? ? ? CGFloatimgViewY = (scanW - cornerW) * (i /2);

? ? ? ? UIImageView*imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(imgViewX, imgViewY, cornerW, cornerW)];

? ? ? ? if(i ==0|| i ==1) {

? ? ? ? ? ? imgView.transform = CGAffineTransformRotate(imgView.transform, M_PI_2 * i);

? ? ? ? }else{

? ? ? ? ? ? imgView.transform=CGAffineTransformRotate(imgView.transform, -M_PI_2* (i -1));

? ? ? ? }

? ? ? ? [self drawImageForImageView:imgView];

? ? ? ? [scanViewaddSubview:imgView];

? ? }

? ? //提示標(biāo)簽

? ? UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(scanView.frame) + padding, KMainW, labelH)];

? ? label.text = @"將二維碼/條形碼放入框內(nèi)坟乾,即可自動(dòng)掃描";

? ? label.font = [UIFont systemFontOfSize:16.0f];

? ? label.textAlignment = NSTextAlignmentCenter;

? ? label.textColor = [UIColor whiteColor];

? ? [self.viewaddSubview:label];

? ? //選項(xiàng)欄

? ? UIView*tabBarView = [[UIViewalloc]initWithFrame:CGRectMake(0,KMainH- tabBarH,KMainW, tabBarH)];

? ? tabBarView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];

? ? [self.viewaddSubview:tabBarView];

? ? //開(kāi)啟照明按鈕

? ? UIButton *lightBtn = [[UIButton alloc] initWithFrame:CGRectMake(KMainW - 100, 0, 100, tabBarH)];

? ? lightBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];

? ? [lightBtnsetTitle:@"開(kāi)啟照明" forState:UIControlStateNormal];

? ? [lightBtnsetTitle:@"關(guān)閉照明" forState:UIControlStateSelected];

? ? [lightBtnaddTarget:self action:@selector(lightBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];

? ? [tabBarViewaddSubview:lightBtn];


}

-(void)setupCamera

{

? ? dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

? ? ? ? //初始化相機(jī)設(shè)備

? ? ? ? self->_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];


? ? ? ? //初始化輸入流

? ? ? ? AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:self->_device error:nil];


? ? ? ? //初始化輸出流

? ? ? ? AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];

? ? ? ? //設(shè)置代理坐漏,主線程刷新

? ? ? ? [outputsetMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];


? ? ? ? //初始化鏈接對(duì)象

? ? ? ? self->_session = [[AVCaptureSession alloc] init];

? ? ? ? //高質(zhì)量采集率

? ? ? ? [self->_session setSessionPreset:AVCaptureSessionPresetHigh];


? ? ? ? if([self->_sessioncanAddInput:input]) [self->_sessionaddInput:input];

? ? ? ? if([self->_sessioncanAddOutput:output]) [self->_sessionaddOutput:output];


? ? ? ? //條碼類(lèi)型(二維碼/條形碼)

? ? ? ? //? ? ? ? output.metadataObjectTypes = [NSArray arrayWithObjects:AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeQRCode, nil];



? ? ? ? //更新界面

? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

? ? ? ? ? ? self->_preview = [AVCaptureVideoPreviewLayer layerWithSession:self->_session];

? ? ? ? ? ? self->_preview.videoGravity = AVLayerVideoGravityResizeAspectFill;

? ? ? ? ? ? self->_preview.frame=CGRectMake(0,0,KMainW,KMainH);

? ? ? ? ? ? [self.view.layer insertSublayer:self->_preview atIndex:0];

? ? ? ? ? ? [self->_sessionstartRunning];

? ? ? ? });

? ? });



}

-(void)addTimer

{

? ? _distance = 0;

? ? self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];

? ? [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

}

-(void)timerAction

{

? ? if(_distance++ >KMainW*0.65)_distance=0;

? ? _line.frame=CGRectMake(0,_distance,KMainW*0.65,2);

}

-(void)removeTimer

{

? ? [_timer invalidate];

? ? _timer=nil;

}

-(void)lightBtnOnClick:(UIButton*)btn

{

? ? //判斷是否有閃光燈

? ? if(![_devicehasTorch]) {

? ? ? ? [selfshowAlertWithTitle:@"當(dāng)前設(shè)備沒(méi)有閃光燈嫩与,無(wú)法開(kāi)啟照明功能"message:nilsureHandler:nilcancelHandler:nil];

? ? ? ? return;

? ? }


? ? btn.selected= !btn.selected;


? ? [_device lockForConfiguration:nil];

? ? if(btn.selected) {

? ? ? ? [_device setTorchMode:AVCaptureTorchModeOn];

? ? }else{

? ? ? ? [_device setTorchMode:AVCaptureTorchModeOff];

? ? }

? ? [_device unlockForConfiguration];


}

-(void)photoBtnOnClick

{

? ? if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

? ? ? ? UIImagePickerController *controller = [[UIImagePickerController alloc] init];

? ? ? ? controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

? ? ? ? controller.delegate=self;


? ? ? ? [self presentViewController:controller animated:YES completion:nil];

? ? }else{

? ? ? ? [self showAlertWithTitle:@"當(dāng)前設(shè)備不支持訪問(wèn)相冊(cè)" message:nil sureHandler:nil cancelHandler:nil];

? ? }


}

#pragma mark - AVCaptureMetadataOutputObjectsDelegate

-(void)captureOutput:(AVCaptureOutput*)captureOutput didOutputMetadataObjects:(NSArray*)metadataObjects fromConnection:(AVCaptureConnection*)connection

{

? ? //掃描完成

? ? if([metadataObjectscount] >0) {

? ? ? ? //停止掃描

? ? ? ? [self stopScanning];

? ? ? ? //顯示結(jié)果

? ? ? ? [selfshowAlertWithTitle:@"掃描結(jié)果"message:[[metadataObjectsfirstObject]stringValue]sureHandler:nilcancelHandler:nil];

? ? }

}

-(void)stopScanning

{

? ? [_session stopRunning];

? ? _session = nil;

? ? [_preview removeFromSuperlayer];

? ? [self removeTimer];

}

#pragma mark - UIImagePickerControllrDelegate

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionaryid> *)info

{

? ? [pickerdismissViewControllerAnimated:YES completion:^{

? ? ? ? //獲取相冊(cè)圖片

? ? ? ? UIImage *image = info[UIImagePickerControllerOriginalImage];


? ? ? ? //識(shí)別圖片

? ? ? ? CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];

? ? ? ? NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];


? ? ? ? //識(shí)別結(jié)果

? ? ? ? if(features.count>0) {

? ? ? ? ? ? [selfshowAlertWithTitle:@"掃描結(jié)果"message:[[featuresfirstObject]messageString]sureHandler:nilcancelHandler:nil];


? ? ? ? }else{

? ? ? ? ? ? [selfshowAlertWithTitle:@"沒(méi)有識(shí)別到二維碼或條形碼"message:nilsureHandler:nilcancelHandler:nil];

? ? ? ? }

? ? }];


}

//提示彈窗

-(void)showAlertWithTitle:(NSString*)title message:(NSString*)message sureHandler:(void(^)())sureHandler cancelHandler:(void(^)())cancelHandler

{

? ? UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

? ? UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"確認(rèn)" style:UIAlertActionStyleDefault handler:sureHandler];

? ? UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:cancelHandler];

? ? [alertControlleraddAction:sureAction];

? ? [alertControlleraddAction:cancelAction];


? ? [self presentViewController:alertController animated:YES completion:nil];


}

-(void)drawImageForImageView:(UIImageView*)imageView

{

? ? UIGraphicsBeginImageContext(imageView.bounds.size);


? ? //獲取上下文

? ? CGContextRef context = UIGraphicsGetCurrentContext();

? ? //設(shè)置線條寬度

? ? CGContextSetLineWidth(context, 6.0f);

? ? //設(shè)置顏色

? ? CGContextSetStrokeColorWithColor(context, [[UIColor greenColor] CGColor]);

? ? //路徑

? ? CGContextBeginPath(context);

? ? //設(shè)置起點(diǎn)坐標(biāo)

? ? CGContextMoveToPoint(context, 0, imageView.bounds.size.height);

? ? //設(shè)置下一個(gè)點(diǎn)坐標(biāo)

? ? CGContextAddLineToPoint(context, 0, 0);

? ? CGContextAddLineToPoint(context, imageView.bounds.size.width, 0);

? ? //渲染誊涯,連接起點(diǎn)和下一個(gè)坐標(biāo)點(diǎn)

? ? CGContextStrokePath(context);


? ? imageView.image = UIGraphicsGetImageFromCurrentImageContext();

? ? UIGraphicsEndImageContext();


}

//繪制線圖片

-(void)drawLineForImageView:(UIImageView*)imageView

{

? ? CGSizesize = imageView.bounds.size;

? ? UIGraphicsBeginImageContext(size);


? ? //獲取上下文

? ? CGContextRef context = UIGraphicsGetCurrentContext();

? ? //創(chuàng)建一個(gè)顏色空間

? ? CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

? ? //設(shè)置開(kāi)始顏色

? ? const CGFloat *startColorComponents = CGColorGetComponents([[UIColor greenColor] CGColor]);

? ? //設(shè)置結(jié)束顏色

? ? const CGFloat *endColorComponents = CGColorGetComponents([[UIColor whiteColor] CGColor]);

? ? //顏色分量的強(qiáng)度值數(shù)組

? ? CGFloatcomponents[8] = {startColorComponents[0], startColorComponents[1], startColorComponents[2], startColorComponents[3], endColorComponents[0], endColorComponents[1], endColorComponents[2], endColorComponents[3]

? ? };

? ? //漸變系數(shù)數(shù)組

? ? CGFloatlocations[] = {0.0,1.0};

? ? //創(chuàng)建漸變對(duì)象

? ? CGGradientRefgradient =CGGradientCreateWithColorComponents(colorSpace, components, locations,2);

? ? //繪制漸變

? ? CGContextDrawRadialGradient(context, gradient, CGPointMake(size.width * 0.5, size.height * 0.5), size.width * 0.25, CGPointMake(size.width * 0.5, size.height * 0.5), size.width * 0.5, kCGGradientDrawsBeforeStartLocation);

? ? //釋放

? ? CGColorSpaceRelease(colorSpace);

? ? CGGradientRelease(gradient);


? ? imageView.image = UIGraphicsGetImageFromCurrentImageContext();

? ? UIGraphicsEndImageContext();


}

@end








GDViewController

#import "GDViewController.h"

#import

#import

@interface GDViewController ()<MAMapViewDelegate , CLLocationManagerDelegate>

@property(nonatomic , strong)CLLocationManager *manager;

@property(nonatomic , strong)MAMapView *mapView;

@end

@implementationGDViewController

-(MAMapView*)mapView{

? ? if(!_mapView) {

? ? ? ? _mapView = [[MAMapView alloc]initWithFrame:self.view.frame];

? ? ? ? //普通地圖

? ? ? ? _mapView.mapType = MAMapTypeStandard;

? ? ? ? _mapView.showsUserLocation = YES;


? ? ? ? //代理

? ? ? ? _mapView.delegate = self;

? ? }

? ? return _mapView;

}

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? // Do any additional setup after loading the view.

? ? self.manager = [[CLLocationManager alloc]init];

? ? self.manager.delegate=self;

? ? [self.manager requestWhenInUseAuthorization];


? ? //指南針

? ? self.navigationController.navigationBar.translucent = NO;

? ? [self.view addSubview:self.mapView];


}

-(IBAction)getCyrrentLocation:(id)sender{

? ? NSLog(@"獲取當(dāng)前位置");

? ? //更新位置信息

? ? [self.manager startUpdatingLocation];


}

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

? ? //獲取當(dāng)前位置

? ? CLLocation*cuLocation = [locationslastObject];

? ? //經(jīng)緯度

? ? CLLocationCoordinate2D curCoor =cuLocation.coordinate;

? ? NSLog(@"-----------");


? ? dispatch_async(dispatch_get_main_queue(), ^{

? ? ? ? //地圖顯示區(qū)域

? ? ? ? MACoordinateRegion showRegin = MACoordinateRegionMakeWithDistance(curCoor, 800, 800);

? ? ? ? //實(shí)例化一個(gè)大頭針

? ? ? ? MAPointAnnotation *point = [[MAPointAnnotation alloc]init];

? ? ? ? point.coordinate= curCoor;

? ? ? ? point.title=@"當(dāng)前位置";

? ? ? ? //地理解析

? ? ? ? CLGeocoder *geocoder = [[CLGeocoder alloc]init];

? ? ? ? [geocoderreverseGeocodeLocation:cuLocationcompletionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {

? ? ? ? ? ? CLPlacemark*place = [placemarkslastObject];

? ? ? ? ? ? point.subtitle= place.name;

? ? ? ? ? ? //回到UI主線程封断,給地圖添加注釋視圖

? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

? ? ? //? ? ? ? ? [self.mapView = nil];

? ? ? ? ? ? });

? ? ? ? }];


? ? });


}

-(MAMapView*)mapView:(MAMapView*)mapView viewForAnnotation:(id)annotation{

? ? if([annotationisKindOfClass:[MAPointAnnotationclass]]) {

? ? ? ? staticNSString*pointReuseIndentifier =@"pointReuseIndentifier";

? ? ? ? MAPinAnnotationView*annotationView =? (MAPinAnnotationView*)[mapViewdequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];


? ? ? ? if(annotationView ==nil) {

? ? ? ? ? ? annotationView = [[MAPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:pointReuseIndentifier];

? ? ? ? }

? ? ? ? annotationView.canShowCallout=YES;

? ? ? ? annotationView.animatesDrop=YES;

? ? ? ? annotationView.draggable=YES;

? ? ? ? annotationView.pinColor = MAPinAnnotationColorPurple;

? ? ? ? returnannotationView;


? ? }

? ? return nil;

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

? ? // Get the new view controller using [segue destinationViewController].

? ? // Pass the selected object to the new view controller.

}

*/

@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末宿亡,一起剝皮案震驚了整個(gè)濱河市谍婉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌巷蚪,老刑警劉巖,帶你破解...
    沈念sama閱讀 210,978評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件濒翻,死亡現(xiàn)場(chǎng)離奇詭異屁柏,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)有送,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,954評(píng)論 2 384
  • 文/潘曉璐 我一進(jìn)店門(mén)淌喻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人雀摘,你說(shuō)我怎么就攤上這事裸删。” “怎么了阵赠?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,623評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵涯塔,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我清蚀,道長(zhǎng)匕荸,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,324評(píng)論 1 282
  • 正文 為了忘掉前任枷邪,我火速辦了婚禮榛搔,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘东揣。我一直安慰自己践惑,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,390評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布嘶卧。 她就那樣靜靜地躺著尔觉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪脸候。 梳的紋絲不亂的頭發(fā)上穷娱,一...
    開(kāi)封第一講書(shū)人閱讀 49,741評(píng)論 1 289
  • 那天绑蔫,我揣著相機(jī)與錄音,去河邊找鬼泵额。 笑死配深,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的嫁盲。 我是一名探鬼主播篓叶,決...
    沈念sama閱讀 38,892評(píng)論 3 405
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼羞秤!你這毒婦竟也來(lái)了缸托?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,655評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤瘾蛋,失蹤者是張志新(化名)和其女友劉穎俐镐,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體哺哼,經(jīng)...
    沈念sama閱讀 44,104評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡佩抹,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了取董。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片棍苹。...
    茶點(diǎn)故事閱讀 38,569評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖茵汰,靈堂內(nèi)的尸體忽然破棺而出枢里,到底是詐尸還是另有隱情,我是刑警寧澤蹂午,帶...
    沈念sama閱讀 34,254評(píng)論 4 328
  • 正文 年R本政府宣布栏豺,位于F島的核電站,受9級(jí)特大地震影響豆胸,放射性物質(zhì)發(fā)生泄漏冰悠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,834評(píng)論 3 312
  • 文/蒙蒙 一配乱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧皮迟,春花似錦搬泥、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,725評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至爆阶,卻和暖如春燥透,著一層夾襖步出監(jiān)牢的瞬間沙咏,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,950評(píng)論 1 264
  • 我被黑心中介騙來(lái)泰國(guó)打工班套, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留肢藐,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,260評(píng)論 2 360
  • 正文 我出身青樓吱韭,卻偏偏與公主長(zhǎng)得像吆豹,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子理盆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,446評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容