ios – 使用GMUClusterManager的自定义标记

我想使用GMUClusterManager显示自定义标记.我按照标记聚类 here的所有步骤进行操作.

但是有这样的蓝色和红色图标.

但是,当我放大该地图时,它只显示红色标记,但我不希望这样.

有实例方法,我已经实现了我的逻辑,但没有用.

- (instancetype)initWithMapView:(GMSMapView *)mapView clusterIconGenerator:(id<GMUClusterIconGenerator>)iconGenerator
{
    if ((self = [super init])) {

        GMSMarker *marker= [GMSMarker markerWithPosition:CLLocationCoordinate2DMake(24.0,75.30)];

        UIView *customMarker =[[UIView alloc] initWithFrame:CGRectMake(0,63,40)];
        customMarker.backgroundColor = [UIColor blueColor];

        marker.iconView = [self EmployeeMarker:0] ;
        marker.appearanimation = kGMSMarkerAnimationPop;
        marker.map = mapView;
    }
    return self;
}

-(UIView *)EmployeeMarker:(int)labelTextInt{
    UIView *customMarker =[[UIView alloc] initWithFrame:CGRectMake(0,40)];
    UIImageView *imgViewCustomMarker = [[UIImageView alloc]initWithFrame:CGRectMake(0,15,24,25)];
    imgViewCustomMarker.image = [UIImage imageNamed:@"iconMapUser.png"];
    [customMarker addSubview:imgViewCustomMarker];
    UIView *viewratingCustom = [[UIView alloc] initWithFrame:CGRectMake(15,40,15)];
    viewratingCustom.backgroundColor = [UIColor colorWithRed:192.0/255.0 green:192.0/255.0 blue:192.0/255.0 alpha:1.0];
    UILabel *lblratingEmployees = [[UILabel alloc] initWithFrame:CGRectMake(8,1,17,8)];
    lblratingEmployees.textColor = [UIColor colorWithRed:0.00/255.0 green:100.0/255.0 blue:150.0/255.0 alpha:1.0];
    lblratingEmployees.text = @"1";
    lblratingEmployees.font = [UIFont fontWithName:@"Helvetica-Bold" size:10];
    [lblratingEmployees sizetoFit];
    [viewratingCustom addSubview:lblratingEmployees];
    UIImageView *imageViewStar = [[UIImageView alloc] initWithFrame:CGRectMake(25,3,10,8)];
    imageViewStar.image = [UIImage imageNamed:@"iconBlueStar.png"];
    [viewratingCustom addSubview:imageViewStar];
    [customMarker addSubview:viewratingCustom];
    return customMarker;
}

我已经使用这种方法显示认为红色的标记数量.

id<GMUClusteralgorithm> algorithm = [[GMUNonHierarchicaldistanceBasedAlgorithm alloc] init];

id<GMUClusterIconGenerator> iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init];


id<GMUClusterRenderer> renderer =
  [[GMUDefaultClusterRenderer alloc] initWithMapView:_mapView
                                clusterIconGenerator:iconGenerator];

_clusterManager =
  [[GMUClusterManager alloc] initWithMap:_mapView algorithm:algorithm renderer:renderer];

 // Generate and add random items to the cluster manager.

// [self generateClusterItems];


for (int i = 0; i<latitudeArray.count; i++) {

    id<GMUClusterItem> item =

    [[POIItem alloc]initWithPosition:CLLocationCoordinate2DMake([[latitudeArray objectAtIndex:i]doubleValue],[[longitudeArray objectAtIndex:i]doubleValue]) name:@"Name"];

    [_clusterManager addItem:item];
}

Adde代表和集群方法.

[_clusterManager cluster];
 [_clusterManager setDelegate:self mapDelegate:self];

所以请帮我添加自定义标记来代替认的红色.

解决方法

您需要创建符合GMUClusterIconGenerator协议的自定义类:

CustomClusterIconGenerator.h文件

@interface CustomClusterIconGenerator : NSObject
<GMUClusterIconGenerator>

@end

CustomClusterIconGenerator.m文件

@implementation CustomClusterIconGenerator

- (UIImage *)iconForSize:(NSUInteger)size {
    // Return custom icon for cluster
    return [UIImage imageNamed:@"Your Custom Cluster Image"];
}

- (UIImage *)iconForMarker {
    // Return custom icon for pin
    return [UIImage imageNamed:@"Your Custom Marker Image"];
}

- (CGPoint)markerIconGroundAnchor {
    // If your marker icon center shifted,return custom value for anchor
    return CGPointMake(0,0);
}

- (CGPoint)clusterIconGroundAnchor {
    // If your cluster icon center shifted,0);
}

@end

然后,而不是

id<GMUClusterIconGenerator> iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init];

使用

CustomClusterIconGenerator *iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init];

这是我的项目的例子:

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...