高德地图的使用
1.库的配置
/***************高德地图*************** 1.库的配置 --> 导入文件,添加头文件 libstdc++.6.0.9 QuartzCore CoreLocation SystemCOnfiguration libz OpenGLES CoreTelePhony Security
/*需要在Other Link Flags添加-Objc, 静态库中实现了一些类别,会与系统有出入,所以需要标记-Objc */
#import "ViewController.h" #import <MAMapKit/MAMapKit.h> #import <AMapSearchKit/AMapSearchAPI.h> @interface ViewController ()<MAMapViewDelegate,CLLocationManagerDelegate,UIGestureRecognizerDelegate,UISearchBarDelegate,AMapSearchDelegate> { CLLocationManager *_manager; MAMapView *_mapView; AMapSearchAPI *_search; } @end
2.初始化
#import "AppDelegate.h" #import <MAMapKit/MAMapKit.h> #import <AMapSearchKit/AMapSearchAPI.h> // @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [MAMapServices sharedServices].apiKey = @"db7d1a8f64d847ff49620ff13eed023c"; //初始化
// 注: INVALID User Scode --> 原因: 申请的key太新 ,10分钟内申请 key 与应用bundle Id 不一致 */
3.创建和显示地图
#pragma mark - 请求定位服务 -(void)requestAccessLocation{ if(![CLLocationManager locationServicesEnabled]){ NSLog(@"定位服务不可用"); return; } _manager = [[CLLocationManager alloc] init]; _manager.delegate = self; //此方法若没有 返回,直接授权 [_manager requestAlwaysAuthorization]; // } -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ NSLog(@"status = %d",status); } #pragma mark - 创建和显示地图 -(void)createAndShowMap{ _mapView = [[MAMapView alloc] initWithFrame:self.view.bounds]; _mapView.delegate = self; [self.view addSubview:_mapView]; //常用设置 _mapView.mapType = MAMapTypeStandard; _mapView.userTrackingMode = MAUserTrackingModeFollowWithHeading; //设置用户追踪模式 _mapView.zoomLevel = 16; //缩放级别 NSLog(@"%f %f",_mapView.maxZoomLevel,_mapView.minZoomLevel); // }
4.地图标记(长按添加大头针)
//4.地图标记(大头针)--> 长按 加 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dealLongPress:)]; longPress.delegate = self; //version 不同 需加上 [_mapView addGestureRecognizer:longPress]; //添加searchBar 实现搜索功能 _search = [[AMapSearchAPI alloc] initWithSearchKey:@"db7d1a8f64d847ff49620ff13eed023c" Delegate:self]; //搜索服务
#pragma mark - 地图标记 -(void)dealLongPress:(UILongPressGestureRecognizer *)longPress{ if(longPress.state == UIGestureRecognizerStateBegan){ //获取长按点 CGPoint point = [longPress locationInView:_mapView]; CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView]; //添加大头针 MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init]; annotation.coordinate = coordinate; annotation.title = @"点击位置"; annotation.subtitle = [NSString stringWithFormat:@"%.3f,%.3f",annotation.coordinate.longitude,annotation.coordinate.latitude]; NSLog(@"s = %@",annotation.subtitle); [_mapView addAnnotation:annotation]; } } //显示大头针的详细信息 -(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{ static NSString *annotationId = @"annotation"; MAPinAnnotationView *pin = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationId]; if(!pin){ pin = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationId]; } pin.canShowCallout = YES; // pin.pinColor = MAPinAnnotationColorGreen; return pin; }
注:iOS8.0需加此方法
#pragma mark - 长按显示大头针(iOS8.0以上需加) //UIGestureRecognizerDelegate --> 加长按显示大头针 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }
5.区域搜索 ---> 高德地图官网
//添加searchBar 实现搜索功能 _search = [[AMapSearchAPI alloc] initWithSearchKey:@"db7d1a8f64d847ff49620ff13eed023c" Delegate:self]; //搜索服务 /****高德地图官网 -- 开发者文档(POI)****/ //5.区域搜索 CGSize size = [UIScreen mainScreen].bounds.size; UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, size.height, 40)]; bar.delegate = self; // [self.view addSubview:bar];
#pragma mark - 实现周边的搜索 -(void)aroundSearchWithKey:(NSString *)key{ //........... AMapPlaceSearchRequest *poiRequest = [[AMapPlaceSearchRequest alloc] init]; poiRequest.searchType = AMapSearchType_PlaceAround; poiRequest.keywords = key; poiRequest.city = @[@"guangzhou"]; poiRequest.location = [AMapGeoPoint locationWithLatitude:_mapView.centerCoordinate.latitude longitude:_mapView.centerCoordinate.longitude]; // poiRequest.radius = 500; // poiRequest.requireExtension = YES; //发起POI搜索 [_search AMapPlaceSearch: poiRequest]; } //实现POI搜索对应的回调函数 - (void)onPlaceSearchDone:(AMapPlaceSearchRequest *)request response:(AMapPlaceSearchResponse *)response { if(response.pois.count == 0) { return; } NSLog(@"搜索个数: %ld",response.count); for(AMapPOI *p in response.pois){ NSLog(@"name = %@, long = %f, lat = %f",p.name,p.location.longitude,p.location.latitude); //添加大头针 MAPointAnnotation *point = [[MAPointAnnotation alloc] init]; point.coordinate = CLLocationCoordinate2DMake(p.location.latitude, p.location.longitude); point.title = p.name; point.subtitle = [NSString stringWithFormat:@"经度long = %f,纬度lat = %f",p.location.longitude,p.location.latitude]; [_mapView addAnnotation:point]; } //原有的代码 /* //通过AMapPlaceSearchResponse对象处理搜索结果 NSString *strCount = [NSString stringWithFormat:@"count: %d",response.count]; NSString *strSuggestion = [NSString stringWithFormat:@"Suggestion: %@", response.suggestion]; NSString *strPoi = @""; for (AMapPOI *p in response.pois) { strPoi = [NSString stringWithFormat:@"%@\nPOI: %@", strPoi, p.description]; } NSString *result = [NSString stringWithFormat:@"%@ \n %@ \n %@", strCount, strSuggestion, strPoi]; NSLog(@"Place: %@", result); */ }
6.路径规划
//UISearchBarDelegate -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ NSLog(@"search key = %@",searchBar.text); // // [self aroundSearchWithKey:searchBar.text]; //周边的搜索 #pragma mark 路径搜索 //构造AMapNavigationSearchRequest对象,配置查询参数 AMapNavigationSearchRequest *naviRequest= [[AMapNavigationSearchRequest alloc] init]; naviRequest.searchType = AMapSearchType_NaviDrive; naviRequest.requireExtension = YES; naviRequest.origin = [AMapGeoPoint locationWithLatitude:23.174659 longitude:113.341038]; naviRequest.destination = [AMapGeoPoint locationWithLatitude:23.174659 longitude:113.341038]; //发起路径搜索 [_search AMapNavigationSearch: naviRequest]; } //实现路径搜索的回调函数 - (void)onNavigationSearchDone:(AMapNavigationSearchRequest *)request response:(AMapNavigationSearchResponse *)response { if(response.route == nil) { return; } //通过AMapNavigationSearchResponse对象处理搜索结果 NSString *route = [NSString stringWithFormat:@"Navi: %@", response.route]; NSLog(@"%@", route); }
7.其他