So I need my delegate updated with a new location when ever the device has moved 50 metres or more. This is part of an end of chapter quiz in a book I'm learning with.
因此,当设备移动50米或以上时,我需要我的委托更新一个新的位置。这是我正在学习的一本书的期末考试的一部分。
Here is my code:
这是我的代码:
- (id)initWithCoder:(NSCoder *)aDecoder //we're overriding the superclasses (UIViewController) initialiser
{
self = [super initWithCoder:aDecoder];
if (self){
//create location manager object
locationManager = [[CLLocationManager alloc] init];
//there will be a warning from this line of code
[locationManager setDelegate:self];
//and we want it to be as accurate as possible
//regardless of how much time/power it takes
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//set the amount of metres travelled before location update is made
[locationManager setDistanceFilter:50];
//tell our manager to start looking for its location immediately
[locationManager startMonitoringSignificantLocationChanges];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
NSLog(@"%@", [locations lastObject]);
}
I have set the distance filter to 50 (I hope that corresponds to 50 metres). Then when printing to console I've called the last object in the array which I hope would be the most recently updated location.
我将距离滤波器设置为50(我希望它对应50米)。当打印到控制台时,我调用了数组中的最后一个对象,我希望这是最近更新的位置。
I then build and run and the initial location is printed to console. I then click debug > location > city run
in my iOS simulator menu. I guess this takes the location passt 50 metres as a city run would definitely be more than 50 metres. The location is updated again. I try to update the location again by clicking freeway drive and nothing happens.
然后,我构建并运行并将初始位置打印到控制台。然后在iOS模拟器菜单中单击debug >位置>。我想这大概要花50米的距离,因为一个城市的跑步肯定会超过50米。位置再次更新。我尝试通过点击高速公路来更新位置,但是什么也没有发生。
In the docs it states: "It does not rely on the value in the distanceFilter property to generate events. Calling this method several times in succession does not automatically result in new events being generated"
在文档中,它声明:“它不依赖于远程筛选器属性的值来生成事件。连续多次调用此方法不会自动生成新的事件。
This is exactly what I'm experiencing. I can change location once and have the new location printed to console.. however changing the location again results in no changes.
这正是我正在经历的。我可以更改位置一次,并将新的位置打印到控制台。但是,更改位置将不会导致任何更改。
I thought the -locationManager:didUpdateLocations:
updates console with the new location every time it's updated. Do I need to do this some other way?
我认为-locationManager:didUpdateLocations:每次更新时都使用新位置更新控制台。我还需要用别的方法吗?
Hope you can help Kind regards
希望你能帮个忙。
1 个解决方案
#1
1
You forgot to call: startUpdatingLocation
like this:
你忘了打电话:startUpdatingLocation:
if ([CLLocationManager locationServicesEnabled]) {
[self.locationManager startUpdatingLocation];
}
#1
1
You forgot to call: startUpdatingLocation
like this:
你忘了打电话:startUpdatingLocation:
if ([CLLocationManager locationServicesEnabled]) {
[self.locationManager startUpdatingLocation];
}