IOS开发百度地图API

  第一部分--牛刀小试
百度地图零基础到各种效果界面。上面的几行不用看,那是为了SEO,就是为了让我这篇博客让更多的人搜索到,上面的问题我都已经解决,我QQ915893620,有问题欢迎和我交流。我也会持续更新兄弟们的问题。下面是步骤:
1.申请百度地图key,要想使用百度地图,你需要申请key,这个key是安桌苹果通用,但是你的项目名必须跟你的key吻合,至于什么是项目名,不多说,到 址http://api.map.baidu.com/lbsapi/cloud/ios-mobile-apply-key.htm申请,

2.下载百度开发包,http://api.map.baidu.com/lbsapi/cloud/sdkiosdev-download.htm,
3.建立一个项目,项目名为cscapp,然后加入3个框架,MapKit.framework,CoreLocation.framework,QuartzCore.framework,将下载的开发包inc,libbaidumapapi.a,mapapi.bundle这三个文件加入到程序中
4.将ViewController的.m改成.mm
5.在委托中.h文件中中加入#import”BMapKit.h”,并声明变量BMKMapManager*_mapManager;在.m委托中加入
 mapManager = [[BMKMapManager alloc]init];
    //如果要关注 络及授权验证事件,请设定 generalDelegate 参数
    BOOL ret =[_mapManager start:@”3102732B30E0D66EF51415C9E6CE055EC78FF07E”generalDelegate:nil];
    if (!ret){
       NSLog(@”manager start failed!”);
    }
6.注意:在这里就开始运行程序的话,我在我的iphone上运行这个百度地图,会出现-[UIDeviceuniqueGlobalDeviceIdentifier]: unrecognized selector sent toinstance 0x1ed19370
这么一个bug,解决办法很多,但是我感觉最爽的一种办法就是加入4个文件NSString+MD5Addition,UIDevice+IdentifierAddition,直接加入到项目里面就可以,无需引入头文件,下载地址
http://www.kuaipan.cn/file/id_30491149655344975.htm
7.在viewController.mm中的viewDidLoad改为如下代码
– (void)viewDidLoad
{
       [superviewDidLoad];
       BMKMapView*mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320,548)];
       self.view =mapView;
       // Do anyadditional setup after loading the view, typically from anib.
}
保存后运行,一个最简单的百度地图API应用就完成了
源代码下载 http://www.kuaipan.cn/file/id_30491149655344976.htm

百度地图的功能很强大,上面只是牛刀小试

第二部分
       在开始之前,要对坐标有个了解,手机自带GPS定位功能,获得的GPS坐标无法直接在国内地图中直接使用,因为经过了偏移转换,目的是为了国家安全,假如小日本知道全中国任意点的精确坐标,可以把国内的任意地图资源整合的话,那么它的导弹想打到哪就是哪。当然这只是一方面。        还有新手注意,别在软件中使用多个地图,也不要同一界面中同一地图的多个窗口。地图授权是根据项目名,那么在项目中需要多处使用的话,要使用单例模式。如果在多个界面加载了地图界面,各个界面由于是使用单例模式会相互干扰,所以可以根据需要进行重定位。
功能五,搜索,用的很广泛,这个功能很强大,你可以搜索某一坐标点附近的加油站,停车场,宾馆,酒店,餐厅,甚至厕所都能搜索到,比如说你想查找5000米内的加油站,以及1000米内的停车场。
flag = [_search poiSearchNearBy:@”加油站” center:coor1 radius:5000 pageIndex:0];
flag = [_search poiSearchNearBy:@”停车场” center:coor1 radius:1000 pageIndex:0];
然后用委托-(void)onGetPoiResult:(NSArray*)poiResultList searchType:(int)typeerrorCode:(int)error
输出结果
将百度地图视角切换到某一坐标点
-(void)Region{
      CLLocationCoordinate2D coor;
      coor.latitude = self._latitude;
      coor.longitude = self._longitude;
       NSDictionary*tip =   BMKBaiduCoorForWgs84(coor);
      CLLocationCoordinate2D coor1=  BMKCoorDictionaryDecode(tip);
      BMKCoordinateRegion viewRegion = BMKCoordinateRegionMake(coor1,BMKCoordinateSpanMake(0.05,0.05));
      BMKCoordinateRegion adjustedRegion = [_mapViewregionThatFits:viewRegion];
       [_mapViewsetRegion:adjustedRegion animated:YES];
}
清除地图上所有痕迹和路线
-(void)clereOldYJDH{
       NSArray*array = [NSArray arrayWithArray:_mapView.annotations];
       [_mapViewremoveAnnotations:array];
       array =[NSArray arrayWithArray:_mapView.overlays];
       [_mapViewremoveOverlays:array];
       [selfRegion];
}
// 当选中一个annotationviews时,调用此接口
– (void)mapView:(BMKMapView *)mapViewdidSelectAnnotationView:(BMKAnnotationView *)view
{
      NSLog(@”选中一个annotationviews:%f,%f”,view.annotation.coordinate.latitude,view.annotation.coordinate.longitude);
}
// 当取消选中一个annotationviews时,调用此接口
– (void)mapView:(BMKMapView *)mapViewdidDeselectAnnotationVie w:(BMKAnnotationView *)view{
      NSLog(@”取消选中一个annotation views”);
}
// 当mapView新添加annotationviews时,调用此接口
– (void)mapView:(BMKMapView *)mapViewdidAddAnnotationViews:(NSArray *)views
{
      NSLog(@”mapView新添加annotation views”);
}
// 当点击annotationview弹出的泡泡时,调用此接口
– (void)mapView:(BMKMapView *)mapViewannotationViewForBubble:(BMKAnnotationView *)view
{
      NSLog(@”点击annotation view弹出的泡泡”);
}
// 拖动annotationview时view的状态变化
– (void)mapView:(BMKMapView *)mapViewannotationView:(BMKAnnotationView *)viewdidChangeDragState:(BMKAnnotationViewDragSta te)newStatefromOldState:(BMKAnnotationViewDragSta te)oldState
{
      NSLog(@”动annotation view时view的状态变化”);
}
// 标注呈绿色样式大头钉
((BMKPinAnnotationView *)annotationView).pinColor =BMKPinAnnotationColorGre en;
// 允许用户拖动
[annotationView setDraggable:YES];
// 气泡框左侧显示的View,可自定义
annotationView.leftCalloutAccessoryView  = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@”icon_location.png”]];
// 气泡框右侧显示的View 可自定义
annotationView.rightCalloutAccessoryVie w =selectButton;
// 让标注在进入界面时就处于弹出气泡框的状态
[annotationView setSelected:YES animated:YES];
// 整个标注的偏移量
annotationView.centerOffset = CGPointMake(0,-(annotationView.frame.size.height * 0.5));
annotationView.annotation = annotation;//绑定对应的标点经纬度
annotationView.canShowCallout = TRUE;//允许点击弹出气泡框

在地图上定制标注替代大头钉,可以将文字图片所有能加到view中的,都可以以大头钉的形式显示出来,需要将view转换为image主要代码,最重要的是知道这个原理,然后实现起来就很简单:
– (BMKAnnotationView *)mapView:(BMKMapView *)viewviewForAnnotation:(id)annotation
在这个委托中实现如下代码
            UIView *viewForImage=[[UIView alloc]initWithFrame:CGRectMake(0, 0,132, 64)];
              UIImageView *imageview=[[UIImageViewalloc]initWithFrame:CGRectMake(0, 0, 32, 64)];
              [imageview setImage:[UIImage imageNamed:@”车位置.png”]];
              [viewForImage addSubview:imageview];
             
              UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(32, 0, 100,64)];
              label.text=@”陈双超”;
              label.backgroundColor=[UIColor clearColor];
              [viewForImage addSubview:label];
              annotationView.image=[self getImageFromView:viewForImage];

-(UIImage *)getImageFromView:(UIView *)view{
      UIGraphicsBeginImageCont ext(view.bounds.size);
       [view.layerrenderInContext:UIGraphicsGetCurrentCont ext()];
       UIImage*image = UIGraphicsGetImageFromCu rrentImageContext();
      UIGraphicsEndImageContex t();
       returnimage;
}

在 上发现别人专门建立view来定制气泡和专门建立文件来定制大头钉,觉得那方法对代码管理起来更简单,可读性强。值得学习 http://www.kuaipan.cn/file/id_30491149655345085.htm
@interface KYBubbleView : UIScrollView{     //UIView是气泡view的本质
       NSDictionary*_infoDict;
      UILabel                *titleLabel;//标题label
      UILabel                *detailLabel;//副标题label
      UILabel                *contactLabel;      //联系人
      UILabel                *homeAddresslabel;     //家庭地址
      UIButton              *rightButton;
      NSUInteger          index;
}
#import “BMKPointAnnotation.h”

@interface KYPointAnnotation : BMKPointAnnotation {
       NSUInteger_tag;
}

@property NSUInteger tag;

@end
部分效果图

         if ([[UIApplication sharedApplicationcanOpenURL:url]) {

         }else{

提示message:@”您未安装百度地图,是否立即前往APPStore安装!delegate:selfcancelButtonTitle:nilotherButtonTitles:@”立即安装,@”稍后安装,nil];

             ;

IOS开发百度地图API-用点生成路线,导航,气泡响应

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2014年7月7日
下一篇 2014年7月7日

相关推荐