IOS百度地图之--->第二篇《大头针__简单使用及自定义》

呵呵!大家不要只看帖不回帖么,要不然我都没有积极性了。

第一步:创建一个用来呈现mapview的viewcontroller,不废话直接贴代码

       BasicMapViewController需要在该类中加入地图的代理并实现相关事件

//    34.2778000000,108.9530980000   数据来源http://www.gpsspg.com/maps.htm

#define xian            CLLocationCoordinate2DMake(34.2239390000,108.9807190000);

 

#import "BasicMapViewController.h"

#import “KHPaoPaoView.h”//气泡窗口

#import “KHPointAnnotation.h”//point

@interfaceBasicMapViewController ()

{

    BMKMapView *_mapView;

    NSBundle *bundle;

    NSArray *pointArr;

}

@end

 

@implementation BasicMapViewController

 

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    CGFloat navH = 64;

    if (__IPHONE_OS_VERSION < 7.0)

    {

        navH = 44;

    }

    _mapView  = [[BMKMapView alloc] initWithFrame:CGRectMake(0, navH+60, __SCREEN_SIZE.width, __SCREEN_SIZE.height-navH-60)];

//    _mapView.showMapScaleBar = NO;

//    _mapView.overlooking = 0;

    bundle = [NSBundle mainBundle];

    

    [self.view addSubview:_mapView];

    

    KHPointAnnotation *point = [[KHPointAnnotationalloc] init];

    point.title = @"西安市";

    point.subtitle = @"我在这";

    point.title1 = @"自定义泡泡测试";

    point.title2 = @"自定义测试";

    point.coordinate = xian;

    

    BMKPointAnnotation *point1 = [[BMKPointAnnotationalloc] init];

    point1.title = @"北京市";

    point1.subtitle = @"0000000000000";

    point1.coordinate = CLLocationCoordinate2DMake(39.905206, 116.390356);

    

    pointArr = @[point,point1];

    

}

-(void)viewWillAppear:(BOOL)animated

{

    [_mapViewviewWillAppear];

    _mapView.delegate = self;// 此处记得不用的时候需要置nil,否则影响内存的释放

}

-(void)viewWillDisappear:(BOOL)animated

{

    [_mapViewviewWillDisappear];

    _mapView.delegate = nil;// 不用时,置nil

}

- (IBAction)switchAction:(UISwitch *)sender

{

    if (sender.tag ==1)

    {

        if (sender.on)

        {

            [_mapViewsetMapType:BMKMapTypeStandard];

            

        }

        else

        {

            [_mapViewsetMapType:BMKMapTypeSatellite];

        }

    }

    if (sender.tag == 2)

    {

        //第一参数地图中心位置,第二参数一中心点显示周围的经纬度差值

        if (sender.on)

        {

            [_mapViewsetRegion:BMKCoordinateRegionMake(CLLocationCoordinate2DMake(34.2778000000,108.9530980000), BMKCoordinateSpanMake(0.5,0.5)) animated:YES];

        }

        else

        {

            [_mapViewsetRegion:BMKCoordinateRegionMake(CLLocationCoordinate2DMake(39.905206, 116.390356), BMKCoordinateSpanMake(0.5,0.5)) animated:YES];

        }

    }

    if (sender.tag == 3)

    {

        [_mapView setTrafficEnabled:sender.on];

    }

}

 

- (void)mapViewDidFinishLoading:(BMKMapView *)mapView

{

    KHLog(@"地图加载完成");//回调事件请自行参考使用

    [_mapViewsetCompassPosition:CGPointMake(100,100)];//指南针位置可能看不见,这个和官方的交流吧!不清楚是什么原因

    [_mapViewaddAnnotations:pointArr];

}

 

- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

{

    KHLog(@"地图区域改变完成");

}

 

#pragma mark 设置大头针

-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation

{

    if([annotation isKindOfClass:[BMKPointAnnotation class]])

    {

        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];

        newAnnotationView.pinColor = BMKPinAnnotationColorRed;

        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示

        return newAnnotationView;

    }

    else

    {

        //        这里使用自定义大头针

        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];

        newAnnotationView.image = [UIImageimageWithContentsOfFile:[bundlepathForResource:@"local@2x"ofType:@"png"]];

        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示

        KHPaoPaoView *paopao = [[KHPaoPaoView alloc] init];

        [paopao setAnnotationWith:((KHPointAnnotation *)annotation)];

        newAnnotationView.paopaoView = [[BMKActionPaopaoView alloc] initWithCustomView:paopao];

        return newAnnotationView;

    }

}

第二步:创建大头针相关的类、

       KHPointAnnotation这个类只要是实现了BMKAnnotation

     // 该类为一个抽象类,定义了基于BMKAnnotationBMKShape类的基本属性和行为,不能直接使用,必须子类化之后才能使用

      @interface BMKShape : NSObject <BMKAnnotation> {

      }  

 

#import "BMKPointAnnotation.h"

 

@interface KHPointAnnotation : BMKShape{

    @package

    CLLocationCoordinate2D _coordinate;

}

///该点的坐标

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

 

@property (strong) NSString *title1;

@property (strong) NSString *title2;

@end

 //.m中没什么东西就不贴了

 

KHPointAnnotation.h这个类继承自UIView  主要就是大头针被选中后展示内容

 

#import <UIKit/UIKit.h>

#import "KHPointAnnotation.h"

 

@interface KHPaoPaoView : UIView

{

    UILabel *label1;

    UILabel *label2;

    UILabel *label3;

    UILabel *label4;

}

-(instancetype)init;

/**

 *   设置标注

 *

 *   @param point 标注

 */

-(void)setAnnotationWith:(KHPointAnnotation *)point;

@end

 

#import "KHPaoPaoView.h"

 

 

 

@implementation KHPaoPaoView

 

 

 

-(instancetype)init

 

{

 

    self = [super init];

 

    if (self)

 

    {

 

//        这里需要注意百度地图中使用的是碎图片拼起来的,这里的细化工作呵呵你们自己完成吧!

 

        UIImage *image =[UIImageimageWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"medium@2x"ofType:@"png"]];

 

        

 

        self.frame = CGRectMake(0, 0, image.size.width+10, image.size.height+10);

 

        UIImageView *IMG = [[UIImageViewalloc] initWithFrame:self.frame];

 

        IMG.image = image;

 

        IMG.contentMode = UIViewContentModeScaleAspectFit;

 

        [self addSubview:IMG];

 

        

 

        label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10__SCREEN_SIZE.width-100, 15)];

 

        [label1setFont:[UIFontsystemFontOfSize:14.0f]];

 

        [self addSubview:label1];

 

        label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 25__SCREEN_SIZE.width-100, 15)];

 

        [label2setFont:[UIFontsystemFontOfSize:14.0f]];

 

        [self addSubview:label2];

 

        label3 = [[UILabel alloc] initWithFrame:CGRectMake(10, 40__SCREEN_SIZE.width-100, 15)];

 

        [label3setFont:[UIFontsystemFontOfSize:14.0f]];

 

        [self addSubview:label3];

 

        label4 = [[UILabel alloc] initWithFrame:CGRectMake(10, 55__SCREEN_SIZE.width-100, 15)];

 

        [label4setFont:[UIFontsystemFontOfSize:14.0f]];

 

        [self addSubview:label4];

 

    }

 

    returnself;

 

}

 

-(void)setAnnotationWith:(KHPointAnnotation *)point

 

{

 

    label1.text = point.title;

 

    label2.text = point.subtitle;

 

    label3.text = point.title1;

 

    label4.text = point.title2;

 

}

IOS Simulator Screen Shot 2015年2月5日 下午6 09 30

posted @ 2015-02-05 18:10  苦海中的渡轮  阅读(7411)  评论(2编辑  收藏  举报