码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 开发之定位与苹果地图

时间:2015-04-16 21:37:52      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

iOS 开发之定位与苹果地图

1.LBS开发

  需求: 社交类应用 ,  购物类 ,  旅游类。。。

  功能: 定位、地图

  官方提供:定位与地图(国内使用的高德的数据)

  第三方:高德,百度,搜搜,腾讯,谷歌地图。。。

2.地理定位

  2.1 库的配置

  /*****地图定位*****/
    //LBS  -->  Location Based Service
    //1.配置库 --> CoreLocation.framework  (系统的)
    // -->      #import <CoreLocation/CoreLocation.h>

  2.2 定位功能

  定义一个CLLocationManager对象,设置代理,

  检测定位服务是否可用-->设置属性,开始定位

 //2.定位(获取经纬度信息)
    _manager = [[CLLocationManager alloc] init];
    _manager.delegate = self;    //delegate
    [self testLocation];
#pragma mark - 2.实现定位
-(void)testLocation{

    //检测定位服务是否可用(重要) -->  iphone 有,ipad 不一定有
    if(![CLLocationManager locationServicesEnabled]){
    
        NSLog(@"定位服务不可用");
        return;
    }
    //设置
    _manager.distanceFilter = 10;    //设置距离过滤器,距离变化时通知的敏感程度
    _manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;  //设置希望定位的精度
    //开始定位
    [_manager startUpdatingLocation];   //设置代理
    
}
#pragma mark CLLocationManagerDelegate
//CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    //获取位置
    CLLocation *location = manager.location;
    //获取经纬度  CLLocationCoordinate2D(结构体)
    CLLocationCoordinate2D coordinate = location.coordinate;   //
    NSLog(@"long = %f, lat = %f",coordinate.longitude,coordinate.latitude);  //经纬度
    
}

   2.3获取方向

    检测方向服务是否可用-->开启方向服务-->设置代理

#pragma mark - 3.获取方向
-(void)testHeading{

    if(![CLLocationManager headingAvailable]){
    
        NSLog(@"方向服务不可用");   //只能真机测试
        return;
    }
    //开启方向服务
    [_manager startUpdatingHeading];
}
//delegate
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{

//    *  Range:
//    *    0.0 - 359.9 degrees, 0 being true North
    NSLog(@"地理方向 : %f",newHeading.trueHeading);
    NSLog(@"地磁方向 : %f",newHeading.magneticHeading);
    // 360/16 = 22.5
    NSLog(@"%d",(int)(newHeading.trueHeading/22.5));   //...
}

  2.4  地址编码,地址反编码

 

#pragma mark - 4.地址编码与地址反编码
-(void)testGeoCoder{

    //通过地理位置获取经纬度  ---  地址编码(地理位置--> 经纬度)
    //注意: 国家标准规定,涉及到地图的经纬度都要经过加密  ,
    //      苹果定位 定位到的地址不能直接用在苹果中 --> 火星地址
    //      谷歌地球,苹果定位取到的是国际标准地址
    CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
    [geoCoder geocodeAddressString:@"dizhi" completionHandler:^(NSArray *placemarks, NSError *error) {
        
        //取出地址placemask
        for(CLPlacemark *placemask in placemarks){
        
            NSLog(@"mask = %@",placemask);
            NSLog(@"%@ %@ %@ %f %f",placemask.country,placemask.locality,placemask.subLocality,placemask.location.coordinate.longitude,placemask.location.coordinate.latitude);   //country,city,区,经度,纬度
        }
    }];
    
    //地址反编码,通过经纬度获取地理位置
    CLLocation *location = [[CLLocation alloc] initWithLatitude:23.177194 longitude:113.339485];
    
    CLGeocoder *reverseGeocoder = [[CLGeocoder alloc] init];
    [reverseGeocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
       
        NSLog(@"\n地址反编码\n");
        for(CLPlacemark *placemask in placemarks){
            
            NSLog(@"mask = %@",placemask);
            NSLog(@"%@ %@ %@ %f %f",placemask.country,placemask.locality,placemask.subLocality,placemask.location.coordinate.longitude,placemask.location.coordinate.latitude);   //country,city,区,经度,纬度
        }

    }];
}

3.苹果地图

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    /*****************AppleMap使用********************************/
    //1.配置 -- MapKit.framework
    
    //2.创建、显示  --MKMapView *_mapView;  <MKMapViewDelegate>
    _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    _mapView.delegate = self;
    [self.view addSubview:_mapView];
    
    //3.地图的基本设置
    [self mapConfig];
    
    //4.地图标记功能(添加大头针)  -- 长按添加
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dealLongPress:)];
    [_mapView addGestureRecognizer:longPress];
    //4.1 大头针的定制
    //代理
    //-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation;
    //4.2大头针的高级定制 -- @class  ImageAnnotationView : MKAnnotationView
    
    //5.地图覆盖物Overlay
    //
    MKCircle *circle = [MKCircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(23.1777194, 113.339485) radius:200];
    [_mapView addOverlay:circle];
}
#pragma mark - 3.设置地图
-(void)mapConfig{

    //设置地图的类型
    _mapView.mapType = MKMapTypeHybrid;
    _mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(23.177194, 113.339485), MKCoordinateSpanMake(0.01, 0.01));   //设置显示区域region
//    _mapView.centerCoordinate   //设置中心位置
    _mapView.showsUserLocation = YES;   //显示用户的位置  --> 设置定位当前地址
    
//    _mapView.userLocation      //获取当前用户的位置
//    _mapView.userTrackingMode        //设置跟随模式
}
#pragma mark - 4.长按点击
-(void)dealLongPress:(UILongPressGestureRecognizer *)longPress{

    if(longPress.state == UIGestureRecognizerStateBegan){
    
        //添加大头针--先获取点击位置
        CGPoint point = [longPress locationInView:_mapView];
        //经纬度
        CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView];
        //添加大头针   annotation注释
        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
        annotation.coordinate = coordinate;
        annotation.title = @"我是title";
        annotation.subtitle = [NSString stringWithFormat:@"位置时: %f %f",coordinate.longitude,coordinate.latitude];
        [_mapView addAnnotation:annotation];
    
    }
}
#pragma mark - 自定义大头针--协议
//delegate
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    
    //加入复用机制
    static NSString *annotationId = @"annotation";
    //系统的大头针MKPinAnnotationView
    ImageAnnotationView *pin = (ImageAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationId];
    if(!pin){   
        pin = [[ImageAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationId];
    }//
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    imageView.image = [UIImage imageNamed:@"u=2222741951,1334421495&fm=58.jpeg"];
    pin.leftCalloutAccessoryView = imageView;    //left View
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button addTarget:self action:@selector(dealTouch:) forControlEvents:UIControlEventTouchUpInside];
    pin.rightCalloutAccessoryView = button;   //right View
    
    return pin;
}
#pragma mark - 5.地图覆盖物Overlay
//- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay NS_AVAILABLE(10_9, 7_0);     //iOS8
//以下协议iOS8.0以上不能用
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{

    MKCircleView *view = [[MKCircleView alloc] initWithOverlay:overlay];
    view.fillColor = [[UIColor blueColor] colorWithAlphaComponent:0.2];  //填充色
    view.strokeColor = [[UIColor cyanColor] colorWithAlphaComponent:0.3];  //圆的颜色
    view.lineWidth = 1;
    
    return view;
}

 

iOS 开发之定位与苹果地图

标签:

原文地址:http://www.cnblogs.com/wlrBlogs/p/4431067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!