2

Using MapKit , I can get the visible map area and then count the number of annotations inside it.However using the Google Maps I am not able to find any working example as such to refer.To begin with I know that GMSVisibleRegion can be used for this.But can't proceed anywhere with this ..Has anyone done this?Any Suggestions?

This is how I did it using Mapkit.How can I do this using Google Maps ?

   NSMutableArray *tempArray=[[NSMutableArray alloc]init];
    for (KPAnnotation *annotation in self.mapView.annotations) {
        if (MKMapRectContainsPoint(self.mapView.visibleMapRect, MKMapPointForCoordinate(annotation.coordinate)))
        {

            NSArray *myArray = [annotation.annotations allObjects];

            int iCount=[myArray count];
            for(int i=0;i<iCount;i++)
            {
                Annotation *a=[myArray objectAtIndex:i];

                [tempArray addObject:a.name];
            }
        }
    }
James Kingsbery
  • 7,298
  • 2
  • 38
  • 67
iCodes
  • 1,382
  • 3
  • 21
  • 47
  • In MapKit, you don't need to loop through the annotations like that. You can call the annotationsInMapRect: method and pass it self.mapView.visibleMapRect and it will return the set of visible annotations. –  Jan 09 '14 at 14:02
  • Hmmm....but right now I am stuck at implementing this using google maps – iCodes Jan 09 '14 at 14:04
  • For Google Maps, try creating a GMSCoordinateBounds object using the initWithRegion: method and pass it the GMSVisibleRegion. Then call containsCoordinate: for each marker. –  Jan 09 '14 at 14:07
  • Can you share some sample code please. – iCodes Jan 09 '14 at 14:55
  • How to do this for geojson files for loading polylines with cluster, any suggestions?https://stackoverflow.com/questions/49365001/how-to-show-cluster-for-mkpolyline-with-geojson-data-view-in-ios-swift – Anilkumar iOS - ReactNative Mar 19 '18 at 14:37

1 Answers1

2

From Anna's comments - this should do the trick

- (void)mapView:(GMSMapView *)mapView_ didChangeCameraPosition:(GMSCameraPosition *)position {
    [NSObject cancelPreviousPerformRequestsWithTarget:self
                                             selector:@selector(updateMapResults) object:nil];
    [self performSelector:@selector(updateMapResults) withObject:nil afterDelay:0.2];
}

- (void)updateMapResults {
    GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc]initWithRegion:mapView_.projection.visibleRegion];

    int count = 0;
    for (GMSMarker *marker in markers) {
        if([bounds containsCoordinate:marker.position]){
            count++; // update your label
        }
    }
}
johndpope
  • 5,035
  • 2
  • 41
  • 43