获取当前手机地址,再从经纬度转换成详细地址(包括:国家名-国家代码,省,市,街道名,邮政代代码等)。
1:获取当前手机经纬度
@implementation CurrentLocation
@synthesize locationManager;
@synthesize target,callBack;
#pragma mark --
#pragma mark Public
-(void) startUpdatingLocation{
[[self locationManager] startUpdatingLocation];
}
#pragma mark --
#pragma mark Memory management
-(void) dealloc{
[super dealloc];
[locationManager release];
}
#pragma mark --
#pragma mark Location manager
/*
Return a location manager -- create one if necessary.
*/
- (CLLocationManager *)locationManager {
if (locationManager != nil) {return locationManager;}
locationManager = [[CLLocationManager alloc] init];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager setDelegate:self];
return locationManager;
}
#pragma mark --
#pragma mark CLLocationManagerDelegate methods
/*
Conditionally enable the Add button:
If the location manager is generating updates, then enable the button;
If the location manager is failing, then disable the button.
*/
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"获取到经纬度!");
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(@"获取失败!");
}
@end
2: 获取当前手机经纬度的详细地址
@implementation AddressReverseGeoder
#pragma mark --
#pragma mark Public
//根据经纬度开始获取详细地址信息
- (void)startedReverseGeoderWithLatitude:(double)latitude longitude:(double)longitude{
CLLocationCoordinate2D coordinate2D;
coordinate2D.longitude = longitude;
coordinate2D.latitude = latitude;
//
MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate2D];
geoCoder.delegate = self;
[geoCoder start];
}
#pragma mark --
#pragma mark MKReverseGeocoderDelegate methods
//获得地址信息
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
NSString *address = [NSString stringWithFormat:@"%@ %@ %@ %@ %@%@",
placemark.country,
placemark.administrativeArea,
placemark.locality,
placemark.subLocality,
placemark.thoroughfare,
placemark.subThoroughfare];
NSLog(@"经纬度所对应的详细:%@", address);
geocoder = nil;
}
//错误处理
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
NSLog(@"error %@" , error);
}
#pragma mark --
#pragma mark Memory management
- (void)dealloc {
[super dealloc];
}
@end
@property (nonatomic, readonly) NSDictionary *addressDictionary; //地址字典
@property (nonatomic, readonly) NSString *thoroughfare; //街道名——“科韵路”
@property (nonatomic, readonly) NSString *subThoroughfare; // 门牌号——“18号”
@property (nonatomic, readonly) NSString *locality; //城市——“广州市”
@property (nonatomic, readonly) NSString *subLocality; //区县——“天河区”
@property (nonatomic, readonly) NSString *administrativeArea; //身份——“广东省”
@property (nonatomic, readonly) NSString *subAdministrativeArea; //没获取到,不知道是什么东东
@property (nonatomic, readonly) NSString *postalCode; //邮政编码——不知道是什么原因,这里没获取到
@property (nonatomic, readonly) NSString *country; //国家——“中国”
@property (nonatomic, readonly) NSString *countryCode; //国家代码——“CN”