2016-11-28 13 views
8

Chcę wyświetlić niestandardowy znacznik za pomocą GMUClusterManager. Wykonałem wszystkie kroki związane z grupowaniem znaczników here.Niestandardowy znacznik za pomocą GMUClusterManager

ale jest niebieski i czerwony kolor Ikona podobna do tej. enter image description here

Ale kiedy powiększam tę mapę, wyświetlany jest tylko znacznik koloru czerwonego, ale nie chcę tego.

jest metoda instancji, w której zaimplementowałem moją logikę, ale nie używam.

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

Użyłem tej metody do wyświetlania możliwej liczby Marker, który domyślnie jest czerwony.

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 delegatów, a także metodę klastra.

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

Więc proszę mi pomóc do dodawania niestandardowych znacznik zamiast czerwieni, która jest w zwłoce.

Odpowiedz

7

Musisz utworzyć klasę niestandardową, zgodnego GMUClusterIconGenerator protokole:

plików CustomClusterIconGenerator.h

@interface CustomClusterIconGenerator : NSObject 
<GMUClusterIconGenerator> 

@end 

plików 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, return custom value for anchor 
    return CGPointMake(0, 0); 
} 

@end 

a następnie wtedy, zamiast

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

użycie

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

Oto przykład z mojego projektu: enter image description here

+0

CustomClusterIconGenerator iconGenerator = [[GMUDefaultClusterIconGenerator Alloc] startowych]; w tym miejscu wystąpił błąd. –

+0

iconForMarker nie działa ... ponieważ znacznik clusterd działa przy użyciu tego kodu - (UIImage *) iconForSize: (NSUInteger) rozmiar –

+0

@MadBurea przepraszam, był błąd. Powinien to być 'CustomClusterIconGenerator * iconGenerator'. Zaktualizowałem odpowiedź. –

3

Możesz sprawdzić samouczek - Marker Clustering with Google's Utility library for Maps SDK (Google-Maps-iOS-Utils), to obecny idealnym próbkę na rzeczy, które chcą wdrożyć w swojej aplikacja. Istnieje również Google sample codes z blog, którego możesz chcieć wypróbować. Wreszcie w github pojawił się problem o numerze Way to customize individual markers added to cluster manager?, który może pomóc ci zrozumieć, jak dostosować znaczniki w GMUClusterManager. Mam nadzieję że to pomoże.

+0

Nie używam wszystkich linków, które wypróbowałem wcześniej –

1

Od wersji 1.1.0 dodano nowe funkcje ułatwiające dostosowywanie znaczników (read more).

Możesz dodać GMUClusterRendererDelegate i GMUDefaultClusterRenderer.h i dodać metodę - (void)renderer:(id<GMUClusterRenderer>)renderer willRenderMarker:(GMSMarker *)marker;

Tam można dostosować znaczniki i klastrów.Np

- (void)renderer:(id<GMUClusterRenderer>)renderer willRenderMarker:(GMSMarker *)marker{ 
    if ([marker.userData conformsToProtocol:@protocol(GMUCluster)]) { 
     marker.icon=[UIImage imageNamed:@"custom_cluster_image.png"]; 
    }else if ([marker.userData conformsToProtocol:@protocol(GMUClusterItem)]) { 
     marker.icon=[UIImage imageNamed:@"custom_marker_image.png"]; 
    } 
} 

pamiętać, aby ustawić pełnomocnik odpowiednio:

id<GMUClusterRenderer> renderer = [[GMUDefaultClusterRenderer alloc] initWithMapView:_mapView clusterIconGenerator:iconGenerator]; 
((GMUDefaultClusterRenderer *)renderer).delegate=self; 
Powiązane problemy