2016-09-07 1 views
1

Ich verwende Google Maps SDK in meiner iOS App. Ich bevölke die Karte mit den Clustering-Methoden.iOS Google Maps, verschiedene benutzerdefinierte Bilder für Cluster und einzelne Markierungen

Ich habe benutzerdefinierte Bilder für die verschiedenen Clustering Eimer ex. 10,20 ...

Die einzelnen Marker haben jedoch die Standardeinstellung (google maps red marker icon).

Ich möchte ein benutzerdefiniertes Symbol für Clustering und ein anderes für einzelne Markierungen.

Innerhalb der Methoden, die den Cluster rendern, die Marker hinzufügen, wenn Sie die Markierungssymbole festlegen, ändert es alle Bilder nicht nur Singles.

Wie lege ich verschiedene Symbole für Singles und Cluster fest?

dies fügt die Elemente clusterManager

 id<GMUClusterItem> item = 
[[POIItem alloc] initWithPosition:CLLocationCoordinate2DMake([bay.latitude doubleValue], [bay.longitude doubleValue]) name:bay.name status:bay.marker_status]; 
[clusterManager addItem:item]; 

Hier habe ich die Symbole für die Cluster-Eimer

 - (id<GMUClusterIconGenerator>)iconGeneratorWithImages { 
    return [[GMUDefaultClusterIconGenerator alloc] initWithBuckets:@[ @10, @50, @100, @200, @1000 ] 
                backgroundImages:@[ 
                    [UIImage imageNamed:@"big_parking_pin_img"], 
                    [UIImage imageNamed:@"big_parking_pin_img"], 
                    [UIImage imageNamed:@"big_parking_pin_img"], 
                    [UIImage imageNamed:@"big_parking_pin_img"], 
                    [UIImage imageNamed:@"big_parking_pin_img"] 
                    ]]; 
} 

Dies ist, wo die Google-Cluster-Klasse fügt Marker

 - (void)renderCluster:(id<GMUCluster>)cluster animated:(BOOL)animated { 
    float zoom = _mapView.camera.zoom; 
    if ([self shouldRenderAsCluster:cluster atZoom:zoom]) { 
    CLLocationCoordinate2D fromPosition; 
    if (animated) { 
     id<GMUCluster> fromCluster = 
      [self overlappingClusterForCluster:cluster itemMap:_itemToOldClusterMap]; 
     animated = fromCluster != nil; 
     fromPosition = fromCluster.position; 
    } 

    UIImage *icon = [_clusterIconGenerator iconForSize:cluster.count]; 
    GMSMarker *marker = [self markerWithPosition:cluster.position 
              from:fromPosition 
             userData:cluster 
            clusterIcon:icon 
             animated:animated]; 

    [_markers addObject:marker]; 
    } else { 
    for (id<GMUClusterItem> item in cluster.items) { 
     CLLocationCoordinate2D fromPosition; 
     BOOL shouldAnimate = animated; 
     if (shouldAnimate) { 
     GMUWrappingDictionaryKey *key = [[GMUWrappingDictionaryKey alloc] initWithObject:item]; 
     id<GMUCluster> fromCluster = [_itemToOldClusterMap objectForKey:key]; 
     shouldAnimate = fromCluster != nil; 
     fromPosition = fromCluster.position; 
     } 

     GMSMarker *marker = [self markerWithPosition:item.position 
               from:fromPosition 
              userData:item 
             clusterIcon:nil 
              animated:shouldAnimate]; 
     [_markers addObject:marker]; 
     [_renderedClusterItems addObject:item]; 
    } 
    } 
    [_renderedClusters addObject:cluster]; 
} 

// Returns a marker at final position of |position| with attached |userData|. 
// If animated is YES, animates from the closest point from |points|. 
- (GMSMarker *)markerWithPosition:(CLLocationCoordinate2D)position 
          from:(CLLocationCoordinate2D)from 
         userData:(id)userData 
         clusterIcon:(UIImage *)clusterIcon 
         animated:(BOOL)animated { 
    CLLocationCoordinate2D initialPosition = animated ? from : position; 
    GMSMarker *marker = [GMSMarker markerWithPosition:initialPosition]; 
    marker.userData = userData; 
    if (clusterIcon != nil) { 
    marker.icon = clusterIcon; 
    marker.groundAnchor = CGPointMake(0.5, 0.5); 
    } 
    marker.map = _mapView; 

    if (animated) { 
    [CATransaction begin]; 
    [CATransaction setAnimationDuration:kGMUAnimationDuration]; 
    marker.layer.latitude = position.latitude; 
    marker.layer.longitude = position.longitude; 
    [CATransaction commit]; 
    } 
    return marker; 
} 

Antwort

1

ich hinzufügen hatte das ähnliche Problem vor 2 Tagen und ich habe gerade die Lösung gefunden. Ich hoffe, es wird dir nützlich sein. Zum Beispiel haben Sie eine mapView und Sie setzen einen Delegaten es in richtigen Ort:

[self.mapView setDelegate:self]; 

Dann müssen Sie die optionale Methode von GMSMapViewDelegate Protokoll implementieren:

- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position { 
    [self performSelector:@selector(updateMarkers) withObject:nil afterDelay:0.2]; 
} 

ich Verzögerung 0,2 Sekunden, weil Marker ihre Symbole nicht aktualisieren würden, wenn Sie einen kleineren Wert verwenden. Der nächste Schritt ist Verfahren zum Aktualisieren von Symbolen implementieren:

-(void) updateMarkers { 
    // "mapView" property in your self.mapView has type GMSVectorMapView, 
    //and it is hidden, so you can't get like self.mapView.mapView 
    id vectorMap = [self.mapView valueForKey:@"mapView"]; 

    // "accessibilityItems" - property that have all items in visible part of map. 
    NSMutableArray* GMSMarkersArray = [vectorMap mutableArrayValueForKey:@"accessibilityItems"]; 

    // Very often you'll get object of GMSPointOfInteretUIItem class, and you don't need it =) 
    NSMutableArray *discardedItems = [NSMutableArray array]; 
    for (id item in GMSMarkersArray) { 
     if (![item isKindOfClass:[GMSMarker class]]) 
      [discardedItems addObject:item]; 
    } 
    [GMSMarkersArray removeObjectsInArray:discardedItems]; 

    // If marker don't have icon image, he use default red pin, but property is still have nil-value ... 
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"icon = nil"]; 
    NSArray* singleMarkers = [GMSMarkersArray filteredArrayUsingPredicate:predicate]; 

    // ... and here you can setup any icon you want, for all singles markers in visible part of map. 
    for(GMSMarker* marker in singleMarkers) { 
     marker.icon = [UIImage imageNamed:@"yourIcon.png"]; 
    } 
} 

Auch wenn Sie Ihren eigenen Marker es schaffen und in dem Cluster, können Sie es von Userdata-Eigenschaft von GMSMarker Objekt in letzter Schleife erhalten. Und zum Beispiel haben Sie dort Ihre individuelle Markierung mit dem Symbol Sie wollen, nur letzte Schleife wie für etwas ändern:

for(GMSMarker* marker in singleMarkers) { 
     YourMarkerClass* yourMaker = marker.userData; 
     marker.icon = yourMaker.icon; 
} 

Sorry für mögliche Fehler und die Fragen stellen, wenn Sie etwas nicht verstehen =)

Verwandte Themen