zoukankan      html  css  js  c++  java
  • iOS 开发之定位与苹果地图

    iOS 开发之定位与苹果地图

    1.LBS开发

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

      功能: 定位、地图

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

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

    2.地理定位

      2.1 库的配置

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

      2.2 定位功能

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

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

      iOS8.0及以上需添加授权-->

        info.plist -NSLocationAlwaysUsageDescription

        NSLocationWhenInUseUsageDescription  

     //2.定位(获取经纬度信息)
        _manager = [[CLLocationManager alloc] init];
        _manager.delegate = self;    //delegate
        [self testLocation];
    #pragma mark - 2.实现定位
    -(void)testLocation{
    
        //检测定位服务是否可用(重要) -->  iphone 有,ipad 不一定有
        if(![CLLocationManager locationServicesEnabled]){
        
            NSLog(@"定位服务不可用");
            return;
        }
     //请求定位权限(Xcode6和iOS中使用)
        if([UIDevice currentDevice].systemVersion.doubleValue >= 8.0){   //获取当前版本 8.0以上
            [_manager requestAlwaysAuthorization];   //授权(always一旦授权,一直授权)
        }
    //设置
        _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(@"
    地址反编码
    ");
            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 - 定义大头针(类似UITableViewCell *)
    -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    
        //加入复用机制
        static NSString *annotationId = @"annotation";
        //系统的大头针MKPinAnnotationView
        MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationId];
        if(!pin){
        
            pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationId];
        }
        //设置大头针  color
        pin.pinColor = MKPinAnnotationColorPurple;   //color
        pin.animatesDrop = YES;   //设置掉落效果
        pin.canShowCallout = YES;    //显示注释框
        
        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 - 自定义大头针--协议 //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;
    }
  • 相关阅读:
    矩阵遍历 局部处理 661. 图片平滑器
    最小堆make_heap(), pop_heap()和push_heap()
    vector<pair<int, int>>或者有序map
    简单的滑动窗口 643. 子数组最大平均数 I
    js中getYear()和getFullYear()的区别
    异常:No WebApplicationContext found: no ContextLoaderListener registered?
    异常:Duplicate key or integrity constraint violation message from server: "Duplicate entry '0' for key 1"
    异常:the type net.sf.hibernate.lockmode cannot be resolved, it is indirectly referenced from required .class files
    使用cxf写web service的简单实例
    struts1多文件上传、下载实例
  • 原文地址:https://www.cnblogs.com/wlrBlogs/p/4431067.html
Copyright © 2011-2022 走看看