所以我有一个模型对象,它是一个名为'location‘的对象,它有一组属性。其中之一是'distanceFromUserLocation'.显然,要知道这一点,它需要知道用户的位置,所以在模型对象中,我正在计算用户的当前位置。
这还会在此模型上设置一个CLLocation属性,每次在didUpdateToLocation方法上。
我在模型上还有一个initWithDictionary方法,它为位置模型上的属性设置所有的值。
下面是我的模型的代码:
@synthesize name = _name;
@synthesize entryLocation = _entryLocation;
@synthesize address = _address;
@synthesize contactNumber = _contactNumber;
@synthesize distanceFromUserLocation = _distanceFromUserLocation;
@synthesize itemDescription = _itemDescription;
@synthesize locationManager = _locationManager;
@synthesize userLocation = _userLocation;
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (newLocation != nil) {
self.userLocation = newLocation;
NSLog(@"lat: %@ lon: %@ ", [NSString stringWithFormat:@"%.7f", newLocation.coordinate.latitude], [NSString stringWithFormat:@"%.7f", newLocation.coordinate.longitude]);
}
[manager stopUpdatingLocation];
}
-(id)initWithDictionary:(NSDictionary*) dictionary
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
self = [super init];
if (self)
{
self.name = [dictionary valueForKey:@"Name"];
self.address = [dictionary valueForKey:@"Address"];
self.contactNumber = [dictionary valueForKey:@"ContactNumber"];
self.itemDescription = [dictionary valueForKey:@"Description"];
double lat = [[dictionary valueForKey:@"Latitude"] doubleValue];
double lon = [[dictionary valueForKey:@"Longitude"] doubleValue];
self.entryLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
self.distanceFromUserLocation = [self.userLocation distanceFromLocation:self.entryLocation];
}
return self;
}我的问题是,在didUpdateToLocation,中,我将'self.currentLocation‘属性设置为newLocation.的值。然后,我记录lat和long,以确保我确实有一个值,这就是我所做的。
但是,当我从控制器类初始化“Location”模型的实例时,然后在该模型对象上记录currentLocation的值:
NSLog(@"%@", location.currentLocation);我在日志中得到(null)。
如果我试着:
NSLog(@%@", location.currentLocation.coordinate.latitude);我得到一个EXC_BAD_ACCESS错误。
我正在成功地获取didUpdateToLocation中的当前位置并对其进行loggint,但是为什么当我将currentLocation属性设置为newLocation值时,它没有被保留呢?
如能对此提供任何帮助,将不胜感激:)
发布于 2011-10-10 11:54:09
因此,EXC_BAD_ACCESS错误的原因在于joost和JBat100的建议。只是使用%@而不是%f。但是,我确实弄明白了为什么我无法从控制器上的属性中获取用户位置值。这些方法是异步调用的,forRowAtIndexPath方法在找到用户位置之前正在呈现表单元格,因此当调用用户位置时,指向用户位置的指针没有任何指向。
我通过添加一个带有活动指示器的视图来解决这个问题,该视图查找用户位置,将其保存到属性中,然后将带有表的视图推到屏幕上,这样设置了它的userlocation属性,从而使该属性永远不会为空:)
谢谢大家的帮助!
发布于 2011-10-06 10:11:57
location.currentLocation.coordinate.latitude类型为CLLocationDegrees,定义为
typedef double CLLocationDegrees;您只能对继承自NSObject的类实例使用%@ (一般情况下)。你应该打印出像这样的双份:
NSLog(@"%f", location.currentLocation.coordinate.latitude);当你使用访问器
self.userLocation = newLocation;如果将属性指定为retain,则保留该值。
发布于 2011-10-06 10:21:23
发送给locationManager:didUpdateToLocation:fromLocation:的值(我希望您正在使用该方法,而不是不太可靠的MKMapView委托方法)不太可靠。在理论上,您应该得到一个逐步更准确的值的连续选择。但是,经常会发送一个零值,正如您注意到的,并且正在检查代码中的值;但是0.0,0.0的一个坐标也经常被发送,您应该检查并忽略这一点。检查horizontalAccuracy和verticalAccuracy属性中的负值并拒绝这些CLLocation属性也是一个好主意。
https://stackoverflow.com/questions/7672793
复制相似问题