描述:CoreLocation框架是用来确定物理位置的框架,借助GPS,WiFI,基站进行定位,可以选择不同精度进行定位。 1初始化参数 CLLocationManager *locationManager = [[CLLocationManager alloc] init];//创建位置管理器 locationManager.delegate=self;//设置代理 locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度级别 locationManager.distanceFilter=1000.0f;//设置距离筛选器 [locationManager startUpdatingLocation];//启动位置管理器 2更新回调函数 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSString *latitudeString=[[NSString alloc] initWithFormat:@"%g",newLocation.coordinate.latitude]; //latitudeLabel.text=latitudeString; [latitudeString release]; NSString *longitudeString=[[NSString alloc] initWithFormat:@"%g",newLocation.coordinate.longitude]; //longitudeLabel.text=longitudeString; [longitudeString release]; } 3错误回调 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSString *errorType = (error.code == kCLErrorDenied) ?@"Access Denied" : @"Unknown Error"; UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error getting Location" message:errorType delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil]; [alert show]; [alert release]; }