自定义Google Map样式的蓝点表示当前位置和精确度

用了MapKit的同学用不着这个,只是为了自定义地图引擎用的。
其实主要的想法就是创建一个CALayer的子类,包含两个部分,一个始终居中的蓝色 图标,和一个圆形的layer背景表示GPS的精确度。每次GPS更新后,通过修改layer的大小,实现像Google map那个蓝点一样的效果。layer的大小通过计算地图比例尺得到,然后再根据CLLocation中的horizontalAccuracy计算出来。比如已知地图两个上端点的坐标,计算这两个点的实际距离,然后计算出这个距离与显示像素的比例,那么layer的大小就是:

diameter = accuracy/(distance/nbPixs)

/**
 *    @file            BlueCircleLayer.h
 *    @author         
 *    @date           
 *    @version        
 *    @description    
 *    @copyright        
 *    @brief
 */
 
@interface BlueCircleLayer : CALayer {
 
}
 
/*!
 *  change the size, keey as a circle
 */
- (void)resize:(CGFloat)diameter;
 
@end


 
/**
 *    @file            BlueCircleLayer.m
 *    @author         
 *    @date           
 *    @version        
 *    @description    
 *    @copyright        
 *    @brief
 */
 
#import "BlueCircleLayer.h"
 
 
@implementation BlueCircleLayer
 
/*!
 *  @override
 */
- (id)init
{
    self = [super init];
    if (self) {
        [self setNeedsDisplayOnBoundsChange:TRUE];        
        [self setBorderWidth:1];
        [self setBorderColor:[[UIColor colorWithRed:64/255.0f green:87/255.0f blue:188/255.0f alpha:1] CGColor]];
        [self setBackgroundColor:[[UIColor colorWithRed:102/255.0f green:153/255.0f blue:255/255.0f alpha:0.3f] CGColor]];
    }
    return self;
}
 
/*!
 *  @override
 */
- (void)drawInContext:(CGContextRef)ctx
{
    UIImage * image = [UIImage imageNamed:@"bluePoint.png"];
    //keep the blue point is in the center of the layer
    CGContextDrawImage(ctx, CGRectMake(self.frame.size.width/2.0f - 10, self.frame.size.height/2.0f - 11, 21, 23), image.CGImage);
}
 
/*!
 *  change the size, keey as a circle
 *  @param diameter diameter of the circle
 */
- (void)resize:(CGFloat)diameter
{
    if (diameter >= 25) {   //if smaller than the img
        [self setBorderWidth:1];
        [self setBorderColor:[[UIColor colorWithRed:64/255.0f green:87/255.0f blue:188/255.0f alpha:1] CGColor]];
        [self setBackgroundColor:[[UIColor colorWithRed:102/255.0f green:153/255.0f blue:255/255.0f alpha:0.3f] CGColor]];
        [self setFrame:CGRectMake(0, 0, diameter, diameter)];
        [self setCornerRadius:diameter/2.0f]; 
    } else {
        [self setBackgroundColor:[[UIColor clearColor] CGColor]];
        [self setBorderColor:[[UIColor clearColor] CGColor]];
        [self setFrame:CGRectMake(0, 0, 25, 25)];
    }
 
}
 
@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值