美文网首页
MapAddLocation 定位

MapAddLocation 定位

作者: 奋斗的小老鼠 | 来源:发表于2016-04-27 20:09 被阅读33次

    首先我们需要导入头文件

    #######import <CoreLocation/CoreLocation.h>

    //经验 在代码正常的情况下,如果在模拟器上定位不到,可以在真机上调试,如果依然定位不到,就是代码问题,有的公司是网络的问题
        //初始化定位管理对象
        self.manager = [[CLLocationManager alloc] init];
        //判断用户是否开启定位服务 (在iOS4.0以后不需要判断)
        BOOL isLocationService = [self.manager locationServicesEnabled];
        if (isLocationService) {
            //说明用户的定位服务已经开启
            //请用户允许当前应用在使用期间和后台都可以使用定位服务。需要在info.plist文件中添加相应的字段来配合该方法的使用 NSLocationAlwaysUsageDescription
            [self.manager requestAlwaysAuthorization];
    //        [self.manager requestWhenInUseAuthorization];//请求使用时定位
            //设置定位的频率(每移动)
            self.manager.distanceFilter = 5.0;
            //设置精确度 (精确度要根据当前应用的需求来定,不是越精确越好,精确度越高越耗电)
            self.manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    //CLLocationAccuracy 的枚举值
    /*
     kCLLocationAccuracyBestForNavigation               最佳导航
     kCLLocationAccuracyBest                            最精准
     kCLLocationAccuracyNearestTenMeters                10米
     kCLLocationAccuracyHundredMeters                   百米
     kCLLocationAccuracyKilometer                       千米
     kCLLocationAccuracyThreeKilometers                 3千米
     
     */
            //指点代理   我们需要遵循协议<CLLocationManagerDelegate>
            self.manager.delegate = self;
    
            //开启定位
            [self.manager startUpdatingLocation];
        }else {
            //弹框 提醒用户在设置中打开定位服务
            NSLog(@"无法获取您的位置信息,为了更好地为您服务,请在设置--隐私--定位服务中开启定位服务");
        }
    

    常用的代理方法

    //定位出现错误的时候
    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
        NSLog(@"定位失败---%@",error.description);
    }
    

    定位获取

    //定位获取
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
        NSLog(@"定位成功了");
        if (locations) {
            //取出最新位置的信息(经纬度)
            CLLocationCoordinate2D lastCoordinate2D = locations.lastObject.coordinate;
            NSLog(@"经度---%f---维度---%f",lastCoordinate2D.longitude,lastCoordinate2D.latitude);
            //通过反地理编码的方式得到具体的地名
            [self.myGeocoder reverseGeocodeLocation:locations.lastObject completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
                //CLPlacemark 该对象中保存着具体的对象的位置信息。
                //从数组中取出区域信息
                CLPlacemark *newPlacemark = placemarks.lastObject;
                NSLog(@"country---%@",newPlacemark.country);
                NSLog(@"address---%@",newPlacemark.addressDictionary);
                NSLog(@"name---%@",newPlacemark.name);
                NSLog(@"thoroughfare---%@",newPlacemark.thoroughfare);
                NSLog(@"ocean---%@",newPlacemark.ocean);
                if ([NSJSONSerialization isValidJSONObject:newPlacemark.addressDictionary]) {
                    NSData *data = [NSJSONSerialization dataWithJSONObject:newPlacemark.addressDictionary options:NSJSONWritingPrettyPrinted error:nil];
                    NSString *printStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                    NSLog(@"%@",printStr);
                }
            }];
        }else {
            NSLog(@"111");
        }
    }
    

    地理编码 是把一个地名转换成为=>location

    -(void)geoCoderWithPlaceName:(NSString *)name{
    //初始化
        CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];
        [myGeocoder geocodeAddressString:name completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            CLPlacemark *newPlacemarks = placemarks.lastObject;
            NSLog(@"qqq经度---%f---纬度---%f",newPlacemarks.location.coordinate.longitude,newPlacemarks.location.coordinate.latitude);
        }];
    }
    
    

    相关文章

      网友评论

          本文标题:MapAddLocation 定位

          本文链接:https://www.haomeiwen.com/subject/ueicrttx.html