在iOS中的标记群集中自定义标记图标

时间:2016-09-30 09:42:37

标签: ios objective-c google-maps markerclusterer

有没有办法在标记聚类中更改默认标记图标?

这是我的代码......

protected void Application_BeginRequest()
{
    if (!Context.Request.IsSecureConnection)
        Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
}

请指导我......

2 个答案:

答案 0 :(得分:2)

我看到你使用了google-maps-ios-utils。问题是还没有用于更改标记图标的API。您只能直接在库的代码中执行此操作。我已将自定义代码粘贴到方法

- (GMSMarker *)markerWithPosition:(CLLocationCoordinate2D)position
                         from:(CLLocationCoordinate2D)from
                     userData:(id)userData
                  clusterIcon:(UIImage *)clusterIcon
                     animated:(BOOL)animated{
//...
    if (clusterIcon != nil) {
        marker.icon = clusterIcon;
        marker.groundAnchor = CGPointMake(0.5, 0.5);
    } else {
    if([[marker.userData class] isSubclassOfClass:[POIItem class]]){
        POIItem *item = (POIItem *)marker.userData;
        MarkerIcon* markerView = (MarkerIcon *)[[NSBundle mainBundle] loadNibNamed:@"MarkerIcon" owner:marker options:nil][0];
        marker.iconView = markerView;
        marker.groundAnchor = CGPointMake(0.5, 0.5);
    }
   }
}

这不是改变这样的代码的好方法。但是那个时刻我找不到更好的解决方案。

答案 1 :(得分:1)

我已经解决了这个问题。

我使用了googleMaps Api版本:8.1。

这是我的代码......

#import "Clustering/GMUClusterItem.h"

// Point of Interest Item which implements the GMUClusterItem protocol.
@interface POIItem : NSObject<GMUClusterItem>

@property(nonatomic, readonly) CLLocationCoordinate2D position;
@property(nonatomic, readonly) NSString *name;

- (instancetype)initWithPosition:(CLLocationCoordinate2D)position name:(NSString *)name;

@end

1.在地图上。

@interface BasicViewController ()<GMUClusterManagerDelegate, GMSMapViewDelegate,
                                  GMUClusterRendererDelegate>
@end

typedef NS_ENUM(NSInteger, ClusterAlgorithmMode) {
  kClusterAlgorithmGridBased,
  kClusterAlgorithmQuadTreeBased,
};

@implementation BasicViewController {
  GMSMapView *_mapView;
  GMUClusterManager *_clusterManager;
}

- (void)loadView {

    //创建地图
  GMSCameraPosition *camera =
      [GMSCameraPosition cameraWithLatitude:kCameraLatitude longitude:kCameraLongitude zoom:10];

  _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

  self.view = _mapView;
}

2.creat GMUClusterManager对象。

- (void)viewDidLoad {
  [super viewDidLoad];

    //添加标注算法方式
    id<GMUClusterAlgorithm> algorithm = [self algorithmForMode:kClusterAlgorithmQuadTreeBased];

    //标注icon
    id<GMUClusterIconGenerator> iconGenerator = [self iconGeneratorWithImages];//[self defaultIconGenerator];

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

  GMUDefaultClusterRenderer *renderer =
      [[GMUDefaultClusterRenderer alloc] initWithMapView:_mapView
                                    clusterIconGenerator:iconGenerator];
  renderer.delegate = self;

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

  // Generate and add random items to the cluster manager.
    //将标注添加到地图上
  [self generateClusterItems];

  // Call cluster() after items have been added to perform the clustering and rendering on map.
    //展示
  [_clusterManager cluster];

  // Register self to listen to both GMUClusterManagerDelegate and GMSMapViewDelegate events.
  [_clusterManager setDelegate:self mapDelegate:self];

  UIBarButtonItem *removeButton =
      [[UIBarButtonItem alloc] initWithTitle:@"Remove"
                                       style:UIBarButtonItemStylePlain
                                      target:self
                                      action:@selector(removeClusterManager)];
  self.navigationItem.rightBarButtonItems = @[ removeButton ];
}

3.in方法:

/*You can set marker image here

 if [marker class] is POIItem.

 */

- (void)renderer:(id<GMUClusterRenderer>)renderer willRenderMarker:(GMSMarker *)marker {

  if ([marker.userData isKindOfClass:[POIItem class]]) {

    POIItem *item = marker.userData;

    marker.title = item.name;

    ******marker.icon = [UIImage imageNamed:@"register"];******

  }
}