从locationManager获取lat / long:使用CoreLocation的didUpdateLocations

时间:2013-07-09 23:39:44

标签: ios objective-c core-location

我看过类似的问题和例子,但我似乎无法从CoreLocation获得经度和经度,因为如果我直接在viewDidLoad中查看位置,它就是0/0。我在委托方法中有一个断点来访问位置,但我从来没有打过它。我也符合CLLocationmanagerDelegate。

代码(ViewController.m):

- (void)viewDidLoad
{
    [super viewDidLoad];

    _locationManager = [[CLLocationManager alloc]init];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
    _locationManager.distanceFilter = kOneMile; //custom defined this one
    [_locationManager startUpdatingLocation];
}

//Actual update of the location
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    //Not sure if this is the best way to grab it?
    CLLocation *location = [locations objectAtIndex:0];

    //Breakpoint shows this as zero here
    [Utilities GetCityAndState:location.coordinate.latitude andLong:location.coordinate.longitude];
}

在我的实用工具课程中

+(NSString *)GetCityAndState:(float)withlat andLong:(float)longitude{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
NSString *urlString = @"http://maps.googleapis.com/maps/api/geocode/json?latlng=";
//[urlString ] I am going to append the lat and lont to the urlString here 
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:urlString]];
NSHTTPURLResponse *responseCode = nil;
NSError *error = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
NSString *get = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

//Once I have that I am going to take the results from the reverse geocoding and return a string like LasAngelesCA
}

2 个答案:

答案 0 :(得分:1)

我不完全确定原因,但在模拟器中删除应用程序并重新运行它修复了问题。

答案 1 :(得分:0)

将声明更改为:

+ (NSString *)getCityAndState:(CLLocationDegrees)withlat andLong:(CLLocationDegrees)longitude;

而不是使用“floats”作为参数。