2011-11-21 76 views
2

我在应用程序位于后台时发送位置时遇到问题。我正在使用CLLocationManager和startMonitoringSignificantLocationChanges。 postion didUpdateToLocation委托方法执行一次,但不是更多。我试着走过去,但没有新的位置被发送到服务器。当iphone位于后台时用CLLocationManager发送位置到服务器

我已经在info.plist文件中设置了“所需的背景模式” - >“位置更新的应用程序注册”。

任何人都知道什么可能是错的?从跟踪被启动,其中

代码:

CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = appDelegate; 
[appDelegate setLocationManager:locationManager withDistanceFilter:kCLDistanceFilterNone]; 
[appDelegate.theLocationManager startMonitoringSignificantLocationChanges]; 

代码(从CLLocationManagerDelegate):

- (void)setLocationManager:(CLLocationManager*)locationManager withDistanceFilter:(CLLocationDistance)distanceFilter { 

    // create a new manager and start checking for sig changes 
    self.theLocationManager.delegate = nil; 
    [theLocationManager release]; 

    self.theLocationManager = locationManager; 
    self.theLocationManager.delegate = self; 
    self.theLocationManager.distanceFilter = distanceFilter; 
} 


    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 

     NSDate *newLocationTimestamp = newLocation.timestamp; 
     NSDate *oldLocationTimestamp = oldLocation.timestamp; 

     int locationUpdateInterval = 15;//15 sec 

     if (!([newLocationTimestamp timeIntervalSinceDate:oldLocationTimestamp] < locationUpdateInterval)) { 
       //NSLog(@"New Location: %@", newLocation); 
       [self updateToLocation:newLocation]; 
     } 

    } 

    - (void)updateToLocation:(CLLocation *)newLocation { 
     NSLog(@"update location!!"); 

     NSString *latitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].latitude]; 
     NSString *longitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].longitude]; 

     [currentUser updatePositionWithLongitude:longitude andLatitude:latitude]; 
    } 
+2

多远你走动?根据您的位置,您可能需要覆盖更远距离 –

+0

hm,不远处。我会尝试通过进一步行走来测试它。 1公里左右应该足够还是? – Madoc

+2

也许只是将'startMonitoringSignificantLocationChanges'替换为'startUpdatingLocation'并查看它是否按预期运行......然后您将排除我读过的其他地方的其他问题 –

回答

3

就像比尔·布拉斯基所说的,你设置位置管理器的准确性可能不会记录你走过的距离。尝试设置您的位置管理器的精度要高得多,只是为了查看是否有效,然后将其拨回精确度和电池效率之间的中等程度。只是用于测试,把它所有的方式:

[appDelegate.theLocationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation]; 

然后代替:

[appDelegate.theLocationManager startMonitoringSignificantLocationChanges]; 

尝试:

[appDelegate.theLocationManager startUpdatingLocation]; 
+0

谢谢,我现在使用startUpdatingLocation,它按预期工作。我仍然需要调整一下,因为电池效率,但我会最终得到它:) – Madoc

+0

超级!很高兴听到这对你有用。 –

2

的-startMonitoringForSignificantLocationChanges是直接关系到手机信号塔的连接。您可能需要行驶里程才能连接到新塔楼并触发位置更改事件。我知道区域监控更准确一些,因为它使用Wifi,手机信号塔,甚至其他查询位置的应用程序的位置更新。您需要弄清楚您的应用程序的准确性和频率。您可能需要在后台主动监控位置(肯定会成为电池杀手)。希望这可以帮助。

+0

非常感谢你。 – Madoc

相关问题