Problem to read user location into coordinate variables

Discussion in 'iOS Programming' started by MACloop, Jul 7, 2009.

  1. MACloop macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #1
    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!
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    You probably want to wait until locationManager:didUpdateToLocation:fromLocation: is triggered before retrieving the lat/long.
     
  3. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  5. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #5
    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
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    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.
     

Share This Page