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 <MKAnnotation>)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 ()<MAMapViewDelegate,AMapSearchDelegate>

  • (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);
    }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,841评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,415评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,904评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,051评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,055评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,255评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,729评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,377评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,517评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,420评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,467评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,144评论 3 317
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,735评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,812评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,029评论 1 256
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,528评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,126评论 2 341

推荐阅读更多精彩内容