您的位置:首页 > 移动开发 > IOS开发

IOS开发 使用地图 MapKit

2016-08-25 19:39 417 查看
现在很多社交、电商、团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的。的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式。例如你到了一个陌生的地方想要查找附近的酒店、超市等就可以打开软件搜索周边;类似的,还有很多团购软件可以根据你所在的位置自动为你推荐某些商品。总之,目前地图和定位功能已经大量引入到应用开发中。今天就和大家一起看一下iOS如何进行地图和定位开发。

 

#import "ViewController.h"

#import <MapKit/MapKit.h>

@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>

{

    MKMapView *mv;

    CLLocationManager *locationManager;

    UITextField *tfLatitude;

    UITextField *tfLongitude;

    CLGeocoder *geocoder;

    CLLocationCoordinate2D coor;

}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    geocoder = [[CLGeocoder alloc] init];

    //初始化CLLocationManager

    locationManager=[[CLLocationManager alloc]init];

    //代理

    locationManager.delegate=self;

    //精确度

    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    locationManager.distanceFilter=1000.f;

    //授权

    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

        [locationManager requestWhenInUseAuthorization];

    }

    [locationManager startUpdatingLocation];

    //初始化MKMapView

    mv=[[MKMapView alloc]initWithFrame:self.view.frame];

    //地图样式

    mv.mapType=MKMapTypeStandard;

    //设置地图滚动

    mv.scrollEnabled=YES;

    //设置地图旋转

    mv.rotateEnabled=YES;

    //设置地图缩放

    mv.zoomEnabled=YES;

    //设置地图显示用户位置

    mv.showsUserLocation=YES;

     //设置代理

    mv.delegate=self;

    //加载

    [self.view addSubview:mv];

    tfLatitude  = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 120, 30)];

    tfLatitude.borderStyle = UITextBorderStyleRoundedRect;

    [mv addSubview:tfLatitude];

    tfLongitude  = [[UITextField alloc] initWithFrame:CGRectMake(170, 20, 120, 30)];

    tfLongitude.borderStyle = UITextBorderStyleRoundedRect;

    [mv addSubview:tfLongitude];

    //创建长按手势

    UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(clickLong:)];

    [self.view addGestureRecognizer:longGR];

}

//长按手势方法

-(void)clickLong:(UILongPressGestureRecognizer *)longGR{

    //获取长按点的坐标

    CGPoint point = [longGR locationInView:mv];

    //将长按点的坐标转换为经、纬度值

    CLLocationCoordinate2D coord = [mv convertPoint:point toCoordinateFromView:mv];

    //将经度、维度值包装为CLLocation对象

    CLLocation *location = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];

    //根据经、纬度反向解析地址

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        if(placemarks.count>0&&error == nil){

        

          //获取解析得到的第一个地址xx

            CLPlacemark *placemark = placemarks[0];

          //获取地址信息中的FormattedAddressLines对应的详细地址

            NSArray *arr = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];

          //创建MKPointAnnotation对象——代表一个锚点

            MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];

            annotation.title = placemark.name;

            annotation.subtitle = arr[0];

            annotation.coordinate = coord;

            [mv addAnnotation:annotation];

        

        }

        

    }];

    

}

//精确获取当前位置

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations

{

    //获取当前位置

    CLLocation *location=[locations lastObject];

    //获取经纬度结构体

    coor=location.coordinate;

    MKCoordinateRegion region=MKCoordinateRegionMake(coor, MKCoordinateSpanMake(0.01, 0.01));

    MKCoordinateRegion coodRegion=[mv regionThatFits:region];

    [mv setRegion:coodRegion animated:YES];

    tfLatitude.text = [NSString stringWithFormat:@"%f",coor.latitude];

    tfLongitude.text = [NSString stringWithFormat:@"%f",coor.longitude];

    

    

}

//锚点自定义方法

//- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{

//

//     static NSString *ka = @"myAnnotation";

//    MKAnnotationView *annotationV = [mapView dequeueReusableAnnotationViewWithIdentifier:ka];

//    if(annotationV == nil){

//    

//        annotationV = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ka];

//    

//    }

//    //为锚点设置图片

//    annotationV.image = [UIImage imageNamed:@"279.png"];

//    //设置该锚点是否显示气泡

//    annotationV.canShowCallout = YES;

//    //定义一个按钮,用于锚点控件设置附加控件

//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

//    //为按钮绑定事件处理方法

//    [btn addTarget:self action:@selector(buttonTapped:)

//     forControlEvents:UIControlEventTouchUpInside];

//    //可通过锚点控件的rightCalloutAccessoryView、leftCalloutAccessoryView设置附加控件

//    annotationV.rightCalloutAccessoryView = btn;

//    

//    return annotationV;

//

//}

-(void)buttonTapped:(id)sender{

    NSLog(@"您点击了锚点信息!");

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: