【问题标题】:Location is displayed once位置显示一次
【发布时间】:2014-12-26 13:35:54
【问题描述】:

我只是在玩 CLLocation 类。我成功完成了以下代码。当我按下按钮时,它会在 ALert msg 中显示位置。当我再次单击相同的按钮时,位置显示错误。而且我还注意到每次我必须去设置 - 隐私 - 位置服务以始终启用访问。当应用程序正确显示当前位置时,iPhone 设置中的位置访问会变为空白。我必须再次将其设置为 Always 才能运行该应用程序。我没有在 .plists 中添加任何内容。我只写了下面的代码。我的问题是将位置访问设置为始终后,它才第一次显示正确的位置

-(CLLocationCoordinate2D) getLocation{
        CLLocationCoordinate2D coordinate;


        if ([CLLocationManager locationServicesEnabled])
        {


        CLLocationManager *locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLDistanceFilterNone;
            [locationManager requestWhenInUseAuthorization];
        [locationManager startUpdatingLocation];



        CLLocation *location = [locationManager location];
        coordinate = [location coordinate];
        }
            return coordinate;

    }


    - (void)startTimer {

       // if ([Run isEqualToString:@"can"]) {

            i++;

            NSLog(@"the i value %d", i);

            Run =@"cant";
        CLLocationCoordinate2D coordinate = [self getLocation];

        CLGeocoder *ceo = [[CLGeocoder alloc]init];
        CLLocation *loc = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude]; //insert your coordinates

        [ceo reverseGeocodeLocation:loc
                  completionHandler:^(NSArray placemarks, NSError error)
         {
             CLPlacemark *placemark = [placemarks objectAtIndex:0];

             NSDictionary *eventLocation = [NSDictionary dictionaryWithObjectsAndKeys:placemark.name,@"NAME",placemark.subLocality,@"SUBLOCALITY" ,placemark.postalCode,@"POSTALCODE" ,nil];

           LocationDetails= [NSMutableDictionary dictionaryWithObjectsAndKeys:eventLocation,@"value", nil];


             NSString *name=placemark.name;
             NSString *subLocality=placemark.subLocality;
             NSString *postalCode=placemark.postalCode;



             NSMutableArray *listData;
             if (!listData) listData = [[NSMutableArray alloc] init];
             [listData addObject:LocationDetails];

             NSLog(@"the Location details %@", listData);

             NSString *location=[NSString stringWithFormat:@"You are now in %@ inside the sublocality of %@ and pin code is %@",name,subLocality,postalCode];


             UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Location Update" message:location delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

             [Alert show];


         }

         ];

      //  }

        // contains the code you posted to start the timer
    }






    - (IBAction)getLocation:(id)sender {

        [ self startTimer];

    }

【问题讨论】:

    标签: objective-c iphone ios8 cllocationmanager cllocation


    【解决方案1】:

    不要使用getLocation 方法,而是在类初始化时执行所有CLLocationManager 初始化。在你的@interface 中设置一个属性来保存用户的当前位置:

    @property (nonatomic, strong) CLLocation *currentLocation;
    

    并像这样实现委托方法-locationManager:didUpdateLocations:

    - (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations
    {
        // set the current location. The most recent location is always at the end of the
        // if there are multiple locations
        _currentLocation = locations[locations.count - 1];
    }
    

    然后,而不是CLLocationCoordinate2D coordinate = [self getLocation];,获取位置:

    CLLocationCoordinate2D coordinate = _currentLocation.coordinate; // I think that's the right property.
    

    注意:确保你的类符合CLLocationManagerDelegate

    【讨论】:

      猜你喜欢
      • 2019-09-28
      • 1970-01-01
      • 2023-02-25
      • 2011-03-30
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多