//
// ViewController.m
// ImageTest
//
// Created by wutong on 15/10/22.
// Copyright © 2015年 wutong. All rights reserved.
//
#import "ViewController.h"
#import <objc/message.h>
@interface ViewController ()
{
UIImageView* _imageView;
CLLocationManager* _localtion;
UILabel* _lableH;
UILabel* _lableV;
MKMapView* _mapview;
BOOL _isNeedGoBack;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/*
UIButton* btn1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];
[btn1 setBackgroundColor:[UIColor orangeColor]];
[btn1 setTitle:@"picture" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(onBtnPicture) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn1];
[btn1 release];
UIButton* btn2 = [[UIButton alloc] initWithFrame:CGRectMake(250, 50, 100, 50)];
[btn2 setBackgroundColor:[UIColor orangeColor]];
[btn2 setTitle:@"capture" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(onBtnCapture) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn2];
[btn2 release];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,200, 200, 200)];
[self.view addSubview:_imageView];
[_imageView release];
*/
// self.view.userInteractionEnabled = NO;
CGRect bounds = [[UIScreen mainScreen] bounds];
_isNeedGoBack = YES;
_mapview = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height)];
_mapview.showsUserLocation = YES;
_mapview.delegate = self;
[self.view addSubview:_mapview];
_lableH = [[UILabel alloc]initWithFrame:CGRectMake(10, bounds.size.height-100, bounds.size.width-20, 20)];
_lableH.backgroundColor = [UIColor clearColor];
_lableH.textColor = [UIColor blackColor];
[self.view addSubview:_lableH];
[_lableH release];
_lableV = [[UILabel alloc]initWithFrame:CGRectMake(10, bounds.size.height-70, bounds.size.width-20, 20)];
_lableV.backgroundColor = [UIColor clearColor];
_lableV.textColor = [UIColor blackColor];
[self.view addSubview:_lableV];
UIButton* btn1 = [[UIButton alloc] initWithFrame:CGRectMake(10, bounds.size.height-40, 60, 30)];
[btn1 setBackgroundColor:[UIColor grayColor]];
[btn1 setTitle:@"原点" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(onBtnBack) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn1];
[btn1 release];
if ([CLLocationManager locationServicesEnabled] == NO) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"没有GPS服务" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
[alertView show];
return;
}
_localtion = [[CLLocationManager alloc]init];
_localtion.delegate = self;
_localtion.desiredAccuracy = kCLLocationAccuracyBest;
[_localtion requestWhenInUseAuthorization];// 前台定位
[_localtion startUpdatingLocation];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)onBtnPicture
{
UIImagePickerController* picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
NSLog(@"press btn!");
[picker release];
}
-(void)onBtnCapture
{
UIImagePickerController* picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
NSLog(@"press btn!");
[picker release];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
_imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];;
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"slelect a picture");
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"canceled");
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
// CLLocation* lastPos = [locations lastObject];
CLLocation* lastPos = [[CLLocation alloc]initWithLatitude:_mapview.userLocation.location.coordinate.latitude longitude:_mapview.userLocation.location.coordinate.longitude];
if ( lastPos.coordinate.latitude == 0 && lastPos.coordinate.longitude == 0) {
return;
}
NSString* text1 = [NSString stringWithFormat:@"平面坐标 = %g : %g, 水平精度 = %g", lastPos.coordinate.latitude, lastPos.coordinate.longitude, lastPos.horizontalAccuracy];
[_lableH setText:text1];
NSString* text2 = [NSString stringWithFormat:@"垂直高度 = %g, 垂直精度 = %g", lastPos.altitude, lastPos.verticalAccuracy];
[_lableV setText:text2];
if (_isNeedGoBack) {
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(lastPos.coordinate, 5000, 5000);
[_mapview setRegion:region animated:YES];
_isNeedGoBack = NO;
}
NSMutableArray* arr = [[NSMutableArray alloc]init];
[arr addObject:lastPos];
CLLocation* nextpos = [[CLLocation alloc]initWithLatitude:39.907001 longitude:116.391001];
[arr addObject:nextpos];
NSLog(@"pos1 = %@", lastPos);
NSLog(@"pos2 = %@", nextpos);
NSLog(@"distance = %0.2f km",[lastPos distanceFromLocation :nextpos]/1000);
[self drawLineWithLocationArray:arr];
}
-(void)onBtnBack
{
_isNeedGoBack = YES;
}
-(void)mapView:(MKMapView *)mapView didAddOverlayRenderers:(NSArray<MKOverlayRenderer *> *)renderers
{
}
-(MKOverlayRenderer*)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolyline* polyLine = (MKPolyline *)overlay;
MKPolylineRenderer *aRenderer = [[MKPolylineRenderer alloc] initWithPolyline:polyLine];
aRenderer.strokeColor = [UIColor blackColor];
aRenderer.lineWidth = 1;
return aRenderer;
}
return nil;
}
- (void)drawLineWithLocationArray:(NSArray *)locationArray
{
NSInteger pointCount = [locationArray count];
CLLocationCoordinate2D *coordinateArray = (CLLocationCoordinate2D *)malloc(pointCount * sizeof(CLLocationCoordinate2D));
for (int i = 0; i < pointCount; ++i) {
CLLocation *location = [locationArray objectAtIndex:i];
coordinateArray[i] = [location coordinate];
}
MKPolyline* line = [MKPolyline polylineWithCoordinates:coordinateArray count:pointCount];
// [_mapview setVisibleMapRect:[line boundingMapRect]];
[_mapview addOverlay:line];
free(coordinateArray);
coordinateArray = NULL;
[_mapview clearsContextBeforeDrawing];
}
@end