位置与地图(一)定位获取位置及位置编码-反编码

         *我们的应用程序,可以通过添加Core Location框架所包含的类,获取设备的地图位置.

         *添加CoreLocation.framework框架,导入#import<CoreLocation/CoreLocation.h>

         *使用地图服务时,会消耗更多地设备电量.因此,在获取到设备的位置后,应该停止定位来节省电量

@跟往常一样,我们通过一个demo来展示内容与效果

//
//  HMTRootViewController.h
//  My-GPS-Map
//
//  Created by hmt on 14-4-12.
//  Copyright (c) 2014年 胡明涛. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HMTRootViewController : UIViewController <CLLocationManagerDelegate>

@end

//
//  HMTRootViewController.m
//  My-GPS-Map
//
//  Created by hmt on 14-4-12.
//  Copyright (c) 2014年 胡明涛. All rights reserved.
//

#import "HMTRootViewController.h"
#import <AddressBook/AddressBook.h>

@interface HMTRootViewController (){

    CLLocationManager * _locationManage;
}

@property (nonatomic,retain) CLLocationManager * locationManage;

@end

@implementation HMTRootViewController

- (void)dealloc{

    RELEASE_SAFELY(_locationManage);
    [super dealloc];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.

    [self createGPSMap];
    self.view.backgroundColor = [UIColor redColor];
    
}

- (void)createGPSMap{

    // 初始化位置服务
    self.locationManage = [[CLLocationManager alloc]init];
    
    // 要求CLLocationManager对象返回全部信息
    _locationManage.distanceFilter = kCLDistanceFilterNone;
    
    // 设置定位精度
    _locationManage.desiredAccuracy = kCLLocationAccuracyBest;
    
    // 设置代理
    _locationManage.delegate = self;
    
    // 开始定位
    [_locationManage startUpdatingLocation];
    
    [_locationManage release];

}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    
    CLLocation * newLocation = [locations lastObject];
    // 停止实时定位
    [_locationManage stopUpdatingLocation];
    
    //  取得经纬度
    CLLocationCoordinate2D coord2D = newLocation.coordinate;
    double latitude = coord2D.latitude;
    double longitude = coord2D.longitude;
    NSLog(@"纬度 = %f  经度 = %f",latitude,longitude);
    
    //  取得精度
    CLLocationAccuracy horizontal = newLocation.horizontalAccuracy;
    CLLocationAccuracy vertical   = newLocation.verticalAccuracy;
    NSLog(@"水平方 = %f 垂直方 = %f",horizontal,vertical);
    
    //  取得高度
    CLLocationDistance altitude = newLocation.altitude;
    NSLog(@"%f",altitude);
    
    //  取得此时时刻
    NSDate *timestamp = [newLocation timestamp];
    //  实例化一个NSDateFormatter对象
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    //  设定时间格式
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss a"];
    [dateFormat setAMSymbol:@"AM"];         //  显示中文, 改成"上午"
    [dateFormat setPMSymbol:@"PM"];
    //  求出当天的时间字符串,当更改时间格式时,时间字符串也能随之改变
    NSString *dateString = [dateFormat stringFromDate:timestamp];
    NSLog(@"此时此刻时间 = %@",dateString);
    
    
    //  -----------------------------------------位置反编码--------------------------------------------
    CLGeocoder * geocoder = [[CLGeocoder alloc]init];
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        
        for (CLPlacemark * place in placemarks) {
            
            NSLog(@"name = %@",place.name);                                    //  位置名
            NSLog(@"thoroughfare = %@",place.thoroughfare);                    //  街道
            NSLog(@"subAdministrativeArea = %@",place.subAdministrativeArea);  //  子街道
            NSLog(@"locality = %@",place.locality);                            //  市
            NSLog(@"subLocality = %@",place.subLocality);                      //  区
            NSLog(@"country = %@",place.country);                              //  国家
            
            NSArray *allKeys = place.addressDictionary.allKeys;
            for (NSString *key in allKeys)
            {
                NSLog(@"key = %@, value = %@",key, place.addressDictionary[key]);
            }
#pragma mark - 使用系统定义的字符串直接查询,记得导入AddressBook框架
            NSLog(@"kABPersonAddressCityKey = %@", (NSString *)kABPersonAddressCityKey);
            NSLog(@"city = %@", place.addressDictionary[(NSString *)kABPersonAddressCityKey]);
            NSString *city = place.locality;
            if(city == nil)
            {
                city = place.addressDictionary[(NSString *)kABPersonAddressStateKey];
            }
        }
    }];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
@程序运行结果:(以39.3,116.4为例)

位置与地图(一)定位获取位置及位置编码-反编码_第1张图片

    //  判断输入的地址
    if (self.locationTextField.text == nil  ||  [self.locationTextField.text length] == 0) {
        return;
    }
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    /*  -----------------------------------------位置编码--------------------------------------------  */
    [geocoder geocodeAddressString:_locationTextField.text completionHandler:^(NSArray *placemarks, NSError *error) {
        
        for (CLPlacemark *placemark in placemarks) {
            
            CLLocationCoordinate2D coordinate = placemark.location.coordinate;
            NSString *strCoordinate = [NSString stringWithFormat:@"纬度 = %3.5f\n 经度 = %3.5f",coordinate.latitude,coordinate.longitude];
            NSLog(@"%@",strCoordinate);
            NSDictionary *addressDictionary = placemark.addressDictionary;
            NSString *address = [addressDictionary objectForKey:(NSString *)kABPersonAddressStreetKey];
            NSString *state = [addressDictionary objectForKey:(NSString *)kABPersonAddressStateKey];
            NSString *city = [addressDictionary objectForKey:(NSString *)kABPersonAddressCityKey];
            NSLog(@"街道 = %@\n 省 = %@\n 城市 = %@",address,state,city);
        }
    }];

你可能感兴趣的:(自动定位)