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

后端 未结 5 1414
一向
一向 2020-12-31 15:11

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 solution

5条回答
  •  攒了一身酷
    2020-12-31 15:52

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

提交回复
热议问题