自定义标记使用GMUClusterManager [英] Custom Marker using GMUClusterManager
问题描述
我想使用 GMUClusterManager
来显示自定义标记。我遵循了标记群集的所有步骤
但是当我放大地图时,它只显示红色标记,但我不'
有实例方法,我已经实现了我的逻辑,但没有用。
< $(){pre $
- (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,0,63,40)];
customMarker.backgroundColor = [UIColor blueColor];
marker.iconView = [self EmployeeMarker:0];
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = mapView;
}
返回自我;
$ b - (UIView *)EmployeeMarker:(int)labelTextInt {
UIView * customMarker = [[UIView alloc] initWithFrame:CGRectMake(0,0,63,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,0,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-Boldsize: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];
返回customMarker;
}
我已经使用这种方法显示可能的标记数量,默认为红色。
id< GMUClusterAlgorithm>算法= [[GMUNonHierarchicalDistanceBasedAlgorithm alloc] init];
id< GMUClusterIconGenerator> iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init];
id< GMUClusterRenderer> renderer =
[[GMUDefaultClusterRenderer alloc] initWithMapView:_mapView $ b $ clusterIconGenerator:iconGenerator];
_clusterManager =
[[GMUClusterManager alloc] initWithMap:_mapView algorithm:algorithm renderer:renderer];
//生成并向集群管理器添加随机项目。
// [self generateClusterItems];
for(int i = 0; i
id< GMUClusterItem> item =
$ b $ [[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
protocol:
CustomClusterIconGenerator.h文件
@interface CustomClusterIconGenerator :NSObject
< GMUClusterIconGenerator>
@end
CustomClusterIconGenerator.m文件
@implementation CustomClusterIconGenerator
$ b $ - (UIImage *)iconForSize:(NSUInteger)大小{
//返回自定义图标cluster
return [UIImage imageNamed:@Your Custom Cluster Image];
$ b - (UIImage *)iconForMarker {
//返回针脚
返回的自定义图标[UIImage imageNamed:@Your Custom Marker Image];
$ b - (CGPoint)markerIconGroundAnchor {
//如果您的标记图标居中移位,返回锚点
的自定义值返回CGPointMake(0,0);
$ b - (CGPoint)clusterIconGroundAnchor {
//如果您的集群图标居中移位,返回锚点
的自定义值返回CGPointMake(0,0);
}
@end
然后,
id< GMUClusterIconGenerator> iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init];
使用
CustomClusterIconGenerator * iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init];
I want to display Custom marker using GMUClusterManager
. I followed all steps for marker clustering here.
but there is blue and red color Icon like this.
But when I Zoom in that map it display only red color Marker but I don't want that.
there is instance method where I have implemented my logic but no use.
- (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, 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, 0, 63, 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, 0, 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;
}
I have used this method for display possible number of Marker that is by default red.
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 delegates and also cluster method.
[_clusterManager cluster];
[_clusterManager setDelegate:self mapDelegate:self];
So please help me for adding custom marker in place of red that is in default.
You need to create custom class, which conforms to GMUClusterIconGenerator
protocol:
CustomClusterIconGenerator.h file
@interface CustomClusterIconGenerator : NSObject
<GMUClusterIconGenerator>
@end
CustomClusterIconGenerator.m file
@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, return custom value for anchor
return CGPointMake(0, 0);
}
@end
and then then, instead of
id<GMUClusterIconGenerator> iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init];
use
CustomClusterIconGenerator *iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init];
Here is example from my project:
这篇关于自定义标记使用GMUClusterManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!