1,我们先申请一个appkey,申请appkey必须注册高德开发者
2,高德SDK的下载,现在SDK分别有三个库,根据你的app 里面的集成需求,可以选择性的下载添加到自己的工程里,他们分别是 2D地图库,3D地图库,还有搜索库;
3,添加SDK进自己的项目(工程)里,添加的时候注意路径问题,添加完高德SDK之后,我们还需要添加一些系统自带库,有了这些才能支持高德SDK的运行,他们分别如下图
4,运行环境的配置,在TARGETS->Build Settings->Other Linker Flaggs 中添加-ObjC。
5,实现地图
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:YES]; [MAMapServices sharedServices].apiKey = @"a2e716827857a145e86e99ea08cfe15f"; _mapView = [[MAMapView alloc] initWithFrame:[UIScreen mainScreen].bounds]; _mapView.mapType = MAMapTypeSatellite;// 设置地图样式,卫星 _mapView.showTraffic = YES; // 是否打开实时交通路况 _mapView.delegate = self; _mapView.logoCenter = CGPointMake(CGRectGetWidth(self.view.bounds)-55, 450);// 设置地图logo的中心点 _mapView.showsCompass= YES; // 设置成 NO 表示关闭指南针;YES 表示显示指南针 _mapView.showsScale = YES; // 设置成 NO 表示不显示比例尺;YES 表示显示比例尺 _mapView.scaleOrigin= CGPointMake(_mapView.scaleOrigin.x, 22); // 设置比例尺位置 _mapView.compassOrigin= CGPointMake(_mapView.compassOrigin.x, 22); // 设置指南针位置 _mapView.showsUserLocation = YES; // 开启地图定位 [_mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES]; // 设置用户跟宗模式 }
6,添加标注
MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];//初始化标注 pointAnnotation.coordinate = CLLocationCoordinate2DMake(39.989631, 116.481018); //设置标注地理坐标 pointAnnotation.title = @"方恒国际";//设置标注标题 pointAnnotation.subtitle = @"阜通东大街 6 号";//设置标注子标题 [_mapView addAnnotation:pointAnnotation];//添加标注
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation { if ([annotation isKindOfClass:[MAPointAnnotation class]]) { static NSString *pointReuseIndetifier = @"pointReuseIndetifier"; MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier]; if (annotationView == nil) { annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier]; } annotationView.canShowCallout= YES; //设置气泡可以弹出,默认为 NO annotationView.animatesDrop = YES; //设置标注动画显示,默认为 NO annotationView.draggable = YES; //设置标注可以拖动,默认为 NO annotationView.pinColor = MAPinAnnotationColorPurple; return annotationView; } return nil; } - (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation{ // NSLog("latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.l ongitude); NSLog(@"latitude : %f,longitude: %f", userLocation.coordinate.latitude,userLocation.coordinate.longitude); }
7,编码、反编码
- (void)viewDidLoad { [super viewDidLoad]; _search = [[AMapSearchAPI alloc] initWithSearchKey:@"a2e716827857a145e86e99ea08cfe15f" Delegate:self]; //初始化搜索 AMapGeocodeSearchRequest *georequest = [[AMapGeocodeSearchRequest alloc] init]; //构造一个request对象 georequest.searchType = AMapSearchType_Geocode; //设置为地理编码样式 georequest.address = @"北大"; //地址 georequest.city = @[@"北京"];//所在城市 [_search AMapGeocodeSearch:georequest]; //发起地理编码 // Do any additional setup after loading the view, typically from a nib. } #pragma mark 地理编码成功的回调 - (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response { if (response.count == 0) { return; } NSString *string = [NSString stringWithFormat:@"%ld", response.count]; for (AMapTip *tip in response.geocodes) {//response.geocodes所有跟地址匹配的地点 NSLog(@"%@", [NSString stringWithFormat:@"%@", tip.description]); } }
_search = [[AMapSearchAPI alloc] initWithSearchKey:@"a2e716827857a145e86e99ea08cfe15f" Delegate:self]; //初始化搜索 AMapReGeocodeSearchRequest *georequest = [[AMapReGeocodeSearchRequest alloc] init]; //构造一个request对象 georequest.searchType = AMapSearchType_ReGeocode; //设置为反地理编码样式 georequest.location = [AMapGeoPoint locationWithLatitude:39.000 longitude:116.00];//设置所在地里位置的经纬度 georequest.radius = 1000;//搜索半径 georequest.requireExtension = YES;// 是否返回扩展信息,默认为 NO [_search AMapGeocodeSearch:georequest]; //发起反地理编码 #pragma mark 反地理编码成功 - (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response { if (response.regeocode != nil) { //处理搜索结果 NSString *result = [NSString stringWithFormat:@"%@", response.regeocode]; } }
时间: 2024-10-01 21:19:09