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

来源:互联网 发布:软件水平考试报名 编辑:程序博客网 时间:2024/04/29 05:52
用了MapKit的同学用不着这个,只是为了自定义地图引擎用的。
其实主要的想法就是创建一个CALayer的子类,包含两个部分,一个始终居中的蓝色图标,和一个圆形的layer背景表示GPS的精确度。每次GPS更新后,通过修改layer的大小,实现像Googlemap那个蓝点一样的效果。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


原创粉丝点击