iPhone Developer Help

Full Version: Core Location Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I got Core Location Framework working. But i've realized there is no way to test this with the damn Aspen Simulator!

When will we finally be able to run our apps on the actual phone?!

Here's some sample code for anyone who wants to get started with Core Location:

Code:
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    locationManager.distanceFilter = 1000;  // 1 kilometer
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    [locationManager startUpdatingLocation];


Code:
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
    fromLocation:(CLLocation *)oldLocation
{
    // Disable future updates to save power.
    [manager stopUpdatingLocation];

     printf("latitude %+.6f, longitude %+.6f\n",
            newLocation.coordinate.latitude,
            newLocation.coordinate.longitude);
}

I've tried getting CoreLocation to work, but the iPhone SDK docs talk about /System/Library/Frameworks/CoreLocation.framework, which isn't on my system. I even have beta 3 of the SDK. Where did you get all the CoreLocation libraries?
Hi haxmeadroom,

Love the name! I am old enough to understand what you are referring to unfortunately!

You can find the CoreLocation libraries here:

/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.0sdk/System/Library/Frameworks/CoreLocation.framework

Just drag the whole folder from Finder to your Frameworks icon in your project. I wouldn't select the checkbox to copy the files, I had issues when I did this. Leaving it alone seemed to work for me.
Hi felixk,

I am just getting started with development (not only in this iPhone SDK environment, but completely), total newbie but have hacked my way through other things so there you go.

I used the code above but made the following mistakes:

1) Tried to put the code in my AppDelegate.m file, really it should be in it's own .h and .m files I think. I named them MyLocationGetter
2) Didn't bring in the Framework, so found it using Spotlight and dragged it under Frameworks folder.
3) First time I brought in the framework I copied the files, but had errors compiling so I brought in without copying and that seemed to work.
4) Compiler complained about locationManager not being initialized properly, so figured out how to add the line to MyLocationGetter.h file:

@interface MyLocationGetter : NSObject {
CLLocationManager *locationManager; <-------
}

I think this is correct, i.e. the compiler likes it, is this right?

My final question now, before I actually try to link up the code to an interface, is that I have one Warning left:

"warning: Class 'MyLocationGetter' does not implement the 'CLLocationManagerDelegate' protocol"

Is this important? If so, how should I add it to the .h file? This is where I am getting stumped right now.

Thanks for any assistance you can provide, and for creating such a great resource that is accessible to newbie developers on the iPhone like myself!
Hi Mahoward,

Welcome to the site. Glad you like it.

1. Yea, AppDelegate shouldn't contain view or controller methods. If you are familiar with MVC (Model-View-Controller) then you will quickly pick up how to create iPhone apps to Apple's standards. Take a look at how to use UIViewController. I'd recommend using that over NSObject for your getter class.
2. I had difficulty finding the CoreLocation framework at first as well. But after searching it came up just like you found it.
3. Right you shouldn't copy the framework. You should just right click on your project and select "SELECT Framework".
4. That looks correct to me.

And finally...the CLLocationManagerDelegate is definitely required. This class is responsible for calling your

" (void)locationManagerSadCLLocationManager *)manager
didUpdateToLocationSadCLLocation *)newLocation
fromLocationSadCLLocation *)oldLocation"

method. To add it all you need to do is add the class to your .h file as so:

@interface MyLocationGetter : NSObject <CLLocationManagerDelegate>

Good luck!
Felix

mahoward Wrote:
Hi felixk,

I am just getting started with development (not only in this iPhone SDK environment, but completely), total newbie but have hacked my way through other things so there you go.

I used the code above but made the following mistakes:

1) Tried to put the code in my AppDelegate.m file, really it should be in it's own .h and .m files I think. I named them MyLocationGetter
2) Didn't bring in the Framework, so found it using Spotlight and dragged it under Frameworks folder.
3) First time I brought in the framework I copied the files, but had errors compiling so I brought in without copying and that seemed to work.
4) Compiler complained about locationManager not being initialized properly, so figured out how to add the line to MyLocationGetter.h file:

@interface MyLocationGetter : NSObject {
CLLocationManager *locationManager; <-------
}

I think this is correct, i.e. the compiler likes it, is this right?

My final question now, before I actually try to link up the code to an interface, is that I have one Warning left:

"warning: Class 'MyLocationGetter' does not implement the 'CLLocationManagerDelegate' protocol"

Is this important? If so, how should I add it to the .h file? This is where I am getting stumped right now.

Thanks for any assistance you can provide, and for creating such a great resource that is accessible to newbie developers on the iPhone like myself!

Thanks for the tip Felix, that did it!
Hello, I am a serious rookie but am very interested in getting this code to work on my iPhone. Would you share exactly what your .h and .m files look like? Thanks

bizkid Wrote:
Hello, I am a serious rookie but am very interested in getting this code to work on my iPhone. Would you share exactly what your .h and .m files look like? Thanks


Hey bizkid, with the new beta 7 there is a core location example for ya.

Cool thnx!

mahoward Wrote:

bizkid Wrote:
Hello, I am a serious rookie but am very interested in getting this code to work on my iPhone. Would you share exactly what your .h and .m files look like? Thanks


Hey bizkid, with the new beta 7 there is a core location example for ya.

Hey MaHoward I am getting 24 errors when I try to build and go. "Can't create directories..." Does this code not work with the simulator? All of the other samples work fine with the simulator when I build and go.
Pages: 1 2 3
Reference URL's