开发者

iphone gps cllocation and making variables globally accessible

I'm pretty new to iPhone development and have been trying to work out how to include GPS information into an app I'm working on. I've gone through the HelloThere tutorial, which is a great start

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

And had no problems getting this to run on my iPhone. I then took the example and have since been trying to incorporate the GPS info into a much larger and more complicated app. The larger application has an existing function which will send a post request to the server, and I'd like to simply provide the location data, specifically the coordinate.latitude and coordinate.longitude to this fun开发者_JS百科ction, if possible without altering it.

This is pretty trivial in the other languages I've worked with but it's turned out to be quite challenging in objective C.

Basically, as per the tutorial I have gotten to the point where I'm logging the location info,


//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];
}

but I can't figure out how I can then make the locLat and locLong variables available to other parts of the application. Pretty lame but I'm still a bit lost with objective C.


There are many ways to do this. The quick and dirty way (and some will frown upon it) is to just declare those as globals in this file and use extern to access them from other files.

Better is to make those @properties of the class, and provide a getter so you can access those from another class or part of the app. That does assume that this class will be available for other classes to access later on.

You also can use delegate to get information. And...

Thinking a bit more, I would probably store data like this someplace else, and will use this routine to update the value in that location (by using a setter of that class), so this method here would just get the location and then store it elsewhere.

You might want to read Scott Knaster's book on Objective C and Mac development for a primer on Obj C.


Here's how I recommend doing it:

Store lat/long in a dictionary and fire them off as strings bundled in a notification. Setup an observer in the application delegate and have the callback function store the lat/long in class properties of the application delegate and/or store them in the application defaults.

In your class where you acquire the coordinates:

- (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];
}

In your application delegate class:

- (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"]);

    }

Dont forget to setup the class properties in the application delegate header file as NSString. You can then access the coordinates by calling directly from the application delegate:

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

Or you can access them anywhere from the user defaults:

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

I hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜