Marker clustering with google maps SDK for iOS?

后端 未结 4 1666
既然无缘
既然无缘 2020-11-29 22:07

I am using Google Maps SDK in my iOS app, and I need to group markers which are very close to each other - basically need to use marker clustering like its shown in the atta

4条回答
  •  死守一世寂寞
    2020-11-29 22:54

    i have an app handle this issue, below is the code

    1. loop all markers (nsdictionary) in an array

    2. use gmsmapview.projection to get CGPoint in order to find out whether the marker should group together

    3 i use 100 points to test and the response time is quite satisfied.

    4 the map will redraw if the zoom level difference is over 0.5;

      -(float)distance :(CGPoint)pointA point:(CGPoint) pointB{
    
            return sqrt( (pow((pointA.x - pointB.x),2) + pow((pointA.y-pointB.y),2)));
    
        }
    
    
    
    
        -(void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position{
    
            float currentZoomLevel = mapView.camera.zoom;
    
            if (fabs(currentZoomLevel- lastZoomLevel_)>0.5){
    
                lastZoomLevel_ = currentZoomLevel;
    
                markersGroupArray_ = [[NSMutableArray alloc] init];
    
                for (NSDictionary *photo in photoArray_){
    
                    float coordx = [[photo objectForKey:@"coordx"]floatValue];
                    float coordy = [[photo objectForKey:@"coordy"] floatValue];
    
                    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(coordx, coordy);
    
                    CGPoint currentPoint = [mapView.projection pointForCoordinate:coord];
    
                    if ([markersGroupArray_ count] == 0){
    
                        NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:photo, nil];
    
                        [markersGroupArray_ addObject:array];
    
                    }
                    else{
    
                        bool flag_groupadded = false;
    
                        int counter= 0;
                        for (NSMutableArray *array in markersGroupArray_){
    
                            for (NSDictionary *marker in array){
    
                                float mcoordx = [[marker objectForKey:@"coordx"]floatValue];
                                float mcoordy = [[marker objectForKey:@"coordy"]floatValue];
    
                                CLLocationCoordinate2D mcoord = CLLocationCoordinate2DMake(mcoordx, mcoordy);
                                CGPoint mpt = [mapView.projection pointForCoordinate:mcoord];
    
                                if ([self distance:mpt point:currentPoint] <30){
                                    flag_groupadded = YES;
                                    break;
                                }
    
    
                            }
                            if (flag_groupadded){
    
                                break;
                            }
                            counter++;
    
                        }
    
    
                        if (flag_groupadded){
    
                            if ([markersGroupArray_ count]>counter){
                                NSMutableArray *groupArray = [markersGroupArray_ objectAtIndex:counter];
                                [groupArray insertObject:photo atIndex:0];
                                [markersGroupArray_ replaceObjectAtIndex:counter withObject:groupArray];
                            }
                        }
                        else if (!flag_groupadded){
    
                            NSMutableArray * array = [[NSMutableArray alloc]initWithObjects:photo, nil];
                            [markersGroupArray_ addObject:array];
                        }
    
                    }
    
                } // for loop for photoArray
    
    
    
                // display group point
    
    
                [mapView clear];
    
                photoMarkers_ = [[NSMutableArray alloc] init];
    
                for (NSArray *array in markersGroupArray_){
    
                    NSLog(@"arry count %d",[array count]);
    
                    NSDictionary *item = [array objectAtIndex:0];
    
                    float coordx = [[item objectForKey:@"coordx"]floatValue];
                    float coordy = [[item objectForKey:@"coordy"] floatValue];
    
                    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(coordx, coordy);
    
                    GMSMarker *marker = [[GMSMarker alloc] init];
                    marker.position = coord;
                    marker.map = mapView;
    
                    [photoMarkers_ addObject:marker];
    
                    marker = nil;
    
    
                }
    
    
    
                NSLog(@"markers %@",photoMarkers_);
    
            } // zoomlevel diffference thersold
    
    
        }
    

提交回复
热议问题