将用户lat lng作为字符串iPhone返回

时间:2010-05-03 01:34:50

标签: iphone core-location

有没有办法从模型中将用户位置作为字符串返回?

我有一个模型,工作是从Web服务下载相同的JSON数据。在我的请求中发送时,我需要将?lat = LAT_HERE& lng = LNG_HERE添加到字符串的末尾。

我看过很多使用地图或不断更新标签的例子。但我无法找到如何显式返回lat和lng值。

我只有2天进入iPhone开发,所以对我来说很容易:)

2 个答案:

答案 0 :(得分:2)

您需要利用核心位置,特别是CLLocationManager。 Apple没有提供任何CL编程指南,因此请查看其中一个示例,如LocateMe,了解如何执行此操作。

答案 1 :(得分:1)

您需要像这样使用CLLocationManager:

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