ios开发之--高德地图以及自定义大头针和气泡、导航

以前开发一直用的百度地图,这次开发想用用高德地图,不复杂,在这里仅做记录,效果如下:

效果不怎么好,产品,,,,你懂的!

代码如下:

准备工作,就不多说了!

1,如下,从Demo里面把这两个类拉到自己的项目里面:

CustomCalloutView.h

CustomCalloutView.m

CustomAnnotationView.h

CustomAnnotationView.m

2,在CustomAnnotationView这个类里面自定义自己所需要展示的样式:

CustomAnnotationView.m里面的代码:

#define kArrorHeight        10

#define kPortraitMargin     5
#define kPortraitWidth      70
#define kPortraitHeight     50

#define kTitleWidth         120
#define kTitleHeight        20

#define kCalloutWidth       200.0
#define kCalloutHeight      70.0

#define kWidth  150.f
#define kHeight 60.f

#import "CustomAnnotationView.h"
#import "ChangeViewController.h"
#import "LoginViewController.h"

@interface CustomAnnotationView()

@property (nonatomic, strong) UILabel *titleLabel;//评分
@property (nonatomic, strong) UILabel *subtitleLabel;//地址
@property (nonatomic, strong) UILabel *equipmentInfoLabel;//设备信息
@property (nonatomic, strong) UILabel *wattingLabel;//等待人数
@property (nonatomic,copy)NSString *deviceStr;//桩号
@property (nonatomic,copy)NSString *fauUserStr;//负责人
@property (nonatomic,copy)NSString *funMobileStr;//负责人电话

@property (nonatomic,copy)NSMutableArray *deviceInfoAry;

@property (nonatomic,copy)NSString *deviceQueueID;//队列号

@end

@implementation CustomAnnotationView

-(NSMutableArray *)deviceInfoAry
{
    if (!_deviceInfoAry) {
        _deviceInfoAry = [NSMutableArray array];
    }
    return _deviceInfoAry;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
     NSLog(@"%@", selected ? @"YES" : @"NO");
    
    if (self.selected == selected)
    {
        return;
    }
    
    if (selected)
    {
        if (self.calloutView == nil)
        {
            self.calloutView = [[CustomCalloutView alloc]initWithFrame:CGRectMake(0, 0, kCalloutWidth, 110+70)];
//            self.backgroundColor = BackgroundColor;
            self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,-CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
            
            [self initSubViews];
            
            CLLocationCoordinate2D coorinate = [self.annotation coordinate];
            
            for (int i = 0; i < self.dataAry.count; i ++) {
                DevicesModel *model = self.dataAry[i];
                if ([model.latitude floatValue] == coorinate.latitude && [model.longitude floatValue] == coorinate.longitude) {
                    self.titleLabel.text = [NSString stringWithFormat:@"评分:%@",model.score];
                    self.subtitleLabel.text = [NSString stringWithFormat:@"地址:%@",model.address];
                    self.equipmentInfoLabel.text = [NSString stringWithFormat:@"设备信息:%@",model.statshow];
                    self.wattingLabel.text = [NSString stringWithFormat:@"等待人数:%@",model.queue];
                    self.deviceStr = [NSString stringWithFormat:@"%@",model.device_no];
                    self.fauUserStr = [NSString stringWithFormat:@"%@",model.device_leader];
                    self.funMobileStr = [NSString stringWithFormat:@"%@",model.leader_mobile];
                    
                    self.deviceQueueID = [NSString stringWithFormat:@"%@",model.ida];
                    
                    NSString *wattingStr = [NSString stringWithFormat:@"%@",model.queue];
                    NSString *addressStr = [NSString stringWithFormat:@"%@",model.address];
                    
                    NSString *latStr = [NSString stringWithFormat:@"%@",model.latitude];
                    NSString *lonStr = [NSString stringWithFormat:@"%@",model.longitude];
                    
                    self.deviceInfoAry = [NSMutableArray arrayWithObjects:self.deviceStr,wattingStr,self.fauUserStr,self.funMobileStr,addressStr,latStr,lonStr,self.deviceQueueID, nil];
                    NSLog(@"infoary is %@",self.deviceInfoAry);
                    
                }
            }
        }
        
        [self addSubview:self.calloutView];
    }
    else
    {
        [self.calloutView removeFromSuperview];
    }
    
    [super setSelected:selected animated:animated];
}

- (void)initSubViews
{
    // 添加标题,即评分
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin, kPortraitMargin, kTitleWidth, kTitleHeight)];
    self.titleLabel.textColor = [UIColor darkGrayColor];
    [self.calloutView addSubview:self.titleLabel];
    
    // 添加副标题,即商户地址
    self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin, kPortraitMargin * 2 + kTitleHeight, kCalloutWidth, kTitleHeight)];
    self.subtitleLabel.font = [UIFont systemFontOfSize:12];
    self.subtitleLabel.textColor = [UIColor darkGrayColor];
    self.subtitleLabel.numberOfLines = 0;
    [self.calloutView addSubview:self.subtitleLabel];
    
    // 设备信息
    self.equipmentInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin, CGRectGetMaxY(self.subtitleLabel.frame)+kPortraitMargin, kCalloutWidth, kTitleHeight)];
    self.equipmentInfoLabel.font = [UIFont systemFontOfSize:12];
    self.equipmentInfoLabel.textColor = [UIColor darkGrayColor];
    [self.calloutView addSubview:self.equipmentInfoLabel];
    
    // 等待人数
    self.wattingLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin, CGRectGetMaxY(self.equipmentInfoLabel.frame)+kPortraitMargin, kCalloutWidth, kTitleHeight)];
    self.wattingLabel.font = [UIFont systemFontOfSize:12];
    self.wattingLabel.textColor = [UIColor darkGrayColor];
    [self.calloutView addSubview:self.wattingLabel];
    
    _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelBtn.frame = CGRectMake(kPortraitMargin, CGRectGetMaxY(self.wattingLabel.frame)+5, 50, 50);
    [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelBtn setTitleColor:[UIColor darkTextColor ] forState:UIControlStateNormal];
    _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [_cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.calloutView addSubview:_cancelBtn];
    
    _changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _changeBtn.frame = CGRectMake(kCalloutWidth-kPortraitMargin-50, CGRectGetMaxY(self.wattingLabel.frame)+5, 50, 50);
    _changeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [_changeBtn setTitle:@"选择它" forState:UIControlStateNormal];
    [_changeBtn setTitleColor:MainColor forState:UIControlStateNormal];
    [_changeBtn addTarget:self action:@selector(changeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.calloutView addSubview:_changeBtn];
}

-(void)cancelAction:(UIButton *)btn
{
    NSLog(@"点击了取消");
    [self.calloutView removeFromSuperview];
    [self setSelected:NO];
}

-(void)changeAction:(UIButton *)btn
{
    NSLog(@"点击了");
    [self setSelected:NO];
    
    
}



- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL inside = [super pointInside:point withEvent:event];

    if (!inside && self.selected)
    {
        inside = [self.calloutView pointInside:[self convertPoint:point toView:self.calloutView] withEvent:event];
    }
    
    return inside;
}



@end

以上就是在view里面重组自己想要的UI效果,点击事件的话就是那个按钮,也可以直接写个代理方法,到主页面调用!

3,在首页展示自定义大头针:

[self.mapView addAnnotations:self.annotations];
[self.mapView showAnnotations:self.annotations edgePadding:UIEdgeInsetsMake(20, 20, 20, 80) animated:YES];

 self.annotations这个是个数组,里面就是经纬度,然后就能在地图上展示了!

4,最主要的就是实现这个方法:

/* 实现代理方法:*/
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAUserLocation class]]) {
        return nil;
    }
    
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        
        if (annotationView == nil)
        {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
        }
        annotationView.image = [UIImage imageNamed:@"ding"];
        
        // 设置为NO,用以调用自定义的calloutView
        annotationView.canShowCallout = NO;
//没有排队
        =[annotationView setDataAry:self.contentAry];
// 设置中心点偏移,使得标注底部中间点成为经纬度对应点
        annotationView.centerOffset = CGPointMake(0, -18);
        
        return annotationView;
    }
    return nil;
    
}

这样就可以了!

其实主要的就上面这个方法!

猜你喜欢

转载自www.cnblogs.com/hero11223/p/8910533.html