2013-02-20 50 views
1

我有下面几行代码来定位我的位置,但我总是得到相同的位置,无论我在哪里:CLLocationManager始终位于伦敦

locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; 
[locationManager startUpdatingLocation]; 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 
    CLLocation *location = [locations lastObject]; 
    self.lati = [NSString stringWithFormat:@"%f",location.coordinate.latitude]; 
    self.longi = [NSString stringWithFormat:@"%f",location.coordinate.longitude]; 

    [locationManager stopUpdatingLocation]; 
    locationManager.delegate = nil; 
} 
+0

你在设备测试?或模拟器?您正在测试哪个iOS版本的 – Venkat 2013-02-20 11:49:11

+0

? – tkanzakic 2013-02-20 11:49:59

+0

设备,ios 6.2.1 – davidOhara 2013-02-20 11:50:23

回答

0

使用到位您的委托方法的这种委托方法。它会为你工作。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    myLatitude = newLocation.coordinate.latitude; 
    myLongitude = newLocation.coordinate.longitude; 

    NSLog(@"LAT AND LON IS %f %f",myLatitude,myLongitude); 


} 
0
in .h write 

<CLLocationManagerDelegate> 

then this also 

CLGeocoder *geocoder; 
CLLocationManager *locationManager;   

then in .m 

locationManager = [[CLLocationManager alloc] init] ; 

locationManager.delegate = self; 
[CLLocationManager locationServicesEnabled]; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
locationManager.distanceFilter = kCLHeadingFilterNone; 
[locationManager startUpdatingLocation]; 

and then implement its delegates 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
[manager stopUpdatingLocation]; 

userCoordinate=newLocation.coordinate; 

[[NSUserDefaults standardUserDefaults] setFloat:userCoordinate.latitude 
             forKey:@"userLat"]; 
[[NSUserDefaults standardUserDefaults] setFloat:userCoordinate.longitude 
             forKey:@"userLong"]; 

CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; 
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
    for (CLPlacemark * placemark in placemarks) 
    { 
     NSString *cityName = @""; 
     NSString *countryName = @""; 
     cityName = [placemark.addressDictionary valueForKey:@"City"]; 
     countryName = [placemark.addressDictionary valueForKey:@"Country"]; 


     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
     [prefs setObject:cityName forKey:@"CityName"]; 
     [prefs setObject:countryName forKey:@"CountryName"]; 
    } 
}]; 

[manager stopUpdatingLocation]; 
} 

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
[manager stopUpdatingLocation]; 

if(error.code == kCLErrorDenied) 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"You   have to select \"Allow\" for current location." delegate:self cancelButtonTitle:@"Ok" 
              otherButtonTitles: nil]; 
    alert.tag = 888; 
    [alert show]; 
} 

[[NSUserDefaults standardUserDefaults] setFloat:0.0 
             forKey:@"userLat"]; 
[[NSUserDefaults standardUserDefaults] setFloat:0.0 
             forKey:@"userLong"]; 

[manager stopUpdatingLocation]; 
}