这与我的其他question有关,但我正在使用iOS标记打开一个新版本,因为它可能是问题是在本机方面。
问题:位置管理员没有更新位置。我尝试阅读locationManager.location
,它总是给我一个缓存位置。
然后我修改了代码以使用CLLocationManagerDelegate
和-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {}
永远不会被召唤。
我的.h文件:
#import "TiModule.h"
#import <CoreLocation/CoreLocation.h>
#import <CoreMotion/CoreMotion.h>
@interface TiMovementModule : TiModule<CLLocationManagerDelegate>
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, readonly) NSDictionary *currentMovement;
- (void)startMovementUpdates:(id)args;
- (void)stopMovementUpdates:(id)args;
@end
的.m
/**
* Your Copyright Here
*
* Appcelerator Titanium is Copyright (c) 2009-2010 by Appcelerator, Inc.
* and licensed under the Apache Public License (version 2)
*/
#import "TiMovementModule.h"
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"
@interface TiMovementModule ()
@property (nonatomic, retain) CMMotionManager *motionManager;
@end
@implementation TiMovementModule
@synthesize motionManager, locationManager;
#pragma mark Internal
// this is generated for your module, please do not change it
-(id)moduleGUID
{
return @"3d2abdb6-bafb-451c-931d-a979dcc1ea78";
}
// this is generated for your module, please do not change it
-(NSString*)moduleId
{
return @"ti.movement";
}
#pragma mark Lifecycle
-(void)startup
{
// this method is called when the module is first loaded
// you *must* call the superclass
[super startup];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
motionManager = [[CMMotionManager alloc] init];
NSLog(@"[INFO] %@ loaded",self); //this prints
}
-(void)shutdown:(id)sender
{
// this method is called when the module is being unloaded
// typically this is during shutdown. make sure you don't do too
// much processing here or the app will be quit forceably
// you *must* call the superclass
[super shutdown:sender];
}
#pragma mark Cleanup
-(void)dealloc
{
self.motionManager = nil;
self.locationManager = nil;
[super dealloc];
}
#pragma Public APIs
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"I AM HEREEEEEE!!!!!!!"); //this never prints
}
- (void)startMovementUpdates:(id)args
{
NSLog(@"[INFO] starting updates...");
[self.locationManager startUpdatingLocation];
[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical];
NSLog(@"[INFO] started updates."); //this prints
}
- (void)stopMovementUpdates:(id)args
{
[locationManager stopUpdatingLocation];
[motionManager stopDeviceMotionUpdates];
}
- (id)currentMovement
{
NSDictionary *location = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:locationManager.location.coordinate.longitude], @"longitude",
[NSNumber numberWithDouble:locationManager.location.coordinate.latitude], @"latitude",
[NSNumber numberWithDouble:locationManager.location.altitude], @"altitude",
nil];
NSDictionary *rotation = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:motionManager.deviceMotion.attitude.roll], @"roll",
[NSNumber numberWithDouble:motionManager.deviceMotion.attitude.pitch], @"pitch",
[NSNumber numberWithDouble:motionManager.deviceMotion.attitude.yaw], @"yaw",
nil];
NSDictionary *movementData = [NSDictionary dictionaryWithObjectsAndKeys:
location, @"location",
rotation, @"rotation",
nil];
return movementData; // here I pull location and it always gives me cached location.
}
@end
答案 0 :(得分:1)
你应该尝试在这一行上设置一个断点:
[self.locationManager startUpdatingLocation];
看看你是否曾到过那里,因为你的代码中没有证据证明它已被执行。
答案 1 :(得分:1)
答案是位置更新必须从主线程完成。 在Titanium中,你只需要在函数开头调用ENSURE_UI_THREAD。