iOS - 百度地图
2016-04-13 23:40 jiangys 阅读(552) 评论(0) 编辑 收藏 举报这段时间,在做百度地图的开发过程中,会遇到一些小问题。做点记录。
开发前期,需要两个链接:
申请密钥:http://lbsyun.baidu.com/index.php?title=iossdk/guide/key
配置开发环境:http://lbsyun.baidu.com/index.php?title=iossdk/guide/buildproject
申请到密钥的时候,要注意BundleID和Xcode的一致。
使用效果:
为了减少页面的资源,在主要页面里,只放一张静态图,通过调用百度的静态API生成。用户需要查看详情的时候,再跳到地图的导航。
生成静态图API地址:http://lbsyun.baidu.com/index.php?title=static
NSString *strCerter = [NSString stringWithFormat:@"%@,%@",_houseModel.map.lng,_houseModel.map.lat]; NSString *mapUrl = [NSString stringWithFormat:@"http://api.map.baidu.com/staticimage?center=%@&width=%f&height=%@&zoom=11&markers=%@&markerStyles=l,A",strCerter,RFSCREEN_W,@"200",strCerter]; UIImageView *mapImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, RFSCREEN_W, 200)]; [mapImageView sd_setImageWithURL:[NSURL URLWithString:mapUrl]]; [cell addSubview:mapImageView];
转跳到MapController就很简单了,代码如:
// // RFHouseMapViewController.m // RFHouse // // Created by jiangys on 16/3/29. // Copyright © 2016年 Jiangys. All rights reserved. // #import "RFHouseMapViewController.h" #import <BaiduMapAPI_Utils/BMKUtilsComponent.h> #import <BaiduMapAPI_Map/BMKMapComponent.h> #import <BaiduMapAPI_Search/BMKSearchComponent.h> @interface RFHouseMapViewController ()<BMKMapViewDelegate, BMKGeoCodeSearchDelegate,BMKGeneralDelegate> /** 百度地图 */ @property (nonatomic, strong) BMKMapView *mapView; @property (nonatomic, strong) BMKGeoCodeSearch *geocodesearch; @property (nonatomic, strong) BMKMapManager *mapManager; @end @implementation RFHouseMapViewController #pragma mark -- 生命周期 - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = Color_White; // 要使用百度地图,请先启动BaiduMapManager _mapManager = [[BMKMapManager alloc]init]; BOOL ret = [_mapManager start:@"rUvg0vdlCsdI227Z6I1LoV8xGhZ0ZZwb" generalDelegate:self]; if (!ret) { NSLog(@"manager start failed!"); } // 初始化地图 _geocodesearch = [[BMKGeoCodeSearch alloc]init]; _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, RFSCREEN_W, RFSCREEN_H)]; [_mapView setZoomLevel:14]; [self.view addSubview:_mapView]; [self reverseGeocodeWithLng:_lng lat:_lat]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)viewWillAppear:(BOOL)animated { [_mapView viewWillAppear]; _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放 _geocodesearch.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放 } -(void)viewWillDisappear:(BOOL)animated { [_mapView viewWillDisappear]; _mapView.delegate = nil; // 不用时,置nil _geocodesearch.delegate = nil; // 不用时,置nil } - (void)dealloc { if (_geocodesearch != nil) { _geocodesearch = nil; } if (_mapView) { _mapView = nil; } } #pragma mark 百度地图API // 百度地图:反向geo检索 -(void)reverseGeocodeWithLng:(CGFloat)lng lat:(CGFloat)lat { CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0}; pt = (CLLocationCoordinate2D){lat, lng}; BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init]; reverseGeocodeSearchOption.reverseGeoPoint = pt; BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption]; if(flag) { RFLog(@"反geo检索发送成功"); } else { RFLog(@"反geo检索发送失败"); } } // 百度地图:根据anntation生成对应的View - (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation { NSString *AnnotationViewID = @"annotationViewID"; //根据指定标识查找一个可被复用的标注View,一般在delegate中使用,用此函数来代替新申请一个View BMKAnnotationView *annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; if (annotationView == nil) { annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]; ((BMKPinAnnotationView*)annotationView).pinColor = BMKPinAnnotationColorRed; ((BMKPinAnnotationView*)annotationView).animatesDrop = YES; } annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5)); annotationView.annotation = annotation; annotationView.canShowCallout = TRUE; return annotationView; } // 百度地图:反向地址编码 -(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error { NSArray* array = [NSArray arrayWithArray:_mapView.annotations]; [_mapView removeAnnotations:array]; array = [NSArray arrayWithArray:_mapView.overlays]; [_mapView removeOverlays:array]; if (error == 0) { BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init]; item.coordinate = result.location; item.title = result.address; [_mapView addAnnotation:item]; _mapView.centerCoordinate = result.location; } } - (void)onGetNetworkState:(int)iError { if (0 == iError) { NSLog(@"联网成功"); } else{ NSLog(@"onGetNetworkState %d",iError); } } - (void)onGetPermissionState:(int)iError { if (0 == iError) { NSLog(@"授权成功"); } else { NSLog(@"onGetPermissionState %d",iError); } } @end
要注意Start这里填的是百度的"百度地图iOS SDK开发密钥"
BOOL ret = [_mapManager start:@"rUvg0vdlCsdI227Z6I1LoV8xGhZ0ZZwb" generalDelegate:self];
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端