2016-09-07 28 views
1

我在iOS應用中使用Google Maps SDK。我正在使用聚類方法填充地圖。iOS谷歌地圖,用於羣集和單個標記的不同自定義圖像

我已經爲不同的聚類桶設置了自定義圖像。 10,20 ...

然而,各個標記具有默認值(谷歌地圖紅色標記圖標)。

我想要一個自定義圖標用於聚類,另一個用於單個標記。

但是在呈現添加標記的集羣的方法中,如果您設置標記圖標,它將更改所有圖像,而不僅僅是單個圖像。

如何爲單曲和羣集設置不同的圖標?

這會將項目添加到clusterManager

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

在這裏,我添加了圖標集羣桶

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

這是谷歌的羣類添加了標記

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

回答

1

我2天前有類似的問題,我剛剛找到解決方案。希望它對你有用。 例如你有一個MapView,你在正確的地方設置一個委託給它:

[self.mapView setDelegate:self]; 

然後,你需要實現從GMSMapViewDelegate協議的可選方法:

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

我用延時0.2秒,因爲如果您使用較小的值,標記不會更新它們的圖標。 下一步是實現方法來更新圖標:

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

此外,如果你創建自己的標記,並將其添加到集羣中,你可以從最後一個循環GMSMarker對象的用戶數據屬性得到它。再舉例來說,你有沒有跟你的圖標標記定製你想要的,只是改變最後的循環類似:

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

對不起,可能存在的錯誤和提出的問題,如果你不明白的東西=)