PDA

View Full Version : Problem to read user location into coordinate variables




MACloop
Jul 7, 2009, 01:51 PM
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...?

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!



dejo
Jul 7, 2009, 02:01 PM
You probably want to wait until locationManager:didUpdateToLocation:fromLocation: is triggered before retrieving the lat/long.

MACloop
Jul 7, 2009, 03:20 PM
You might be right - but how do I do that?

dejo
Jul 7, 2009, 03:40 PM
I'd check out the LocateMe sample app (http://developer.apple.com/iphone/library/samplecode/LocateMe/index.html) for some good ideas.

MACloop
Jul 8, 2009, 04:27 AM
I'd check out the LocateMe sample app (http://developer.apple.com/iphone/library/samplecode/LocateMe/index.html) 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

dejo
Jul 8, 2009, 11:25 AM
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.