views:

34

answers:

2

Is there a way to return the users location as a string from a model?

I have a model thats job is to download same JSON data from a web service. When sending in my request I need to add ?lat=LAT_HERE&lng=LNG_HERE to the end of the string.

I have seen tons of examples using the map or constantly updating a label. But I cant find out how to explicitly return the lat and lng values.

Im only 2 days into iPhone dev so go easy on me :)

+1  A: 

You need to leverage Core Location, specifically CLLocationManager. Apple doesn't provide any CL programming guide, so just look at one of the samples like LocateMe to see how to do it.

bpapa
A: 

You need to use CLLocationManager like this:

- (void)viewDidLoad 
    {  
        // this creates the CCLocationManager that will find your current location
        CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        [locationManager startUpdatingLocation];
    }

    // this delegate is called when the app successfully finds your current location
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
    {
        // retrieve lat and lng in a string from newLocation.coordinate
    NSString *lat = [NSString stringWithFormat:@"%d", newLocation.coordinate.latitude];
    NSString *lng = [NSString stringWithFormat:@"%d", newLocation.coordinate.longitude];
    }

    // this delegate method is called if an error occurs in locating your current location
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
    {
     NSLog(@"locationManager:%@ didFailWithError:%@", manager, error);
    }
tul697