Google Maps for iOS – 如何判断一个标记是否在屏幕的边界内?

我试图找出一个简单的方法来确定在iOS的谷歌地图,如果一个给定的GMSMarker是在可见的地图范围内。 在Javascript API中似乎有这样的解决scheme,但除了基于这篇文章做一些复杂的推理,似乎没有办法。

用GMSVisibleRegion检索视口的边界,并用它创build一个GMSCoordinateBounds 。 呼叫containsCoordinate ,通过标记的位置。 如果标记在视口内,它应该返回true,否则返回false。

基于Andy的有益回应的代码示例:

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

希望这个代码可以帮助代码猎人。

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

我已经写了一个方法来寻找GMSMarker是在特定的框架。 设置您的矩形框(x,y,maxX,maxY)。 你可以从屏幕上设置任何帧,告诉查找标记是否在该帧或不。

 - (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 = oo; 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; } }