iOS百度地图

来源:互联网 发布:韩国bj 知乎 编辑:程序博客网 时间:2024/06/07 02:11
[objc] view plain copy
print?
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2.   
  3.     {  
  4.   
  5.     //启动引擎 BaiduMapManager  
  6.   
  7.     _mapManager = [[BMKMapManager alloc]init];  
  8.   
  9.     // 如果要关注网络及授权验证事件,设定generalDelegate参数,否则为nil即可  
  10.   
  11.     BOOL ret = [_mapManager start:@” 申请的有效key” generalDelegate: (id<BMKGeneralDelegate>)self];  
  12.   
  13.     if (!ret) {  
  14.   
  15.     NSLog(@”manager start failed!”);  
  16.   
  17.     }  
  18.   
  19.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
  20.   
  21.     // Override point for customization after application launch.  
  22.   
  23.     self.viewController = [[[ViewController alloc] initWithNibName:@”ViewController” bundle:nil] autorelease];  
  24.   
  25.     self.window.rootViewController = self.viewController;  
  26.   
  27.     [self.window makeKeyAndVisible];  
  28.   
  29.     return YES;  
  30.   
  31.     }  
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions    {    //启动引擎 BaiduMapManager    _mapManager = [[BMKMapManager alloc]init];    // 如果要关注网络及授权验证事件,设定generalDelegate参数,否则为nil即可    BOOL ret = [_mapManager start:@" 申请的有效key" generalDelegate: (id<BMKGeneralDelegate>)self];    if (!ret) {    NSLog(@”manager start failed!”);    }    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    // Override point for customization after application launch.    self.viewController = [[[ViewController alloc] initWithNibName:@”ViewController” bundle:nil] autorelease];    self.window.rootViewController = self.viewController;    [self.window makeKeyAndVisible];    return YES;    }

用xib 或者手动创建一张地图

在 -(void)viewWillAppear:(BOOL)animated中 要设置代理(或者viewDidLoad的时候创建设置),切记,否则可能会出现方格 不出现地图。

[objc] view plain copy
print?
  1. _myMapView=[[BMKMapView alloc] initWithFrame:CGRectMake(00, Screen.width, Screen.height)];  
_myMapView=[[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, Screen.width, Screen.height)];

[objc] view plain copy
print?
  1. [super viewWillAppear:animated];  
  2. [_mapView viewWillAppear];  
  3. _mapView.showsUserLocation = NO;//先关闭显示的定位图层  
  4. _mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态  
  5. _mapView.showsUserLocation = YES;//显示定位图层  
  6.   _mapView.delegate = self;  
  7. _geocodesearch.delegate = self;  
  8. _userLocation.delegate = self;  
  9. _walkingRoutePlanOption.delegate = self;  
    [super viewWillAppear:animated];    [_mapView viewWillAppear];    _mapView.showsUserLocation = NO;//先关闭显示的定位图层    _mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态    _mapView.showsUserLocation = YES;//显示定位图层      _mapView.delegate = self;    _geocodesearch.delegate = self;    _userLocation.delegate = self;    _walkingRoutePlanOption.delegate = self;

步行路线代理、定位代理等。


在退出界面的时候 (注意!不能在viewWillDisappear方法中,至于为什么 听别人说的 百度地图与ios会冲突)设置代理为空

[objc] view plain copy
print?
  1. [_mapView viewWillDisappear];  
  2. _mapView.delegate = nil;  
  3. _geocodesearch.delegate = nil// 此处记得不用的时候需要置nil,否则影响内存的释放  
  4. _userLocation.delegate = nil;  
  5. [_userLocation stopUserLocationService];  
  6. _mapView.showsUserLocation = NO;  
  7. _walkingRoutePlanOption.delegate = nil;  
        [_mapView viewWillDisappear];        _mapView.delegate = nil;        _geocodesearch.delegate = nil; // 此处记得不用的时候需要置nil,否则影响内存的释放        _userLocation.delegate = nil;        [_userLocation stopUserLocationService];        _mapView.showsUserLocation = NO;        _walkingRoutePlanOption.delegate = nil;

当你用到
[objc] view plain copy
print?
  1. /** 
  2.  *打开定位服务 
  3.  */  
  4. -(void)startUserLocationService;  
/** *打开定位服务 */-(void)startUserLocationService;
的时候,就会回调代理方法

[objc] view plain copy
print?
  1. /** 
  2.  *用户方向更新后,会调用此函数 
  3.  *@param userLocation 新的用户位置 
  4.  */  
  5. - (void)didUpdateUserHeading:(BMKUserLocation *)userLocation;  
/** *用户方向更新后,会调用此函数 *@param userLocation 新的用户位置 */- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation;

还有一些失败。停止定位等代理方法 请下载百度SDK查看。

[objc] view plain copy
print?
  1. -(void)didUpdateUserLocation:(BMKUserLocation *)userLocation{  
  2.     NSLog(@”latitude–%f,longtitude—%f”,userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);  
  3.     CLLocationDegrees locaLatitude;  
  4.     CLLocationDegrees locaLongitude;  
  5.     locaLatitude=userLocation.location.coordinate.latitude;//纬度  
  6.     locaLongitude=userLocation.location.coordinate.longitude;//精  
  7.     BMKCoordinateRegion region;  
  8.     //将定位的点居中显示  
  9.     region.center.latitude=locaLatitude;  
  10.     region.center.longitude=locaLongitude;  
  11.     region.span.latitudeDelta  = 0.01;  
  12.     region.span.longitudeDelta = 0.01;  
  13.     [_mapView setRegion:region animated:NO];  
  14.       
  15.     subCoor.latitude = locaLatitude;  
  16.     subCoor.longitude = locaLongitude;  
  17.     [_mapView setCenterCoordinate:subCoor animated:YES];  
  18. }  
-(void)didUpdateUserLocation:(BMKUserLocation *)userLocation{    NSLog(@"latitude--%f,longtitude---%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);    CLLocationDegrees locaLatitude;    CLLocationDegrees locaLongitude;    locaLatitude=userLocation.location.coordinate.latitude;//纬度    locaLongitude=userLocation.location.coordinate.longitude;//精    BMKCoordinateRegion region;    //将定位的点居中显示    region.center.latitude=locaLatitude;    region.center.longitude=locaLongitude;    region.span.latitudeDelta  = 0.01;    region.span.longitudeDelta = 0.01;    [_mapView setRegion:region animated:NO];    subCoor.latitude = locaLatitude;    subCoor.longitude = locaLongitude;    [_mapView setCenterCoordinate:subCoor animated:YES];}

根据所得到的 经纬度 可以确定地理位置

[objc] view plain copy
print?
  1. [self geocode:[_latitude doubleValue] withLongitude:[_longitude doubleValue]];  
[self geocode:[_latitude doubleValue] withLongitude:[_longitude doubleValue]];

[objc] view plain copy
print?
  1. -(void)geocode:(double)latitude withLongitude:(double)longitude{  
  2.       
  3.     CLLocationCoordinate2D pt = (CLLocationCoordinate2D){00};  
  4.     pt = (CLLocationCoordinate2D){latitude, longitude};  
  5.     BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];  
  6.     reverseGeocodeSearchOption.reverseGeoPoint = pt;  
  7.     BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];  
  8.       
  9.     if(flag)  
  10.     {  
  11.         NSLog(@”反geo检索发送成功”);  
  12.     }  
  13.     else  
  14.     {  
  15.         NSLog(@”反geo检索发送失败”);  
  16.     }  
  17. }  
-(void)geocode:(double)latitude withLongitude:(double)longitude{    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};    pt = (CLLocationCoordinate2D){latitude, longitude};    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];    reverseGeocodeSearchOption.reverseGeoPoint = pt;    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];    if(flag)    {        NSLog(@"反geo检索发送成功");    }    else    {        NSLog(@"反geo检索发送失败");    }}

其中 
[objc] view plain copy
print?
  1. /** 
  2.  *根据地理坐标获取地址信息 
  3.  *异步函数,返回结果在BMKGeoCodeSearchDelegate的onGetAddrResult通知 
  4.  *@param reverseGeoCodeOption 反geo检索信息类 
  5.  *@return 成功返回YES,否则返回NO 
  6.  */  
  7. - (BOOL)reverseGeoCode:(BMKReverseGeoCodeOption*)reverseGeoCodeOption;  
/** *根据地理坐标获取地址信息 *异步函数,返回结果在BMKGeoCodeSearchDelegate的onGetAddrResult通知 *@param reverseGeoCodeOption 反geo检索信息类 *@return 成功返回YES,否则返回NO */- (BOOL)reverseGeoCode:(BMKReverseGeoCodeOption*)reverseGeoCodeOption;
然后会调用
[objc] view plain copy
print?
  1. /** 
  2.  *返回反地理编码搜索结果 
  3.  *@param searcher 搜索对象 
  4.  *@param result 搜索结果 
  5.  *@param error 错误号,@see BMKSearchErrorCode 
  6.  */  
  7. - (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error;  
/** *返回反地理编码搜索结果 *@param searcher 搜索对象 *@param result 搜索结果 *@param error 错误号,@see BMKSearchErrorCode */- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error;
则为
[objc] view plain copy
print?
  1. -(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error  
  2. {  
  3.     if (error == 0) {  
  4.         //  
  5.         self.addArr = [NSString stringWithFormat:@”%@”,result.address];  
  6.         _pointAnnotation.subtitle =[NSString stringWithFormat:@”当前位置:%@”,self.addArr];  
  7.         if (_pointAnnotation == nil) {  
  8.             [XCCommonUtility QQTShowAlertMsg:@”无法获取当前位置,请稍后再试”];  
  9.             return ;  
  10.         }  
  11.         [annotationArrays addObject:_pointAnnotation];  
  12.         [_mapView addAnnotations:annotationArrays];  
  13.         [self seachWalkRoute];  
  14.     }  
  15. }  
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{    if (error == 0) {        //        self.addArr = [NSString stringWithFormat:@"%@",result.address];        _pointAnnotation.subtitle =[NSString stringWithFormat:@"当前位置:%@",self.addArr];        if (_pointAnnotation == nil) {            [XCCommonUtility QQTShowAlertMsg:@"无法获取当前位置,请稍后再试"];            return ;        }        [annotationArrays addObject:_pointAnnotation];        [_mapView addAnnotations:annotationArrays];        [self seachWalkRoute];    }}

PS:

result.address即为反地理编码得到的位置。    [annotationArrays addObject:_pointAnnotation]; 是为标注数组。当有多个标注,先把标注添加到数组里 然后再添加至

 [_mapView addAnnotations:annotationArrays];文章末尾会说明。

[objc] view plain copy
print?
  1. /** 
  2.  *向地图窗口添加标注,需要实现BMKMapViewDelegate的-mapView:viewForAnnotation:函数来生成标注对应的View 
  3.  *@param annotation 要添加的标注 
  4.  */  
  5. - (void)addAnnotation:(id <BMKAnnotation>)annotation;  
  6.   
  7. /** 
  8.  *向地图窗口添加一组标注,需要实现BMKMapViewDelegate的-mapView:viewForAnnotation:函数来生成标注对应的View 
  9.  *@param annotations 要添加的标注数组 
  10.  */  
  11. - (void)addAnnotations:(NSArray *)annotations;  
/** *向地图窗口添加标注,需要实现BMKMapViewDelegate的-mapView:viewForAnnotation:函数来生成标注对应的View *@param annotation 要添加的标注 */- (void)addAnnotation:(id <BMKAnnotation>)annotation;/** *向地图窗口添加一组标注,需要实现BMKMapViewDelegate的-mapView:viewForAnnotation:函数来生成标注对应的View *@param annotations 要添加的标注数组 */- (void)addAnnotations:(NSArray *)annotations;


步行路线

[objc] view plain copy
print?
  1. /** 
  2.  *步行路线检索 
  3.  *异步函数,返回结果在BMKRouteSearchDelegate的onGetWalkingRouteResult通知 
  4.  *@param walkingRoutePlanOption 步行检索信息类 
  5.  *@return 成功返回YES,否则返回NO 
  6.  */  
  7. -(BOOL)walkingSearch:(BMKWalkingRoutePlanOption*)walkingRoutePlanOption;  
  8. /** 
  9.  *返回步行搜索结果 
  10.  *@param searcher 搜索对象 
  11.  *@param result 搜索结果,类型为BMKWalkingRouteResult 
  12.  *@param error 错误号,@see BMKSearchErrorCode 
  13.  */  
  14. -(void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error;  
/** *步行路线检索 *异步函数,返回结果在BMKRouteSearchDelegate的onGetWalkingRouteResult通知 *@param walkingRoutePlanOption 步行检索信息类 *@return 成功返回YES,否则返回NO */-(BOOL)walkingSearch:(BMKWalkingRoutePlanOption*)walkingRoutePlanOption;/** *返回步行搜索结果 *@param searcher 搜索对象 *@param result 搜索结果,类型为BMKWalkingRouteResult *@param error 错误号,@see BMKSearchErrorCode */-(void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error;

直接上代码

[objc] view plain copy
print?
  1. -(void)seachWalkRoute{  
  2.     BMKWalkingRoutePlanOption *walkingRoute = [[BMKWalkingRoutePlanOption alloc]init];  
  3.       
  4.     BMKPlanNode* currentNode = [[BMKPlanNode alloc] init];  
  5.     currentNode.pt = mianCoor;  
  6.     //    currentNode.name = @”aaa”; //注意,这里不能为空@”“  
  7.     BMKPlanNode* poiNode = [[BMKPlanNode alloc] init];  
  8.     poiNode.pt = subCoor;  
  9.     //    poiNode.name = @”bbb”;//注意,这里不能为空@”“  
  10.     walkingRoute.from = currentNode;  
  11.     walkingRoute.to = poiNode;  
  12.     BOOL flag = [_walkingRoutePlanOption walkingSearch:walkingRoute];  
  13.     if (flag == YES) {  
  14.         NSLog(@”搜索成功”);  
  15.     }else{  
  16.         [XCCommonUtility QQTShowAlertMsg:@”暂时无法获取步行路线”];  
  17.         NSLog(@”搜索失败”);  
  18.     }  
  19. }  
  20. - (void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error{  
  21.     NSArray* array = [NSArray arrayWithArray:_mapView.annotations];  
  22.     [_mapView removeAnnotations:array];  
  23.     array = [NSArray arrayWithArray:_mapView.overlays];  
  24.     [_mapView removeOverlays:array];  
  25.     if (error == BMK_SEARCH_NO_ERROR) {  
  26.         BMKWalkingRouteLine* plan = (BMKWalkingRouteLine*)[result.routes objectAtIndex:0];  
  27.         int size = [plan.steps count];  
  28.         int planPointCounts = 0;  
  29.         for (int i = 0; i < size; i++) {  
  30.             BMKWalkingStep* transitStep = [plan.steps objectAtIndex:i];  
  31.             if(i==0){  
  32.                 MapPointAnnotion* item = [[MapPointAnnotion alloc]init];  
  33.                 item.coordinate = plan.starting.location;  
  34.                 item.subtitle =_describe;;  
  35.                 item.type = 0;  
  36.                 item.tagNumber = 1;  
  37.                 [_mapView addAnnotation:item]; // 添加起点标注  
  38.                   
  39.             }else if(i==size-1){  
  40.                 MapPointAnnotion* item = [[MapPointAnnotion alloc]init];  
  41.                 item.coordinate = plan.terminal.location;  
  42.                 item.subtitle =[NSString stringWithFormat:@”当前位置:%@”,self.addArr];;  
  43.                 item.type = 1;  
  44.                 item.tagNumber = 0;  
  45.                 [_mapView addAnnotation:item]; // 添加起点标注  
  46.             }  
  47.             //添加annotation节点  
  48.             MapPointAnnotion* item = [[MapPointAnnotion alloc]init];  
  49.             item.coordinate = transitStep.entrace.location;  
  50.             item.title = transitStep.entraceInstruction;  
  51.             item.degree = transitStep.direction * 30;  
  52.             item.type = 4;  
  53.             //            [_mapView addAnnotation:item];  
  54.             //轨迹点总数累计  
  55.             planPointCounts += transitStep.pointsCount;  
  56.         }  
  57.           
  58.         //轨迹点  
  59.         BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];  
  60.         int i = 0;  
  61.         for (int j = 0; j < size; j++) {  
  62.             BMKWalkingStep* transitStep = [plan.steps objectAtIndex:j];  
  63.             int k=0;  
  64.             for(k=0;k<transitStep.pointsCount;k++) {  
  65.                 temppoints[i].x = transitStep.points[k].x;  
  66.                 temppoints[i].y = transitStep.points[k].y;  
  67.                 i++;  
  68.             }  
  69.               
  70.         }  
  71.         // 通过points构建BMKPolyline  
  72.         BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];  
  73.         [_mapView addOverlay:polyLine]; // 添加路线overlay  
  74.         delete []temppoints;  
  75.           
  76.     }  
  77. }  
-(void)seachWalkRoute{    BMKWalkingRoutePlanOption *walkingRoute = [[BMKWalkingRoutePlanOption alloc]init];    BMKPlanNode* currentNode = [[BMKPlanNode alloc] init];    currentNode.pt = mianCoor;    //    currentNode.name = @"aaa"; //注意,这里不能为空@""    BMKPlanNode* poiNode = [[BMKPlanNode alloc] init];    poiNode.pt = subCoor;    //    poiNode.name = @"bbb";//注意,这里不能为空@""    walkingRoute.from = currentNode;    walkingRoute.to = poiNode;    BOOL flag = [_walkingRoutePlanOption walkingSearch:walkingRoute];    if (flag == YES) {        NSLog(@"搜索成功");    }else{        [XCCommonUtility QQTShowAlertMsg:@"暂时无法获取步行路线"];        NSLog(@"搜索失败");    }}- (void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error{    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];    [_mapView removeAnnotations:array];    array = [NSArray arrayWithArray:_mapView.overlays];    [_mapView removeOverlays:array];    if (error == BMK_SEARCH_NO_ERROR) {        BMKWalkingRouteLine* plan = (BMKWalkingRouteLine*)[result.routes objectAtIndex:0];        int size = [plan.steps count];        int planPointCounts = 0;        for (int i = 0; i < size; i++) {            BMKWalkingStep* transitStep = [plan.steps objectAtIndex:i];            if(i==0){                MapPointAnnotion* item = [[MapPointAnnotion alloc]init];                item.coordinate = plan.starting.location;                item.subtitle =_describe;;                item.type = 0;                item.tagNumber = 1;                [_mapView addAnnotation:item]; // 添加起点标注            }else if(i==size-1){                MapPointAnnotion* item = [[MapPointAnnotion alloc]init];                item.coordinate = plan.terminal.location;                item.subtitle =[NSString stringWithFormat:@"当前位置:%@",self.addArr];;                item.type = 1;                item.tagNumber = 0;                [_mapView addAnnotation:item]; // 添加起点标注            }            //添加annotation节点            MapPointAnnotion* item = [[MapPointAnnotion alloc]init];            item.coordinate = transitStep.entrace.location;            item.title = transitStep.entraceInstruction;            item.degree = transitStep.direction * 30;            item.type = 4;            //            [_mapView addAnnotation:item];            //轨迹点总数累计            planPointCounts += transitStep.pointsCount;        }        //轨迹点        BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];        int i = 0;        for (int j = 0; j < size; j++) {            BMKWalkingStep* transitStep = [plan.steps objectAtIndex:j];            int k=0;            for(k=0;k<transitStep.pointsCount;k++) {                temppoints[i].x = transitStep.points[k].x;                temppoints[i].y = transitStep.points[k].y;                i++;            }        }        // 通过points构建BMKPolyline        BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];        [_mapView addOverlay:polyLine]; // 添加路线overlay        delete []temppoints;    }}

第二个方法基本上是直接照搬官方deom的 后面才发现原来这么方便。。刚开始全是自己研究,直到绘制步行路线这块 搞了很久 最后还没搞出来 就去照搬demo了  然后略微修改 分分钟搞定了。。



刚刚所提到的添加标注,可以在路线两端用到。

添加标注必须实现此代理

[objc] view plain copy
print?
  1. /** 
  2.  *根据anntation生成对应的View 
  3.  *@param mapView 地图View 
  4.  *@param annotation 指定的标注 
  5.  *@return 生成的标注View 
  6.  */  
  7. - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation;  
/** *根据anntation生成对应的View *@param mapView 地图View *@param annotation 指定的标注 *@return 生成的标注View */- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation;
[objc] view plain copy
print?
  1. -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation  
  2. {  
  3.     BMKAnnotationView *newAnnotation =[mapView viewForAnnotation:annotation];  
  4.       
  5.     if (newAnnotation==nil && [annotation isKindOfClass:[MapPointAnnotion class]])  
  6.     {  
  7.         MapPointAnnotion* pointAnnotation = (MapPointAnnotion*)annotation;  
  8.         NSString *AnnotationViewID = [NSString stringWithFormat:@”iAnnotation-%d”,pointAnnotation.tagNumber];  
  9.         newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];  
  10.         // 设置颜色  
  11.         newAnnotation.tag = pointAnnotation.tagNumber;  
  12.         ((BMKPinAnnotationView*)newAnnotation).pinColor = BMKPinAnnotationColorPurple;  
  13.           
  14.         // 从天上掉下效果  
  15.         ((BMKPinAnnotationView*)newAnnotation).animatesDrop = NO;  
  16.         // 设置可拖拽  
  17.         ((BMKPinAnnotationView*)newAnnotation).draggable = NO;  
  18.         //设置大头针图标  
  19.         if (pointAnnotation.tagNumber == 0) {  
  20.             ((BMKPinAnnotationView*)newAnnotation).image = [UIImage imageNamed:@”map_mark_mylocation.png”];  
  21.         }else{  
  22.             ((BMKPinAnnotationView*)newAnnotation).image = [UIImage imageNamed:@”map_mark.png”];  
  23.         }  
  24.         UIView *popView = [[UIView alloc]initWithFrame:CGRectMake(0022060)];  
  25.         popView.layer.cornerRadius = 3;//设置那个圆角的有多圆  
  26.         popView.layer.borderWidth = 1;//设置边框的宽度,当然可以不要  
  27.         popView.layer.borderColor = [[UIColor lightGrayColor] CGColor];//设置边框的颜色  
  28.         popView.layer.masksToBounds = YES;  
  29.         //    popView.backgroundColor = [QQTStringPlist colorPlistForKey:@”qqt_bc”];  
  30.         [popView setBackgroundColor:[[QQTStringPlist colorPlistForKey:@”qqt_bc”]colorWithAlphaComponent:0.5]];  
  31.           
  32.         UILabel *carName = [[UILabel alloc]initWithFrame:CGRectMake(0022060)];  
  33.         carName.text = [annotation subtitle];  
  34.         carName.font = [UIFont systemFontOfSize:14];  
  35.         carName.textColor = [UIColor blackColor];  
  36.         carName.lineBreakMode = kTextLineBreakByCharWrapping;  
  37.         carName.textAlignment = kTextAlignmentCenter;  
  38.         carName.numberOfLines = 0;  
  39.         [popView addSubview:carName];  
  40.         BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView];  
  41.         pView.frame = CGRectMake(0022063);  
  42.         ((BMKPinAnnotationView*)newAnnotation).paopaoView = nil;  
  43.         ((BMKPinAnnotationView*)newAnnotation).paopaoView = pView;  
  44.         [newAnnotation setSelected:YES];  
  45.     }  
  46.     return newAnnotation;  
  47. }  
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{    BMKAnnotationView *newAnnotation =[mapView viewForAnnotation:annotation];    if (newAnnotation==nil && [annotation isKindOfClass:[MapPointAnnotion class]])    {        MapPointAnnotion* pointAnnotation = (MapPointAnnotion*)annotation;        NSString *AnnotationViewID = [NSString stringWithFormat:@"iAnnotation-%d",pointAnnotation.tagNumber];        newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];        // 设置颜色        newAnnotation.tag = pointAnnotation.tagNumber;        ((BMKPinAnnotationView*)newAnnotation).pinColor = BMKPinAnnotationColorPurple;        // 从天上掉下效果        ((BMKPinAnnotationView*)newAnnotation).animatesDrop = NO;        // 设置可拖拽        ((BMKPinAnnotationView*)newAnnotation).draggable = NO;        //设置大头针图标        if (pointAnnotation.tagNumber == 0) {            ((BMKPinAnnotationView*)newAnnotation).image = [UIImage imageNamed:@"map_mark_mylocation.png"];        }else{            ((BMKPinAnnotationView*)newAnnotation).image = [UIImage imageNamed:@"map_mark.png"];        }        UIView *popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 220, 60)];        popView.layer.cornerRadius = 3;//设置那个圆角的有多圆        popView.layer.borderWidth = 1;//设置边框的宽度,当然可以不要        popView.layer.borderColor = [[UIColor lightGrayColor] CGColor];//设置边框的颜色        popView.layer.masksToBounds = YES;        //    popView.backgroundColor = [QQTStringPlist colorPlistForKey:@"qqt_bc"];        [popView setBackgroundColor:[[QQTStringPlist colorPlistForKey:@"qqt_bc"]colorWithAlphaComponent:0.5]];        UILabel *carName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, 60)];        carName.text = [annotation subtitle];        carName.font = [UIFont systemFontOfSize:14];        carName.textColor = [UIColor blackColor];        carName.lineBreakMode = kTextLineBreakByCharWrapping;        carName.textAlignment = kTextAlignmentCenter;        carName.numberOfLines = 0;        [popView addSubview:carName];        BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView];        pView.frame = CGRectMake(0, 0, 220, 63);        ((BMKPinAnnotationView*)newAnnotation).paopaoView = nil;        ((BMKPinAnnotationView*)newAnnotation).paopaoView = pView;        [newAnnotation setSelected:YES];    }    return newAnnotation;}



0 0