Google Maps for iOS - How can you tell if a marker is within the bounds of the screen?

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

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.


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)
}