2017-01-10 12 views
0

Google Map iOS SDK(Objective C)を使用してアプリを開発しています。GoogleMap iOS SDKのマーカークラスタに既存のマーカーを追加するにはどうすればよいですか?

は、これまでに完了:

  • ロードされたマーカーを、サーバからマップビューに
  • がランダムクラスタのみを生成します。

私はこのようなプロジェクトを実行します。

Screen shot of map view

これは、両方のズームレベル10でマップビューにマーカーおよびクラスタの両方を示している。しかし、私はそれにズームしたとき、私はまず、クラスタを見せたかった本当のマーカーが表示されるはずです。マップにクラスターを表示する方法がわからないため、作成したランダムに生成されたマーカーではありません。ここで

は、偽のURLリンクとの完全なコードです:ビューに変更

#import "ViewController.h" 
//#import "CSMarker.h" 
#import <GoogleMaps/GoogleMaps.h> 
#import <Google-Maps-iOS-Utils/GMUMarkerClustering.h> 

//importing POI Item object - points of interest 

@interface POIItem : NSObject<GMUClusterItem> 

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

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

@end 

@implementation POIItem 

- (instancetype)initWithPosition:(CLLocationCoordinate2D)position name:(NSString *)name { 
    if ((self = [super init])) { 
     _position = position; 
     _name = [name copy]; 
    } 
    return self; 
} 

@end 

//implementation start - map view controller 

static const NSUInteger kClusterItemCount = 60; 
static const double kCameraLatitude = 25.277683999999997; 
static const double kCameraLongitude = 55.309802999999995; 

@interface ViewController()<GMUClusterManagerDelegate, GMSMapViewDelegate> 
{ 

    NSMutableArray *waypoints_; 
    NSMutableArray *waypointStrings_; 
    GMSMapView *_mapView; 
    GMUClusterManager *_clusterManager; 



} 
@property(strong, nonatomic) NSURLSession *markerSession; 
@property(strong, nonatomic) GMSMapView *mapView; 
@property(copy, nonatomic) NSSet *markers; 

@property(nonatomic, strong) NSMutableArray *markersArray; 

@end 

@implementation ViewController 
@synthesize gs; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:kCameraLatitude 
                  longitude:kCameraLongitude 
                   zoom:10]; 

    self.mapView = 
    [GMSMapView mapWithFrame:self.view.bounds camera:camera]; 
    [self.view addSubview:self.mapView]; 

    self.mapView.settings.compassButton = YES; 
    self.mapView.settings.myLocationButton = YES; 

    //setup the cluster manager 



    NSURLSessionConfiguration *config = 
    [NSURLSessionConfiguration defaultSessionConfiguration]; 
    config.URLCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024 
                diskCapacity:10 * 1024 * 1024 
                 diskPath:@"MarkerData"]; 
    self.markerSession = [NSURLSession sessionWithConfiguration:config]; 

    [self downloadMarkerData]; 

    //cluster load setup 

    id<GMUClusterAlgorithm> algorithm = [[GMUNonHierarchicalDistanceBasedAlgorithm alloc] init]; 
    id<GMUClusterIconGenerator> iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init]; 
    id<GMUClusterRenderer> renderer = 
    [[GMUDefaultClusterRenderer alloc] initWithMapView:self.mapView 
            clusterIconGenerator:iconGenerator]; 
    _clusterManager = 
    [[GMUClusterManager alloc] initWithMap:self.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]; 

    // Do any additional setup after loading the view, typically from a nib. 
} 

- (NSMutableArray *)markersArray 
{ 
    if (!_markersArray) { 
     _markersArray = [NSMutableArray array]; 
    } 
    return _markersArray; 
} 



- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

//downloading marker data 

- (void)downloadMarkerData { 

    NSURL *lakesURL = 
    [NSURL URLWithString:@"http://myscrap.com/xxx.php/webservice/xxx/xxxx"]; 

    NSURLSessionDataTask *task = [self.markerSession dataTaskWithURL:lakesURL 
                completionHandler:^(NSData *data, NSURLResponse *response, NSError *e) 
            { 

             NSArray *json = [NSJSONSerialization JSONObjectWithData:data 
                         options:0 
                         error:nil]; 
             NSLog(@"json: %@",json); 

             [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 

             [self createMarkerObjectsWithJson:json]; 

             }]; 


            }]; 
    [task resume]; 
} 

-(void)drawMarkers 
{ 
    for(GMSMarker *marker in self.markers) { 

     if(marker.map == nil) { 
      marker.map = self.mapView; 
     } 

    } 
} 




-(void)createMarkerObjectsWithJson:(NSArray *)json 
{ 


    NSMutableSet *mutableSet = [[NSMutableSet alloc] initWithSet:self.markers]; 

    for (NSDictionary *markerData in json) { 

     GMSMarker *newMarker = [[GMSMarker alloc] init]; 
     // newMarker.appearAnimation = [markerData[@"id"] integerValue]; 

     newMarker.position = CLLocationCoordinate2DMake([markerData[@"latitud"] doubleValue], 
                 [markerData[@"longitude"] doubleValue]); 

     newMarker.title = markerData[@"name"]; 
     newMarker.snippet = markerData[@"adress"]; 

     // [mutableSet addObject:newMarker]; 
     newMarker.map=self.mapView; 

    } 
    self.markers =[mutableSet copy]; 
    [self drawMarkers]; 

} 

#pragma mark GMUClusterManagerDelegate 

- (void)clusterManager:(GMUClusterManager *)clusterManager didTapCluster:(id<GMUCluster>)cluster { 
    GMSCameraPosition *newCamera = 
    [GMSCameraPosition cameraWithTarget:cluster.position zoom:_mapView.camera.zoom + 1]; 
    GMSCameraUpdate *update = [GMSCameraUpdate setCamera:newCamera]; 
    [_mapView moveCamera:update]; 
} 

#pragma mark GMSMapViewDelegate 

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker { 
    POIItem *poiItem = marker.userData; 
    if (poiItem != nil) { 
     NSLog(@"Did tap marker for cluster item %@", poiItem.name); 
    } else { 
     NSLog(@"Did tap a normal marker"); 
    } 
    return NO; 
} 


#pragma mark Private 

// Randomly generates cluster items within some extent of the camera and adds them to the 
// cluster manager. 
- (void)generateClusterItems { 
    const double extent = 0.2; 
    for (int index = 1; index <= kClusterItemCount; ++index) { 
     double lat = kCameraLatitude + extent * [self randomScale]; 
     double lng = kCameraLongitude + extent * [self randomScale]; 
     NSString *name = [NSString stringWithFormat:@"Item %d", index]; 
     id<GMUClusterItem> item = 
     [[POIItem alloc] initWithPosition:CLLocationCoordinate2DMake(lat, lng) name:name]; 
     [_clusterManager addItem:item]; 
    } 
} 

// Returns a random value between -1.0 and 1.0. 
- (double)randomScale { 
    return (double)arc4random()/UINT32_MAX * 2.0 - 1.0; 
} 

答えて

0

試みは、それは、その後のクラスタを表示してはならない5または4に負荷

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:kCameraLatitude longitude:kCameraLongitude zoom:4]; 

ズームをしましたマーカー。ズームインすると、「本当の」マーカーが表示されます。

関連する問題