I'm trying to figure out a straightforward way to determine in Google Maps for iOS if a given GMSMarker is within the bounds of the visible map. There seems to be solutions for this in the Javascript API but other than perhaps doing some complex reasoning based on this post there doesn't seem to be a way.
Asked
Active
Viewed 7,434 times
5 Answers
17
A code example based on Andy's helpful response:
- (void)snapToMarkerIfItIsOutsideViewport:(GMSMarker *)m{
GMSVisibleRegion region = _mapView.projection.visibleRegion;
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithRegion:region];
if (![bounds containsCoordinate:m.position]){
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:m.position.latitude
longitude:m.position.longitude
zoom:_mapView.camera.zoom];
[self.mapView animateToCameraPosition: camera];
}
}

Dave Cole
- 2,446
- 2
- 20
- 26
-
Thanks a lot! it works perfectly! saves me a lot of time – youssman Dec 29 '15 at 19:31
-
What will it return if my marker is half inside the region and half is outside? Actually I want to check if my marker is fully inside the region or not. How can I check that? – Nimisha Patel Apr 04 '18 at 16:47
-
Well, strictly speaking, your marker is a point, so it doesn't have dimensions. If your aim is to make sure that the graphic image you've associated with your marker is fully visible, I recommend iterating on this conditional, zooming out until it's satisfied, then zooming out one level more from that. – Dave Cole Apr 04 '18 at 17:15
14
Retrieve the bounds of your viewport with GMSVisibleRegion and create a GMSCoordinateBounds with it. Call containsCoordinate
, passing in the marker's position. It should return true if the marker is within the viewport and false if not.

Andy
- 2,374
- 3
- 17
- 20
10
The swift 4 version of the answer. Returning a boolean if marker is within the screen region or not
func isMarkerWithinScreen(marker: GMSMarker) -> Bool {
let region = self.mapView.projection.visibleRegion()
let bounds = GMSCoordinateBounds(region: region)
return bounds.contains(marker.position)
}

Gabriel Wamunyu
- 734
- 9
- 25
1
I have written on method to find GMSMarker is in particular frame. Set your rectangle frame (x,y,maxX,maxY). You can set any frame from screen it tell find marker is in that frame or not..
- (BOOL)isGoogleMapMarkerVisible:(GMSMarker*)marker {
//Marker point
CGPoint markerpoint = [self.mapview.projection pointForCoordinate:marker.position];
//Maximum visible region from x and y axis
float x = 0.0;
float y = o.o;
float maxX = self.mapview.frame.size.width;
float maxY = self.mapview.frame.size.height;
//If marker point is on visible region return true else return false
if (markerpoint.x > x && markerpoint.y > y && markerpoint.x < maxX && markerpoint.y < maxY) {
return YES;
}
else {
return NO;
}
}

Shelly Pritchard
- 10,777
- 4
- 18
- 17
0
Hope this code may help to code hunters.
NSMutableArray *mutArrMarkers; //Have all markers added on Map
.
.
.
.
NSMutableArray *mutArrMarkersInPath = [NSMutableArray array];
[mutArrMarkers enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
GMSMarker *marker = obj;
if(GMSGeometryContainsLocation(currentCoordinates, pathToCheck, YES)){
[mutArrMarkersInPath addObject:marker];
}
}];

Mrug
- 4,963
- 2
- 31
- 53