在 iOS 中自定义标记聚类中的标记图标
customize marker icon in marker clustering in iOS
有什么方法可以更改标记聚类中的默认标记图标吗?
这是我的代码...
- (void)viewDidLoad {
[super viewDidLoad];
// Set up the cluster manager with a supplied icon generator and renderer.
id<GMUClusterAlgorithm> algorithm = [[GMUNonHierarchicalDistanceBasedAlgorithm alloc] init];
id<GMUClusterIconGenerator> iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init];
id<GMUClusterRenderer> renderer = [[GMUDefaultClusterRenderer alloc] initWithMapView:googleMapView
clusterIconGenerator:iconGenerator];
clusterManager = [[GMUClusterManager alloc] initWithMap:googleMapView
algorithm:algorithm
renderer:renderer];
// Register self to listen to both GMUClusterManagerDelegate and
// GMSMapViewDelegate events.
[clusterManager setDelegate:self mapDelegate:self];
}
- (void)loadView {
// Create a GMSCameraPosition that tells the map to display the
_camera = [GMSCameraPosition cameraWithLatitude:29.3117
longitude:47.4818
zoom:8];
googleMapView = [GMSMapView mapWithFrame:CGRectZero camera:_camera];
googleMapView.myLocationEnabled = YES;
googleMapView.settings.compassButton = YES;
googleMapView.settings.myLocationButton = YES;
googleMapView.delegate = self;
self.view = googleMapView;
}
-(void)setLocation:(CLLocation *)location
{
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
[googleMapView animateToLocation:center];
[googleMapView animateToZoom:12];
NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"29.0827,48.1363",
@"29.2679,47.9927",
@"29.348706, 48.092425",
@"29.340925, 48.088477",
@"29.324912, 48.089850",
@"29.330599, 47.990630",
@"29.300364, 47.960589",
@"29.271917, 47.918017",
@"29.3032,47.936", nil];
//remove all clusters before adding clusters
[clusterManager clearItems];
for (int i = 0; i < [array count]; i++)
{
center = CLLocationCoordinate2DMake ([[array[i] componentsSeparatedByString:@","][0] floatValue], [[array[i] componentsSeparatedByString:@","][1] floatValue]);
// Add items to the cluster manager.
NSString *name = nil;//[NSString stringWithFormat:@"Item %d", i];
id<GMUClusterItem> item =[[POIItem alloc] initWithPosition:center
name:name];
[clusterManager addItem:item];
}
// Call cluster() after items have been added
// to perform the clustering and rendering on map.
[clusterManager cluster];
}
请指导我...
我看到您使用了 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);
}
}
}
这样改代码可不是什么好办法。但当时我找不到更好的解决方案。
这个问题我已经解决了
我使用了 googleMaps Api version: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.creat地图。
@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"];******
}
}
有什么方法可以更改标记聚类中的默认标记图标吗?
这是我的代码...
- (void)viewDidLoad {
[super viewDidLoad];
// Set up the cluster manager with a supplied icon generator and renderer.
id<GMUClusterAlgorithm> algorithm = [[GMUNonHierarchicalDistanceBasedAlgorithm alloc] init];
id<GMUClusterIconGenerator> iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init];
id<GMUClusterRenderer> renderer = [[GMUDefaultClusterRenderer alloc] initWithMapView:googleMapView
clusterIconGenerator:iconGenerator];
clusterManager = [[GMUClusterManager alloc] initWithMap:googleMapView
algorithm:algorithm
renderer:renderer];
// Register self to listen to both GMUClusterManagerDelegate and
// GMSMapViewDelegate events.
[clusterManager setDelegate:self mapDelegate:self];
}
- (void)loadView {
// Create a GMSCameraPosition that tells the map to display the
_camera = [GMSCameraPosition cameraWithLatitude:29.3117
longitude:47.4818
zoom:8];
googleMapView = [GMSMapView mapWithFrame:CGRectZero camera:_camera];
googleMapView.myLocationEnabled = YES;
googleMapView.settings.compassButton = YES;
googleMapView.settings.myLocationButton = YES;
googleMapView.delegate = self;
self.view = googleMapView;
}
-(void)setLocation:(CLLocation *)location
{
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
[googleMapView animateToLocation:center];
[googleMapView animateToZoom:12];
NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"29.0827,48.1363",
@"29.2679,47.9927",
@"29.348706, 48.092425",
@"29.340925, 48.088477",
@"29.324912, 48.089850",
@"29.330599, 47.990630",
@"29.300364, 47.960589",
@"29.271917, 47.918017",
@"29.3032,47.936", nil];
//remove all clusters before adding clusters
[clusterManager clearItems];
for (int i = 0; i < [array count]; i++)
{
center = CLLocationCoordinate2DMake ([[array[i] componentsSeparatedByString:@","][0] floatValue], [[array[i] componentsSeparatedByString:@","][1] floatValue]);
// Add items to the cluster manager.
NSString *name = nil;//[NSString stringWithFormat:@"Item %d", i];
id<GMUClusterItem> item =[[POIItem alloc] initWithPosition:center
name:name];
[clusterManager addItem:item];
}
// Call cluster() after items have been added
// to perform the clustering and rendering on map.
[clusterManager cluster];
}
请指导我...
我看到您使用了 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);
}
}
}
这样改代码可不是什么好办法。但当时我找不到更好的解决方案。
这个问题我已经解决了
我使用了 googleMaps Api version: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.creat地图。
@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"];******
}
}