您的位置:首页 > 移动开发 > IOS开发

获取当前位置信息-ios

2013-08-15 13:47 489 查看
locationManager= [[CLLocationManager alloc] init];//位置管理器
locationManager.desiredAccuracy = kCLLocationAccuracyBest;//精度,设可而止
locationManager.delegate = self;//设置委托
locationManager.distanceFilter = 100.0f;//距离删选器
[locationManager startUpdatingLocation]; //启动位置管理器

#pragma mark -
#pragma mark locationManager
-  (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//获取所在地城市名
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks,NSError *error)
{
for(CLPlacemark *placemark in placemarks)
{

NSString *str1=[[placemark.addressDictionary objectForKey:@"Name"] substringFromIndex:0];
NSLog(@"%@", [NSString stringWithFormat:@"我的位置:%@",str1]);
UITextView *locText = (UITextView*)[self.view viewWithTag:];
}
}];
//map region
[geocoder release];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{

if (error.code == kCLErrorDenied) {
[[[[UIAlertView alloc] initWithTitle:@"错误信息"
message:@"无法获取您的定位信息"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
[locationManager stopUpdatingLocation];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: