Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
Hello!

I have a problem what I suppose is rather trivial... I have coded an app using the mapkit and have done the development on my Mac using the simulator which has no GPS. Therefore did I hard code the coordinates to simulate a user position. Today as I tried my app on my iPhone for the first time I thought I only had to change my code a bit in order to read the coordinates from the GPS and to get the information about the users position. But it did not work that easy. Does anyone have an idea how to solve this? I need the user longitude and latitude to calculate the distance to another position and I thought I could do something like this...?

Code:
CLLocationManager *clm = [[CLLocationManager alloc]init];
[clm.delegate self]; [clm startUpdatingLocation];

userLocationFromGPS = clm.location.coordinate;
//the userLocationFromGPS is a CLLocationCoordinate2D userLocationFromGPS object

user = [[Annotations alloc]initWithCoordinate:userLocationFromGPS];
[user setTheTitle:@"You are here!"];

userLocationPlain =
[[CLLocation alloc]initWithLatitude:userLocationFromGPS.latitude longitude:userLocationFromGPS.longitude];

I need the coordinates but cant figure out how to get them...any ideas? Thanks in advance!
 
I'd check out the LocateMe sample app for some good ideas.

Thanks, I did check that out but there are some details I did not understand. I have come abit further now and have seen in the console that my locationmanager is called to late in the program flow... this means when the app is opened the user has 0 as both long and lat. Where exactly is correct to call the locationManager? I called in the viewDid load
 
Where exactly is correct to call the locationManager? I called in the viewDid load
What do you mean "call the locationManager"?

I believe you can call startUpdatingLocation in your viewDidLoad and then retrieve your lat/long from within your locationManager:didUpdateToLocation:fromLocation: delegate method.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.