• Core Location实现定位


    2013/4/22记录:

    注意:如非必要,不应该多次轮询位置信息i
    使用时,可根据需要制定精度,通过仔细制定所需的绝对最低精度级别,可以纺织不必要的电池消耗。 
     
    导入:CoreLocation.framework
    导入头文件:#import <CoreLocation/CoreLocation.h>
    实现CLLocationManagerDelegate协议
     
       self.locationManager= [[CLLocationManager alloc] init];    //位置管理器实例
        locationManager.delegate = self;    //设置委托
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;  //设置请求制定精度级别,精度越高越耗电
        [locationManager startUpdatingLocation];    //启动

    回调方法:

    #pragma mark -
    #pragma mark CLLocationManagerDelegate Methods
    //当这个实施时下面那个就失效。
    //- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    //    
    //}
    - (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
               fromLocation:(CLLocation *)oldLocation {
       
        if (startingPoint == nil)   //如果没有开始坐标,选择刚定位到的坐标作为开始坐标
            self.startingPoint = newLocation;
       
        //纬度值
        NSString *latitudeString = [NSStringstringWithFormat:@"%gu00B0",
                                    newLocation.coordinate.latitude];   //“u00B0”:角度符的Unicode表示形式
        latitudeLabel.text = latitudeString;
       
        //经度值
        NSString *longitudeString = [NSStringstringWithFormat:@"%gu00B0",
                                     newLocation.coordinate.longitude];
        longitudeLabel.text = longitudeString;
       
        //以coordinate为中心的圆的半径
        NSString *horizontalAccuracyString = [NSStringstringWithFormat:@"%gm",
                                              newLocation.horizontalAccuracy];
        horizontalAccuracyLabel.text = horizontalAccuracyString;
       
        //海拔
        NSString *altitudeString = [NSStringstringWithFormat:@"%gm",
                                    newLocation.altitude];
        altitudeLabel.text = altitudeString;
       
        //海拔方面的精度
        NSString *verticalAccuracyString = [NSStringstringWithFormat:@"%gm",
                                            newLocation.verticalAccuracy];
        verticalAccuracyLabel.text = verticalAccuracyString;
       
        //更新位置的时间戳
        NSLog(@"%lf
    ",newLocation.timestamp.timeIntervalSince1970);
       
        //两位置距离
        CLLocationDistance distance = [newLocation
                                       distanceFromLocation:startingPoint];
        NSString *distanceString = [NSStringstringWithFormat:@"%gm", distance];
        distanceTraveledLabel.text = distanceString;
       
        //停止
        [self.locationManager stopUpdatingLocation];
    }
    - (void)locationManager:(CLLocationManager *)manager
           didFailWithError:(NSError *)error {
        NSString *errorType = (error.code == kCLErrorDenied) ?
        @"Access Denied" : @"Unknown Error";
        UIAlertView *alert = [[UIAlertViewalloc]
                              initWithTitle:@"Error getting Location"
                              message:errorType
                              delegate:nil
                              cancelButtonTitle:@"Okay"
                              otherButtonTitles:nil];
        [alert show];
    }
  • 相关阅读:
    客户端与服务器持续同步解析(轮询,comet,WebSocket)
    JQuery批量上传插件Uploadify使用详解及参数说明
    win7下安装配置tomcat,java运行环境
    我在Facebook工作的十大经验分享
    jquery submit方法在IE6下的诡异bug
    小练一下canvas版简单时钟与css3版漂亮时钟
    全面解析jquery实现回车键提交表单
    IE8页面加载速度测试
    微软发布Visual Studio 2008 SP1
    开发人员为什么要支持非IE浏览器的四个故事
  • 原文地址:https://www.cnblogs.com/ios-wmm/p/10215050.html
Copyright © 2020-2023  润新知