day8---地图

//********** 苹果自带的地图 ************
info 里面添加
NSLocationAlwaysUsageDescription YES
NSLocationWhenInUseUsageDescription YES
一、创建地图对象 【两个步骤:创建地图对象,添加到视图上】
self.map = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height+60)];

//********* 地图相关属性 ************
//1.显示指南针【 7.0 之后的方法】

// self.map.showsScale = YES;
//2.显示比例的方法 7.0 之后的方法】
// self.map.showsScale = YES;
//3.左右滑动
self.map.zoomEnabled = YES;
//4.上下滑动
self.map.scrollEnabled = YES;

self.map.delegate = self;

//将地图对象添加到视图上
[self.view addSubview:self.map];

pragma mark -MKMapViewDelegate 【地图对象遵守的协议】

//是为了更换大头针 变成自定义的大头针

  • (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation
    {

}

二、创建定位对象 【两个步骤:创建定位对象,添加到视图上】

//创建定位对象
self.cllManager = [[CLLocationManager alloc] init];

//******** 定位对象相关属性 **********
//1.精确到100m范围
self.cllManager.distanceFilter = 100;
//2.一直定位
self.cllManager.desiredAccuracy = kCLLocationAccuracyBest;
//3.判断版本
if ([[[UIDevice currentDevice] systemVersion] floatValue]>= 8.0) {
    
    NSLog(@"%f",[[[UIDevice currentDevice] systemVersion] floatValue]);
    
    [self.cllManager requestAlwaysAuthorization];
    [self.cllManager requestWhenInUseAuthorization];
    
}
self.cllManager.delegate = self;

//将定位对象添加到视图上
[self.cllManager startUpdatingLocation];

pragma mark -CLLocationManagerDelegate 【定位对象遵守的协议】

//代理方法中进行定位 【旧的方法】

  • (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
    fromLocation:(CLLocation *)oldLocation __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_6, __MAC_NA, __IPHONE_2_0, __IPHONE_6_0)
    {
    //MKCoordinateSpan 进行定位中,经纬度定位的一个结构体
    MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);

    //MKCoordinateRegion 这是一个结构体,里面有两个结构体
    MKCoordinateRegion region = MKCoordinateRegionMake(newLocation.coordinate, span);

    [self.map setRegion:region animated:YES];

    //系统的大头针对象 【两个步骤:创建大头针对象,将大头针对象添加到地图上】

MKPointAnnotation *pin = [[MKPointAnnotation alloc] init];      【创建大头针对象】

//******* 大头针相关属性 **********
//1.将新的经纬度赋值给大头针的经纬度,显示大头针;
pin.coordinate = newLocation.coordinate;
//2.标题
pin.title = @"标题";
//3.副标题
pin.subtitle = @"副标题";

//添加一个大头针对象
[self.map addAnnotation:pin];
//添加多个大头针对象

// [self.map addAnnotations:<#(NSArray *)#>];

//知道经纬度  自己定位
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(30.4538967846, 114.4265225293);

MKCoordinateSpan span1 = MKCoordinateSpanMake(0.01, 0.01);
MKCoordinateRegion region1 = MKCoordinateRegionMake(coordinate, span1);

[self.map setRegion:region1 animated:YES];


MKPointAnnotation *pin1 = [[MKPointAnnotation alloc] init];
pin1.coordinate = coordinate;
pin1.title = @"金融港B26";
pin1.subtitle = @"千峰";
[self.map addAnnotation:pin1];

}

//代理方法中进行定位 【新的方法】
//- (void)locationManager:(CLLocationManager *)manager
// didUpdateLocations:(NSArray *)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0)
//{
//
//
//
//}

地图库学习网址:
http://www.it165.net/pro/html/201403/10827.html

高德地图库网址:
http://lbs.amap.com/api/ios-sdk/guide/searchkit/

//********** 用高德官方的地图 ************
info 里面添加 下面10个文件
NSLocationAlwaysUsageDescription YES
NSLocationWhenInUseUsageDescription YES
libstdc++.6.0.9.dylib
libz.dylib
AMapSearchKit.framework
MAMapKit.framework
QuartzCore.framework
CoreLocation.framework
SystemConfiguration.framework
Security.framework

//遵守两个协议:
@interface ViewController ()

  • (void)viewDidLoad {
    [super viewDidLoad];

    [MAMapServices sharedServices].apiKey = @"ca37aa11a9d2cad2d780ee1c8354443f";
    self.mapView = [[MAMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.mapView.showsUserLocation = YES;
    self.mapView.delegate = self;
    //这句就是开启定位
    self.mapView.userTrackingMode = MAUserTrackingModeFollow; //
    [self.view addSubview:self.mapView];

[self searchAdd];

}

-(void)searchAdd
{
//配置用户Key
[AMapSearchServices sharedServices].apiKey = @"ca37aa11a9d2cad2d780ee1c8354443f";

//初始化检索对象
self.search = [[AMapSearchAPI alloc] init];
self.search.delegate = self;

//构造AMapGeocodeSearchRequest对象,address为必选项,city为可选项
AMapGeocodeSearchRequest *geo = [[AMapGeocodeSearchRequest alloc] init];
geo.address = @"武汉光谷";

//发起正向地理编码
[self.search AMapGeocodeSearch:geo];




//构造AMapReGeocodeSearchRequest对象
AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
regeo.location = [AMapGeoPoint locationWithLatitude:39.990459     longitude:116.481476];
regeo.radius = 10000;
regeo.requireExtension = YES;

//发起逆地理编码
[_search AMapReGoecodeSearch: regeo];

}

//实现正向地理编码的回调函数

  • (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response
    {
    if(response.geocodes.count == 0)
    {
    return;
    }

    //通过AMapGeocodeSearchResponse对象处理搜索结果
    NSString *strCount = [NSString stringWithFormat:@"count: %ld", response.count];
    NSString *strGeocodes = @"";
    for (AMapTip *p in response.geocodes) {
    strGeocodes = [NSString stringWithFormat:@"%@\ngeocode: %@", strGeocodes, p.description];
    }
    NSString *result = [NSString stringWithFormat:@"%@ \n %@", strCount, strGeocodes];
    NSLog(@"Geocode: %@", result);
    }

//实现逆地理编码的回调函数

  • (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
    {
    if(response.regeocode != nil)
    {
    //通过AMapReGeocodeSearchResponse对象处理搜索结果
    NSString *result = [NSString stringWithFormat:@"ReGeocode: %@", response.regeocode];
    NSLog(@"ReGeo: %@", response.regeocode.formattedAddress);
    }
    }

你可能感兴趣的:(day8---地图)