【问题标题】:iphone gps cllocation and making variables globally accessibleiphone gps cllocation并使变量全局可访问
【发布时间】:2009-11-02 06:56:08
【问题描述】:

我是 iPhone 开发的新手,我一直在努力研究如何将 GPS 信息包含到我正在开发的应用程序中。 我已经完成了 HelloThere 教程,这是一个很好的开始

http://www.mobileorchard.com/hello-there-a-corelocation-tutorial/

在我的 iPhone 上运行它没有任何问题。然后我举了这个例子,并且一直试图将 GPS 信息整合到一个更大、更复杂的应用程序中。较大的应用程序有一个现有的函数,它将向服务器发送一个发布请求,我想简单地提供位置数据,特别是这个函数的坐标.纬度和坐标.经度,如果可能的话而不改变它。
这在我使用过的其他语言中非常简单,但在 Objective C 中却被证明是相当具有挑战性的。
基本上,按照教程,我已经到了记录位置信息的地步,


//GPS stuff
- (void)locationUpdate:(CLLocation *)location {
 //locationLabel.text = [location description];
 locationString = [location description];
 locationLabel.text = locationString;
 locLat  = [NSString stringWithFormat:@"%lf", location.coordinate.latitude];
 locLong = [NSString stringWithFormat:@"%lf", location.coordinate.longitude];
}

但我不知道如何才能使 locLat 和 locLong 变量可用于应用程序的其他部分。很蹩脚,但我还是有点迷失了目标C。

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    有很多方法可以做到这一点。快速而肮脏的方法(有些人会皱眉头)是在这个文件中将它们声明为全局变量,然后使用 extern 从其他文件中访问它们。

    更好的是创建类的那些@properties,并提供一个getter,以便您可以从另一个类或应用程序的一部分访问它们。这确实假设这个类稍后可供其他类访问。​​

    您也可以使用委托来获取信息。还有……

    再想一想,我可能会将这样的数据存储在其他地方,并使用这个例程来更新那个位置的值(通过使用那个类的 setter),所以这里的这个方法只会获取位置和然后将其存储在其他地方。

    您可能想阅读 Scott Knaster 关于 Objective C 和 Mac 开发的书,以了解有关 Obj C 的入门知识。

    【讨论】:

    • 感谢您的提示。我确实尝试将 locLat 和 locLong 变量设置为属性,这允许我编译我的代码,但它会导致运行时异常。我什至还不确定如何将变量设置为全局变量!除了你提到的那本书,你还有其他更简洁的教程可以推荐吗?
    • Obj C 中的全局变量与 C 相同。我不能说运行时异常是来自此还是其他。我需要更多信息。试试 Apple 网站上的 Obj C 教程,但如果您不懂 C,它们也无济于事。
    • @bbb - 运行时异常(可能)是因为您创建的字符串是自动释放的。向他们发送保留消息(并记住在您的 dealloc 中释放它们)。
    【解决方案2】:

    我建议这样做:

    将 lat/long 存储在字典中,并将它们作为捆绑在通知中的字符串触发。在应用程序委托中设置观察者,并让回调函数在应用程序委托的类属性中存储纬度/经度和/或将它们存储在应用程序默认值中。

    在您获取坐标的班级中:

    - (void)locationUpdate:(CLLocation *)location {
        NSString *locationString, *locLat, *locLong;
        locationString = [location description];
        locLat  = [NSString stringWithFormat:@"%lf", location.coordinate.latitude];
        locLong = [NSString stringWithFormat:@"%lf", location.coordinate.longitude];
        NSDictionary *locationDictionary = [NSDictionary dictionaryWithObjectsAndKeys:locationString, @"description", 
                        locLat, @"latitude", locLong, @"longitude", nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"updateSearchLocation" 
                        object:self userInfo:locationDictionary];
    }
    

    在您的应用程序委托类中:

    - (void)applicationDidFinishLaunching:(UIApplication *)application {
        // Listen for search coordinates broadcast
        [[NSNotificationCenter defaultCenter] addObserver:self
                    selector:@selector(setCoordinates:)
                name:@"updateSearchLocation" object:nil];
    }
    
        - (void)setCoordinates:(id)sender {
            self.latitude = [[sender userInfo] objectForKey:@"latitude"];
            self.longitude = [[sender userInfo] objectForKey:@"longitude"];
            NSLog(@"location = %@", [[sender userInfo] objectForKey:@"description"]);
    
        }
    

    不要忘记将应用程序委托头文件中的类属性设置为 NSString。然后,您可以通过直接从应用程序委托调用来访问坐标:

    YourAppDelegateClassName *appDelegate = [[UIApplication sharedApplication] delegate];
    NSLog(@"lat = %@, long = %@", appDelegate.latitude, appDelegate.longitude);
    

    或者您可以从用户默认值的任何位置访问它们:

    [[NSUserDefaults standardUserDefaults] objectForKey:@"latitude"];
    [[NSUserDefaults standardUserDefaults] objectForKey:@"longitude"];
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2012-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多