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.
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.
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 .
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)
}
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];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With