本文共 5235 字,大约阅读时间需要 17 分钟。
在进行Objective-C++开发时,除了编写高质量的代码外,还需要注意一些细节问题,这些问题可能会导致编译和运行时的各种错误。以下是需要注意的两大细节:
为了确保代码能够顺利编译并运行,最简单的方法是将工程文件中的某一个文件扩展名从.m更改为.mm。这样可以自动切换至Objective-C++的混编模式进行编译。
由于静态库中包含类别条目(第四点),建议在工程的Build Settings -> Other Linker Flags中添加-all_load标志。这可以确保所有类别和相关文件被正确编译和连接。
setPaopaoView 警告针对setPaopaoView相关的警告,可以通过在Other Linker Flags中添加-w标志来临时解决问题。
为了提高效率和减少代码冗余,建议合并相关的静态库。
在开发地图应用时,可以允许地图定位功能获取设备的地理位置信息,并通过委托进行处理。以下是实现代码示例:
_mapView.showsUserLocation = YES;
地图视图更新用户位置时会触发以下委托:
- (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation { // 定位成功,获取当前位置信息 _currentLocation = userLocation.location; // 使用NSLog输出经纬度信息 NSLog(@"!latitude!!! %f", userLocation.location.coordinate.latitude); NSLog(@"!longtitude!!! %f", userLocation.location.coordinate.longitude); // 根据当前位置锁定地图显示范围 [baiduMapView setMapRegionWithCoordinate:_currentLocation.coordinate];} 为了更好地定位用户位置,可以设置地图的显示区域和中心点。以下是实现代码:
- (void)setMapRegionWithCoordinate:(CLLocationCoordinate2D)coordinate { if (!_isSetMapSpan) { BMKCoordinateRegion region = BMKCoordinateRegionMake(coordinate, BMKCoordinateSpanMake(0.05, 0.05)); _isSetMapSpan = YES; [baiduMapView setRegion:region animated:YES]; [baiduMapView setCenterCoordinate:coordinate animated:YES]; _currentSelectCoordinate = coordinate; }} 当地图显示区域发生变化时,可以通过以下委托获取最新的标注信息:
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { [baiduMapView.annotations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { BMKPointAnnotation *item = (BMKPointAnnotation *)obj; if (item.coordinate.latitude == _currentSelectCoordinate.latitude && item.coordinate.longitude == _currentSelectCoordinate.longitude) { [baiduMapView selectAnnotation:obj animated:YES]; *stop = YES; } }];} 为了在地图中添加标注,可以使用以下代码:
BMKPointAnnotation *item = [[BMKPointAnnotation alloc] init];item.coordinate = coordinate;item.title = titleString;item.subtitle = subTitleString;[baiduMapView addAnnotation:item];
为了实现自定义标注视图,可以使用以下代码:
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id)annotation { static NSString *AnnotationViewID = @"annotationViewID"; BMKAnnotationView *annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; if (annotationView == nil) { annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]; ((BMKPinAnnotationView *)annotationView).animatesDrop = YES; annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_location.png"]]; UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom]; [selectButton setFrame:CGRectMake(260, 0, 50, annotationView.Help_height)]; [selectButton setTitle:@"确定" forState:UIControlStateNormal]; [selectButton setBackgroundColor:[UIColor redColor]]; [selectButton setShowsTouchWhenHighlighted:YES]; [selectButton addTarget:self action:@selector(Location_selectPointAnnotation:) forControlEvents:UIControlEventTouchUpInside]; annotationView.rightCalloutAccessoryView = selectButton; [selectButton setTag:_cacheAnnotationTag]; [_cacheAnnotationMDic setObject:annotation forKey:[NSNumber numberWithInteger:_cacheAnnotationTag]]; _cacheAnnotationTag++; if ([annotation.title isEqualToString: String_myLocation]) { ((BMKPinAnnotationView *)annotationView).pinColor = BMKPinAnnotationColorGreen; [annotationView setDraggable:YES]; [annotationView setSelected:YES animated:YES]; } else { ((BMKPinAnnotationView *)annotationView).pinColor = BMKPinAnnotationColorRed; } annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5)); annotationView.annotation = annotation; annotationView.canShowCallout = TRUE; return annotationView; }}
为了实现POI检索,可以使用以下代码:
baiduMapSearch = [[BMKSearch alloc] init];baiduMapSearch.delegate = self;[BMKSearch setPageCapacity:10];
以下是搜索的具体实现:
BOOL flag = [baiduMapSearch poiSearchInCity:_currentCity withKey:_searchKeywordString pageIndex:_searchPageIndex];
搜索成功后,可以通过以下委托获取POI信息:
- (void)onGetPoiResult:(NSArray *)poiResultList searchType:(int)type errorCode:(int)error { if (error == BMKErrorOk) { BMKPoiResult *result = [poiResultList objectAtIndex:0]; for (int i = 0; i < result.poiInfoList.count; i++) { BMKPoiInfo *poi = [result.poiInfoList objectAtIndex:i]; // 获取POI详细信息 } }} 为了删除地图上的标注,可以使用以下代码:
NSMutableArray *annotationMArray = [[NSArray arrayWithArray:baiduMapView.annotations] mutableCopy];[baiduMapView removeAnnotations:annotationMArray];
通过以上优化,开发者可以更高效地实现地图定位和POI检索功能,同时确保代码的编译和运行环境的正确配置。如果需要更详细的内容,可以参考原文链接:https://my.oschina.net/jack088/blog/533124
转载地址:http://pfhfk.baihongyu.com/