couldn't find latitude and longitude of my iphone

时间:2022-02-19 16:04:21

i am trying to find out iphone's current location and for this I am using this method. and try to save it for further procedure. but when I print value of location in form of latitude and longitude It always giving me 0.00000 and 0.0000. I am not getting the point that where I am wrong.this method is save in GetLocation class.

我试图找出iphone的当前位置,为此我使用这种方法。并尝试保存它以进行进一步的操作。但是当我以纬度和经度的形式打印位置值时它总是给我0.00000和0.0000。我没有说明我错了。这个方法保存在GetLocation类中。

-(void)updatelocation{

NSLog(@"coming in update location    in get location class");
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];

NSLog(@"location manager   %@", locationManager);
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

NSLog(@"Latitude is: %f\n\n\n and longitude is: %f\n\n\n", location.coordinate.latitude, location.coordinate.longitude);
NSLog(@"coordinate   %f",coordinate.latitude);
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:latitude forKey:@"latitude"];
[defaults setObject:longitude forKey:@"longitude"];

}

when I call this method in another class like this.

当我在另一个类中调用此方法时。

GetLocation *location = [GetLocation alloc] init ];
[location updatelocation];

but I always get 0.0000 and 0.0000. can anybody tell me that where I am wrong.

但我总是得到0.0000和0.0000。任何人都可以告诉我,我错了。

2 个解决方案

#1


2  

Look at the -[CLLocation coordinate] documentation first...and even this one...

首先看一下 - [CLLocation coordinate]文档......甚至这一个......

#2


3  

You are going too fast, the CLLocationManager hasn't determined your location yet. You should implement CLLocationManagerDelegate's locationManager:didUpdateToLocation:fromLocation: method. In that method, the user's location is known.

您的速度太快,CLLocationManager尚未确定您的位置。您应该实现CLLocationManagerDelegate的locationManager:didUpdateToLocation:fromLocation:方法。在该方法中,用户的位置是已知的。

#1


2  

Look at the -[CLLocation coordinate] documentation first...and even this one...

首先看一下 - [CLLocation coordinate]文档......甚至这一个......

#2


3  

You are going too fast, the CLLocationManager hasn't determined your location yet. You should implement CLLocationManagerDelegate's locationManager:didUpdateToLocation:fromLocation: method. In that method, the user's location is known.

您的速度太快,CLLocationManager尚未确定您的位置。您应该实现CLLocationManagerDelegate的locationManager:didUpdateToLocation:fromLocation:方法。在该方法中,用户的位置是已知的。