map log

//

//  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



/*
在info.plist中添加子项,NSLocationWhenInUseUsageDescription,NSLocationAlwaysUsageDescription,设置为boolean类型,yes
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值