新建工程后,几项准备:
1、工程中一个文件设为.mm后缀
2、在Xcode的Project -> Edit Active Target -> Build -> Linking -> Other Linker Flags中添加-ObjC
3、 设置静态库的链接路径,在Xcode的Project -> Edit Active Target -> Build -> Search Path -> Library Search Paths中添加您的静态库目录,比如"$(SRCROOT)/../libs/Release$(EFFECTIVE_PLATFORM_NAME)",$(SRCROOT)宏代表您的工程文件目录,$(EFFECTIVE_PLATFORM_NAME)宏代表当前配置是OS还是simulator
4、framework: CoreLocation.framework和QuartzCore.framework、OpenGLES.framework、SystemConfiguration.framework、CoreGraphics.framework、Security.framework
5、自iOS SDK v2.5.0起,为了对iOS8的定位能力做兼容,做了相应的修改,开发者在使用过程中注意事项如下: 需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):
NSLocationWhenInUseUsageDescription ,允许在前台使用时获取GPS的描述
NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述
首先要在appdelegate里面添加
BMKMapManager
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 要使用百度地图,请先启动BaiduMapManager _mapManager = [[BMKMapManager alloc]init]; // 如果要关注网络及授权验证事件,请设定 generalDelegate参数 BOOL ret = [_mapManager start:@"在此处输入您的授权Key" generalDelegate:nil]; if (!ret) { NSLog(@"manager start failed!"); } // Add the navigation controller's view to the window and display. [self.window addSubview:navigationController.view]; [self.window makeKeyAndVisible]; return YES; }
定位:
@interface ViewController ()<BMKMapViewDelegate,BMKCloudSearchDelegate,BMKLocationServiceDelegate> { BMKMapView* mapView; BMKCloudSearch* search; BMKLocationService* location; }
- (void)viewDidLoad { [super viewDidLoad]; CGRect rect = CGRectMake(0, 0, 320, 400); mapView = [[BMKMapView alloc] initWithFrame:rect]; [self.view addSubview:mapView]; mapView.showsUserLocation= YES; //搜索 search = [[BMKCloudSearch alloc] init]; search.delegate = self; //服务 location = [[BMKLocationService alloc] init]; location.delegate = self; [location startUserLocationService]; mapView.showsUserLocation = NO; mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading; mapView.showsUserLocation = YES; //搜索按钮 UIButton* btSearch = [UIButton buttonWithType:UIButtonTypeCustom]; [btSearch setTitle:@"搜索" forState:UIControlStateNormal]; [btSearch addTarget:self action:@selector(clickSearch:) forControlEvents:UIControlEventTouchUpInside]; [btSearch setFrame:CGRectMake(0, 400, 320, 80)]; [btSearch setTintColor:[UIColor whiteColor]]; [btSearch setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.view addSubview:btSearch]; } -(void)viewWillAppear:(BOOL)animated{ [mapView viewWillAppear]; mapView.delegate = self; } -(void)viewWillDisappear:(BOOL)animated{ [mapView viewWillDisappear]; mapView.delegate = nil; }
mapview代理实现:
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation { [mapView updateLocationData:userLocation]; } //处理位置坐标更新 - (void)didUpdateUserLocation:(BMKUserLocation *)userLocation { [mapView updateLocationData:userLocation]; }
本地云检索:
BMKCloudLocalSearchInfo *cloudLocalSearch = [[BMKCloudLocalSearchInfo alloc] init]; cloudLocalSearch.ak = @"pjzXs0beL65WCNiRvtLrOe5X"; cloudLocalSearch.geoTableId = 87197; cloudLocalSearch.pageIndex = 0; cloudLocalSearch.pageSize = 8; // cloudLocalSearch.region = @"成都市"; cloudLocalSearch.keyword = @"ex"; BOOL flag = [search localSearchWithSearchInfo:cloudLocalSearch]; if (flag) { NSLog(@"本地云检索成功"); } else{ NSLog(@"本地云检索失败"); }
检索回调:
- (void)onGetCloudPoiResult:(NSArray*)poiResultList searchType:(int)type errorCode:(int)error{ // 清楚屏幕中所有的annotation NSArray* array = [NSArray arrayWithArray:mapView.annotations]; [mapView removeAnnotations:array]; if (error == BMKErrorOk) { BMKCloudPOIList* result = [poiResultList objectAtIndex:0]; for (int i = 0; i < result.POIs.count; i++) { BMKCloudPOIInfo* poi = [result.POIs objectAtIndex:i]; BMKPointAnnotation* item = [[BMKPointAnnotation alloc] init]; CLLocationCoordinate2D pt = (CLLocationCoordinate2D){ poi.longitude,poi.latitude}; item.coordinate = pt; item.title = poi.title; [mapView addAnnotation:item]; } } else { NSLog(@"error ==%d",error); } }
但到目前为止,使用xcode6.2beta,真机使用iphone5,始终在本地云检索的时候报错:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString stringByAppendingString:]: nil argument'
。求高人指教啊
终于找到原因所在,原来我的服务端的ak设置的sn校验,虽然我不知道什么是sn校验,但是最后改成了ip白名单。然后在“我的服务”中打开了服务,终于,奇迹出现了。终于回调成功了。。。呵呵呵。。。
ios技术交流群:378501081。。期待你加入。。