我正在使用 CLLocationManager 获取当前位置,并且正在获取当前位置的纬度和经度值。我没有在我的应用程序中使用 Mapview。因此,每当更改当前位置时,我都想将当前位置更新到服务器。并且每 5 分钟我需要调用 Web 服务并在后台模式和前台模式下将当前位置更新到服务器。
-(void) getCurrentLocation
{
// To get the current location of the place
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest; //100 m
//Start the location manager, then only updates the current location
[locationManager startUpdatingLocation];
}
/In Location manager delegate method, if the location is updated it will call and get the values from the newLocation using CLLocation
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *lat = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *lon = [NSString stringWithFormat:@"%f", coordinate.longitude];
NSLog(@"dLatitude : %@", lat);
NSLog(@"dLongitude : %@",lon);
self.reverseGeocoder = [[[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate] autorelease];
reverseGeocoder.delegate = self;
[reverseGeocoder start];
//Stop the location manager
[locationManager stopUpdatingLocation];
}
每当位置改变时,那个时候我想获得特定位置的纬度和经度。
谢谢!