地理

//
//  ViewController.m
//  Core Location_定位功能
//
//  Created by DC020 on 15/12/23.
//  Copyright (c) 2015年 Bill. All rights reserved.
//



//只要需要定位功能。。。在plist中添加。。。
//NSLocationWhenInUseUsageDescription->YES(当使用时)
//NSLocationAlwaysUsageDescription->YES(一直)
#import "ViewController.h"
//2.引入头文件
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>//地图库
#import "TCAnnotation.h"

@interface ViewController ()<CLLocationManagerDelegate,
                             MKMapViewDelegate>{
    //3.创建定位管理
    CLLocationManager *_locationManager;
    //定义地图视图
    MKMapView *_mapView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _mapView = [[MKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _mapView.delegate = self;
    [self.view addSubview:_mapView];
    //设置用户位置追踪
    _mapView.userTrackingMode = MKUserTrackingModeFollow;
//    _mapView.mapType = MKMapTypeHybrid;
    
    //初始化管理器
    _locationManager = [[CLLocationManager alloc]init];
    //判断定位服务是否开启
//    NSLog(@"%d",[CLLocationManager locationServicesEnabled]);
    if (![CLLocationManager locationServicesEnabled]){
        NSLog(@"定位服务当前可能尚未打开,请设置打开");
        return;
    }
    //如果没有授权,则请求用户授权
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)//表示授权状态没有决定,也就是没有授权
    {//如果授权没有,请求授权
        [_locationManager requestWhenInUseAuthorization];
    }
    //如果授权状态为whenInUse
    else if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse){
        //设置管理器
        //设置代理
        _locationManager.delegate = self;
        //设置定位精度
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        //定位频率
        _locationManager.distanceFilter = 100;
        //启动定位
        [_locationManager startUpdatingLocation];
    }
    [self addAnnotation];
}
#pragma mark 添加大头针
-(void)addAnnotation{
    CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(30.7266819435, 120.7208981251);
    TCAnnotation *annonation1 = [[TCAnnotation alloc]init];
    annonation1.title = @"智慧产业创新园";
    annonation1.subtitle = @"东臣信息科技";
    annonation1.coordinate = location1;
    [_mapView addAnnotation :annonation1];
}
#pragma mark CoreLocation代理
#pragma mark 跟踪定位代理方法,每次位置发生变化就会执行
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    CLLocation *location = [locations firstObject];//取出位置信息
    CLLocationCoordinate2D coordinate = location.coordinate;//获取位置坐标
    
    NSLog(@"经度为:%f",coordinate.longitude);
    NSLog(@"纬度为:%f",coordinate.latitude);
    NSLog(@"海拔:%f,航向:%f,行走速度:%f",location.altitude,location.course,location.speed);
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    //将地图中心位置移动到某点
    //_mapView
//        _mapView setCenterCoordinate:<#(CLLocationCoordinate2D)#>
    //地图显示区域
    MKCoordinateRegion theRegion;
    theRegion.center = userLocation.coordinate;
    theRegion.span.latitudeDelta = 0.02;
    theRegion.span.longitudeDelta = 0.02;
    [_mapView setRegion:theRegion];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end




//
//  TCAnnotation.h
//  Core Location_定位功能
//
//  Created by DC020 on 15/12/23.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import <Foundation/Foundation.h>
//1.引入头文件,遵守协议MKAnnotation
#import <MapKit/MapKit.h>
@interface TCAnnotation : NSObject<MKAnnotation>
@property(nonatomic,assign)CLLocationCoordinate2D coordinate;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *subTitle;
@end



//
//  ViewController.m
//  地理编码以及反地理编码
//
//  Created by DC020 on 15/12/23.
//  Copyright (c) 2015年 Bill. All rights reserved.
//


//搜索的所有结果都是在中国境内的,因为苹果在中国的地图服务商是高德地图
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController (){
    CLGeocoder *_geoCoder;//创建地理编码对象
    
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _geoCoder =[[CLGeocoder alloc]init];
    //地理编码...传入的信息是地名
    [self getCoordinateByAddress:@"浙江"];
    //反地理编码...传入的信息是坐标(经纬度)
//    [self getAddressByLatitude:30 longitude:120];
}

#pragma mark 根据地名确定地理坐标
-(void)getCoordinateByAddress:(NSString *)address{
    [_geoCoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
        //取得地标,地标中存储了详细的地址信息。注意:一个地名可以搜索出多个地址
        CLPlacemark *placemark = [placemarks firstObject];
        //剖析地标
        //位置
        CLLocation *location = placemark.location;
        CLLocationCoordinate2D coordinate = location.coordinate;
        NSLog(@"经度是%f",coordinate.latitude);
        //区域
        CLRegion *region = placemark.region;
        NSLog(@"%@",region);
        //详细地址
        NSDictionary*addressDic = placemark.addressDictionary;
        NSLog(@"%@",addressDic);
        
    }];
    
}

#pragma mark 根据坐标取得地名
-(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{
    //通过经纬度创建位置信息
    CLLocation *location = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    
    [_geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        //获取地标
        CLPlacemark *placemark =[placemarks firstObject];
        //打印地标中的地址信息
        NSLog(@"%@",placemark.addressDictionary[@"FormattedAddressLines"]);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end






你可能感兴趣的:(地理)