【问题标题】:Can't get location from Wifi connection on iPhone无法从 iPhone 上的 Wifi 连接获取位置
【发布时间】:2012-09-12 00:13:03
【问题描述】:

我正在尝试编写一个应用程序,该应用程序将为公共交通应用程序获取用户位置,当我在地面上时效果很好。当我在地下时,即使我有 wifi 和/或手机信号,位置也不会更新。下面是我正在使用的代码。据我了解,iPhone 只能从 wifi 信号中获取位置,这是不正确的吗?任何帮助将不胜感激,在此先感谢您!

- (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];

}

【问题讨论】:

    标签: iphone gps location wifi locationmanager


    【解决方案1】:

    您在 viewDidLoad 中设置的 locationManager 是您的 CL 位置管理器实例,而当您将 showsUserLocation 设置为 true 时,MapKit 使用它自己的实例。

    因此 MapKit 没有使用您的距离过滤器设置和所需精度,无论如何,您还没有使用 startUpdatingLocation 启动位置管理器。

    所以,尝试启动您的位置管理器实例,然后使用委托方法

    locationManager:(CLLocationManager *)manager didUpdateToLocation:

    了解您的位置经理所说的内容。

    【讨论】:

    • 感谢您的帮助我使用了一些来自苹果的示例代码并进行了更新。我会把它贴在下面,希望这对其他人有帮助!
    【解决方案2】:

    这是我想出的,似乎运行良好需要更多测试。

    - (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
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-14
      • 2011-03-07
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 2014-01-03
      • 2012-10-13
      相关资源
      最近更新 更多