2017-01-10 2 views
0

Ich entwickle eine App mit Google Map iOS SDK (Objective C).Wie füttern Sie vorhandene Marker in einen Marker-Cluster im GoogleMap iOS SDK?

bisher getan:

  • Loaded Marker vom Server in der Karte anzeigen
  • Zufall Cluster nur generieren.

Wenn ich das Projekt ausführen, sieht es so aus.

Screen shot of map view

Es zeigt beide Marker und Cluster sowohl in der Kartenansicht auf Zoomstufe 10. Aber ich wollte Cluster zeigen zuerst, dann, wenn ich in gezoomt es die realen Markierungen zeigen sollte. Nicht die zufällig generierten Markierungen, die ich erstellt habe, weil ich keine Möglichkeit kenne, die Cluster in der Karte anzuzeigen. Hier

ist der vollständige Code mit gefälschten URL-Link:

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

Antwort

0

Versuch im Hinblick auf Wechsel tat Last

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

der Zoom auf eine 5 oder 4. Es dann die Cluster zeigen sollte und nicht die Marker. Wenn Sie hineinzoomen, werden die "echten" Markierungen angezeigt.

Verwandte Themen