1

私は、地上にいるときにうまく機能する大量輸送アプリのユーザーの場所を取得するアプリを作成しようとしています。地下にいると、Wi-Fi や携帯電話の信号がある場合でも、場所が更新されません。以下は私が使用しているコードです。私が理解したことから、iPhone は Wi-Fi 信号からのみ位置情報を取得できますが、これは正しくありませんか? どんな助けでも大歓迎です、事前に感謝します!

- (void)viewDidLoad
{
    [super viewDidLoad];

    //********************** Add map ******************************************

    //setup location manager
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];

    //setup map view
    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 160.0f)];
    mapView.showsUserLocation = YES;
    mapView.userTrackingMode = MKUserTrackingModeFollow;

    //run loop in background
    loopTimer = [[NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(tick:) userInfo:nil repeats:YES]retain];

}

// Search for n seconds to get the best location during that time
- (void) tick: (NSTimer *) timer
{
    // Check for valid coordinate
    CLLocationCoordinate2D coord = mapView.userLocation.location.coordinate;
if (!coord.latitude && !coord.longitude) return;

    //get coordinates to update map
    [mapView setRegion:MKCoordinateRegionMake(coord, MKCoordinateSpanMake(0.005f, 0.005f)) animated:NO];

    //update current location in view
    currentLatView.text = [NSString stringWithFormat:@"%.2f", coord.latitude];
    currentLonView.text = [NSString stringWithFormat:@"%.1f", coord.longitude];

}
4

2 に答える 2

0

viewDidLoad で設定した locationManager は CL Location Manager のインスタンスですが、showsUserLocation を true に設定すると、MapKit は独自のインスタンスを使用します。

そのため、距離フィルターと目的の精度の設定は MapKit によって使用されていません。いずれにせよ、startUpdatingLocation でロケーション マネージャーを開始していません。

そのため、ロケーション マネージャー インスタンスを起動してから、デリゲート メソッドを使用してみてください

locationManager:(CLLocationManager *)manager didUpdateToLocation:

あなたの場所のマネージャーが言うことを得るために。

于 2012-09-12T08:20:45.497 に答える
0

これが私が思いついたものです。うまく機能するようで、さらにテストする必要があります。

- (void)viewDidLoad
{
    [super viewDidLoad];

    //********************** Add map ******************************************

    // Create the manager object
    self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;

    // This is the most important property to set for the manager. It ultimately determines how the manager will
    // attempt to acquire location and thus, the amount of power that will be consumed.
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;

    // When "tracking" the user, the distance filter can be used to control the frequency with which location measurements
    // are delivered by the manager. If the change in distance is less than the filter, a location will not be delivered.
    locationManager.distanceFilter = kCLLocationAccuracyBest;

    // Once configured, the location manager must be "started".
    [locationManager startUpdatingLocation];

    //initialize newCoord
    currentCoord = [[CLLocation alloc] initWithLatitude:0 longitude:0];

    //setup map view
    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 160.0f)];
    mapView.showsUserLocation = YES;
    mapView.userTrackingMode = MKUserTrackingModeFollow;

    //create map view
    [self.view addSubview:mapView];

}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    // test that the horizontal accuracy does not indicate an invalid measurement
    if (newLocation.horizontalAccuracy < 0) return;

    // test the age of the location measurement to determine if the measurement is cached
    // in most cases you will not want to rely on cached measurements
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;

    // store all of the measurements, just so we can see what kind of data we might receive
    currentCoord = newLocation;

    [self tick];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    // The location "unknown" error simply means the manager is currently unable to get the location.
    if ([error code] != kCLErrorLocationUnknown) {
        [self stopUpdatingLocation:NSLocalizedString(@"Error", @"Error")];
    }
}

- (void)stopUpdatingLocation:(NSString *)state
{
    [locationManager stopUpdatingLocation];
    locationManager.delegate = nil;
}

- (void) tick
{
    //do stuff here
}
于 2012-09-12T18:40:03.747 に答える