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;
    
}

這樣就可以了!

 

其實主要的就上面這個方法!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM