我刚刚开始使用Objective C进行编码,并且正在创建一个应用程序来跟踪用户位置,并发送一个包含latlng的警报。这段代码没有丝毫完整,但我遇到了一个问题,试图使用我在“viewDidLoad”中创建的“lat”变量作为警报。我在CLLocationManager委托/方法中声明了变量(我真的不知道它叫什么),我不知道如何在其他地方使用它。
- (void)viewDidLoad
{
locationManager =[[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 100.0f;
[locationManager startUpdatingLocation];
UIAlertView *message = [[UIAlertView alloc] initWithTitle: @"Your current Latitude is:"
message:This is where I want to put the variable "lat"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void) locationmanager: (CLLocationManager *) manager
didUpdateToLocation: (CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation
{
NSString *lat = [[NSString alloc] initWithFormat: @"%g",newLocation.coordinate.latitude];
NSString *lng = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];
}
任何帮助将不胜感激!
答案 0 :(得分:0)
简单的解决方案。 你的ViewController .h看起来应该是这样的
@interface ViewController : UIViewController {
NSString *lat;
NSString *lng;
}
然后这就变成了
-(void) locationmanager: (CLLocationManager *) manager
didUpdateToLocation: (CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation
{
lat = [[NSString alloc] initWithFormat: @"%g",newLocation.coordinate.latitude];
lng = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];
NSLog(lat);
NSLog(lng);
}
NSlog simple out将放置在位于您编码的下方的控制台
除去
UIAlertView *message = [[UIAlertView alloc] initWithTitle: @"Your current Latitude is:"
message:This is where I want to put the variable "lat"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
因为只要加载视图就会显示警告
希望这有帮助