3

位置情報を取得して表示するだけの簡単な iPhone アプリを作成しました。distanceTraveled位置が変わるたびに変数をインクリメントしたい。私の問題は、メソッド内locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locationsで ,locations.countが常に 1 に等しいことです。メソッドが呼び出されるたびに配列に要素が追加されると思っていましたが、そうではないようです...

シミュレーターでアプリケーションを実行しています (現在デバイスを持っていません)。

これは私のコードです:

// BIDViewController.h

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

@interface BIDViewController : UIViewController <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLLocation *startingPoint;

@property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (strong, nonatomic) IBOutlet UILabel *horizontalAccuracyLabel;
@property (strong, nonatomic) IBOutlet UILabel *altitudeLabel;
@property (strong, nonatomic) IBOutlet UILabel *verticalAccuracyLabel;
@property (strong, nonatomic) IBOutlet UILabel *distanceTraveledLabel;

@end



// BIDViewController.m :

#import "BIDViewController.h"

@interface BIDViewController ()

@property (strong, nonatomic)CLLocation *currentLocation;
@property (strong, nonatomic)CLLocation *lastLocation;
@property CLLocationDistance distanceTraveled;

@end

@implementation BIDViewController

@synthesize latitudeLabel;
@synthesize locationManager;
@synthesize longitudeLabel;
@synthesize startingPoint;
@synthesize altitudeLabel;
@synthesize distanceTraveledLabel;
@synthesize horizontalAccuracyLabel;
@synthesize verticalAccuracyLabel;
@synthesize currentLocation;
@synthesize lastLocation;
@synthesize distanceTraveled;

- (void)viewDidLoad
{
    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //locationManager.distanceFilter = 10.0f;

    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    if (startingPoint == nil) {
        startingPoint = [locations lastObject];
        NSLog(@"first time -- locations.count = %d", locations.count);
        distanceTraveled = 0;
        distanceTraveledLabel.text = @"0m";
    }
    else if (locations.count > 1) {
        NSLog(@"into the else if -- locations.count = %d", locations.count);
        lastLocation = locations[locations.count -2];
        distanceTraveled = distanceTraveled + [currentLocation distanceFromLocation:lastLocation];
        distanceTraveledLabel.text = [NSString stringWithFormat:@"%gm", distanceTraveled];
    }

    NSLog(@"locations.count = %d", locations.count);

    currentLocation = [locations lastObject];

    latitudeLabel.text =
        [NSString stringWithFormat:@"%g\u00B0", currentLocation.coordinate.latitude];
    longitudeLabel.text =
        [NSString stringWithFormat:@"%g\u00B0", currentLocation.coordinate.longitude];
    horizontalAccuracyLabel.text =
        [NSString stringWithFormat:@"%gm", currentLocation.horizontalAccuracy];
   verticalAccuracyLabel.text =
        [NSString stringWithFormat:@"%gm", currentLocation.verticalAccuracy];
    altitudeLabel.text =
        [NSString stringWithFormat:@"%gm", currentLocation.altitude];

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSString *errorString = [[NSString alloc] init];

    switch (error.code) {
        case kCLErrorLocationUnknown:
            errorString = @"Location unknown";
            break;

        case kCLErrorDenied:
            errorString = @"Access denied";
            break;

        case kCLErrorNetwork:
            errorString = @"No network coverage";
            break;

        case kCLErrorDeferredAccuracyTooLow:
            errorString = @"Accuracy is too low to display";
            break;

        default:
            break;
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error getting location"
                                                    message:errorString
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

@end

ロガーは次のようになります。

2012-11-21 11:28:34.882 WhereAmI[652:11c03] first time -- locations.count = 1
2012-11-21 11:28:34.897 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:35.835 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:36.835 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:37.836 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:38.836 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:39.837 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:40.837 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:41.838 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:42.839 WhereAmI[652:11c03] locations.count = 1

等々...

何かアドバイス?みんなありがとう。いつものように、下手な英語でごめんなさい!

4

1 に答える 1

2

シミュレーターはコンピューターの場所を使用するため、複数の場所を返すことはありません。したがって、Wi-Fiの精度が得られます。実行中に別の場所で別のWi-Fiネットワークに変更しない限り、これ以上の精度は返されません。 /別の場所。

移動する実際のデバイスでテストしている場合、またはデバイスが最初にセルタワーの精度、次にWi-Fi、次にGPSの精度を提供する場合は、アレイ内の複数の場所を受信します。

これがそれをクリアすることを願っています。

于 2012-11-21T13:12:28.103 に答える