开发者

Returning a users lat lng as a string iPhone

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开发者_如何学Python_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 :)


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.


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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜