// ios 设备提供了3种不同的途径进行定位
/*
1、Wi-Fi 通过Wi-Fi路由器的地理位置信息查询,比较省电,iPhone、iPod touch、和iPad都可以采用这种方式定位
2、蜂窝式移动电话基站 通过移动运用商基站定位,只有iPhone、3G版本的iPod touch和iPad可以采用这种方式定位
3、GPS卫星 通过GPS卫星定位,这种方式最为准确,但是耗电量大,不能遮挡,iPhone、iPod touch和iPad都可以采用这种方式定位
*/
// ios定位
/*
ios 6定位主要使用Core Location框架,定位时主要使用Core LocationManager、Core LocationManagerDelegate和CLLocation这3个类,此定位可以单纯定位,也就是可以不使用地图
至此,地理信息反编码介绍已经完毕,程序运行效果图如下:
至此,地理信息编码查询介绍已经完毕,程序运行效果图如下:
/*
1、Wi-Fi 通过Wi-Fi路由器的地理位置信息查询,比较省电,iPhone、iPod touch、和iPad都可以采用这种方式定位
2、蜂窝式移动电话基站 通过移动运用商基站定位,只有iPhone、3G版本的iPod touch和iPad可以采用这种方式定位
3、GPS卫星 通过GPS卫星定位,这种方式最为准确,但是耗电量大,不能遮挡,iPhone、iPod touch和iPad都可以采用这种方式定位
*/
// ios定位
/*
ios 6定位主要使用Core Location框架,定位时主要使用Core LocationManager、Core LocationManagerDelegate和CLLocation这3个类,此定位可以单纯定位,也就是可以不使用地图
*/
位置定位,代码实现如下:
- (void)initUI
{
for (int i = 0; i < 3; i ++)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, i*50+50, 70, 30)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:label];
[label release];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, i*50+50, 200, 30)];
textField.backgroundColor = [UIColor whiteColor];
textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];
[textField release];
if (i == 0)
{
label.text = @"经度:";
textLot = textField;
}
else if (i == 1)
{
label.text = @"纬度:";
textLat = textField;
}
else if (i == 2)
{
label.text = @"高度:";
textAlt = textField;
}
}
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(60, 200, 200, 30)];
btn.backgroundColor = [UIColor whiteColor];
btn.layer.cornerRadius = 5.0f;
btn.layer.masksToBounds = YES;
btn.showsTouchWhenHighlighted = YES;
[btn setTitle:@"地理信息反编码" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(reverseGeoCode:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
labGeo = [[UILabel alloc] initWithFrame:CGRectMake(60, 250, 200, 30)];
labGeo.backgroundColor = [UIColor clearColor];
labGeo.font = [UIFont systemFontOfSize:15.0f];
labGeo.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:labGeo];
[labGeo release];
}
#pragma mark - 初始化CCLocationManager
- (void)initLocationManager
{
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 100.0f;
// desiredAccuracy 精准度
/*
kCLLocationAccuracyBest; // 使用电池供电时最高的精度
kCLLocationAccuracyNearestTenMeters; // 精确到10米
kCLLocationAccuracyHundredMeters; // 精确到100米
kCLLocationAccuracyKilometer; // 精确到1000米
kCLLocationAccuracyThreeKilometers; // 精确到3000米
*/
// distanceFilter 距离过滤器
/*
定义了设备移动后获得位置信息的最小距离,单位是米
*/
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[_locationManager startUpdatingLocation];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:YES];
[_locationManager stopUpdatingLocation];
}
#pragma mark - CLLocationManager Delegate
/*
当用户设备移动到达过滤距离时,系统就会回调下面两个方法
*/
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
_currLocation = [locations lastObject];
textLot.text = [NSString stringWithFormat:@"%3.5f",_currLocation.coordinate.longitude];
textLat.text = [NSString stringWithFormat:@"%3.5f",_currLocation.coordinate.latitude];
textAlt.text = [NSString stringWithFormat:@"%3.5f",_currLocation.altitude];
// locations
/*
locations是位置变化的集合,它按照时间的顺序存放,当前设备的位置在最后的一个元素。
*/
// CLLocation
/*
CLLocation有两个属性。分别是altitude和coordinate,前者是海拔高度,后者是经纬度坐标
coordinate是一个CLLocationCoordinate2D结构体
*/
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"%@",[error description]);
}
// 地理信息反编码
/*
地理信息反编码,就是把经纬度这些信息转成地理文字信息,这些人们才能看得懂地理信息
*/
地理信息反编码,代码实现如下:
#pragma mark - 地理信息反编码
- (void)reverseGeoCode:(id)sender
{
CLGeocoder *geoCode = [[CLGeocoder alloc] init];
[geoCode reverseGeocodeLocation:_currLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0)
{
CLPlacemark *placeMark = placemarks[0];
NSDictionary *dict = placeMark.addressDictionary;
NSString *strStreet = [dict objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *strState = [dict objectForKey:(NSString *)kABPersonAddressStateKey];
NSString *strCity = [dict objectForKey:(NSString *)kABPersonAddressCityKey];
labGeo.text = [NSString stringWithFormat:@"%@%@%@",strStreet,strState,strCity];
}
}];
[geoCode release];
// CLGeocoder 地理信息反编码使用CLGeocoder类使用,该类能够实现地理坐标和地理文字信息的转换
/*
使用reverseGeocodeLocation: completionHandler:方法来进行地理信息反编码。
_currLocation 地理位置对象。
completionHandler 代码块,用于地理信息反编码之后的回调。
placemarks 地理信息反编码返回来的地理坐标数组,一个地理坐标并不是完成意义上的几个点,而是指一个范围,在这个范围中,有多种不同的描述信息,这些信息放在地标的集合中。
error 地理信息反编码返回来的错误信息。
*/
// 地理文字信息,这些枚举存在AddressBook.framework中,所以必须导入AddressBook.framework框架
/*
kABPersonAddressStreetKey // 街道信息
kABPersonAddressStateKey // 州、省信息
kABPersonAddressCityKey // 市信息
*/
}
至此,地理信息反编码介绍已经完毕,程序运行效果图如下:
// 地理信息编码查询
/*
地理信息编码查询和反编码刚好相反,它通过地理信息的文字描述查询出相关的地理坐标,这种查询结果也是一个集合
*/
地理信息编码查询,代码实现如下:
- (void)initSubUI
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 200, 30)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentLeft;
label.text = @"输入查询地点关键字:";
[self.view addSubview:label];
[label release];
textQuery = [[UITextField alloc] initWithFrame:CGRectMake(20, 90, 280, 30)];
textQuery.backgroundColor = [UIColor whiteColor];
textQuery.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
textQuery.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textQuery.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textQuery];
[textQuery release];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(60, 150, 200, 30)];
btn.backgroundColor = [UIColor whiteColor];
btn.layer.cornerRadius = 5.0f;
btn.layer.masksToBounds = YES;
btn.showsTouchWhenHighlighted = YES;
[btn setTitle:@"地理信息编码查询" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(geoCodeQuery:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
textCode = [[UITextView alloc] initWithFrame:CGRectMake(20, 200, 280, 100)];
textCode.backgroundColor = [UIColor colorWithWhite:0.6f alpha:1.0f];
textCode.font = [UIFont systemFontOfSize:15.0f];
textCode.textAlignment = NSTextAlignmentLeft;
textCode.editable = NO;
[self.view addSubview:textCode];
[textCode release];
}
#pragma mark - 地理信息编码查询
- (void)geoCodeQuery:(id)sendr
{
if (textQuery.text == nil || [textQuery.text length] <= 0)
{
return;
}
CLGeocoder *geoCode = [[CLGeocoder alloc] init];
[geoCode geocodeAddressString:textQuery.text completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"查询记录数:%d",[placemarks count]);
if ([placemarks count] > 0)
{
CLPlacemark *placeMark = [placemarks objectAtIndex:0];
CLLocationCoordinate2D coordinate = placeMark.location.coordinate;
NSString *strCoordinate = [NSString stringWithFormat:@"经度:%3.5f\n纬度:%3.5f",coordinate.latitude, coordinate.longitude];
NSDictionary *dict = placeMark.addressDictionary;
NSString *strStreet = [dict objectForKey:(NSString *)kABPersonAddressStreetKey];
strStreet = strStreet == nil ? @"" : strStreet;
NSString *strState = [dict objectForKey:(NSString *)kABPersonAddressStateKey];
strState = strState == nil ? @"" : strState;
NSString *strCity = [dict objectForKey:(NSString *)kABPersonAddressCityKey];
strCity = strCity == nil ? @"" : strCity;
textCode.text = [NSString stringWithFormat:@"%@\n%@\n%@\n%@",strCoordinate,strState,strStreet,strCity];
[textQuery resignFirstResponder];
}
}];
[geoCode release];
// 地理信息编码查询采用CLGeocoder类
/*
1、geocodeAddressDictionary: completionHandler: 通过指定一个地址信息字典对象来查询
2、geocodeAddressString: completionHandler: 通过一个地址信息字符串对象来查询
3、geocodeAddressString: inRegion: completionHandler: 通过通过地址信息字符串和查询范围来查询
*/
}
至此,地理信息编码查询介绍已经完毕,程序运行效果图如下: