Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 579
Dave Cole Avatar asked May 05 '15 23:05

Dave Cole


People also ask

What does a green marker mean on Google Maps?

Green – Vegetation, darker shades mean more dense. Tan – Sand & scrub, lighter shades mean less vegetation. White – Void of any vegetation, sand dunes, mountain peaks. Light Gray – Population areas, cities, suburbs. Medium Gray – Military areas.

What do the markers mean on Google Maps?

A marker identifies a location on a map. By default, a marker uses a standard image. Markers can display custom images, in which case they are usually referred to as "icons." Markers and icons are objects of type Marker .


2 Answers

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)
}
like image 111
Gabriel Wamunyu Avatar answered Sep 28 '22 01:09

Gabriel Wamunyu


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];
    }
}
like image 31
Dave Cole Avatar answered Sep 28 '22 00:09

Dave Cole