我正在使用GMSCoordinateBounds
来获取可见区域中的标记列表。但我得到的所有标记都是绘制而不仅仅是可见标记。
我就是这样做的:
GMSVisibleRegion visibleRegion = [mapView_.projection visibleRegion];
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc]initWithRegion:visibleRegion];
GMSMarker *resultMarker = [[GMSMarker alloc]init];
for (int i=0; i<[markerArray count]; i++) //this has all the markers
{
resultMarker = [markerArray objectAtIndex:i];
if ([bounds containsCoordinate:resultMarker.position])
{
NSLog(@"User is present on screen");
[listTableArray addObject:resultMarker.title];
}
}
[listTableView reloadData];
答案 0 :(得分:0)
您的代码看起来很好。我很确定无论你的问题是什么,它都来自你发布的代码之外的其他地方。
另一个潜在的问题是,如果你的地图允许轮换,那么你的GMSVisibleRegion
对象就会发生各种迷失方向的事情。 (即farLeft
属性与西北点不对应)。我认为GMSCoordinateBounds
会考虑到这一点而不会被它绊倒。
话虽如此,您可以编写自己的方法来检查区域中是否包含标记的坐标。这是我写的(包括我自己的区域和标记“包装”):
-(BOOL)isMarker:(SGMarker*)m inVisibleRegion:(SGRegion*)region
{
CLLocationCoordinate2D upperLeftPosition = region.topLeft;
CLLocationCoordinate2D lowerRightPosition = region.bottomRight;
if (m.position.latitude > lowerRightPosition.latitude && m.position.latitude < upperLeftPosition.latitude &&
m.position.longitude < lowerRightPosition.longitude && m.position.longitude > upperLeftPosition.longitude) {
return YES;
}
return NO;
}
// In my region wrapper, this is how I make sure I have the north-east/south-west points
+(SGRegion*)regionWithGMSVisibleRegion:(GMSVisibleRegion)region
{
SGRegion* mapRegion = [[self alloc] init];
// Since the camera can rotate, first we need to find the upper left and lower right positions of the
// visible region, which may not correspond to the farLeft and nearRight points in GMSVisibleRegion.
double latitudes[] = {region.farLeft.latitude, region.farRight.latitude, region.nearLeft.latitude};
double longitudes[] = {region.nearRight.longitude, region.nearLeft.longitude, region.farLeft.longitude};
double highestLatitude = latitudes[0], lowestLatitude = latitudes[0];
double highestLongitude = longitudes[0], lowestLongitude = longitudes[0];
for (int i = 1; i < 3; i++) {
if (latitudes[i] >= highestLatitude)
highestLatitude = latitudes[i];
if (latitudes[i] < lowestLatitude)
lowestLatitude = latitudes[i];
if (longitudes[i] >= highestLongitude)
highestLongitude = longitudes[i];
if (longitudes[i] < lowestLongitude)
lowestLongitude = longitudes[i];
}
mapRegion.topLeft = CLLocationCoordinate2DMake(highestLatitude, lowestLongitude);
mapRegion.bottomRight = CLLocationCoordinate2DMake(lowestLatitude, highestLongitude);
return mapRegion;
}
因此,如果您使用这些方法,您应该能够绝对地告诉您的问题来自哪里(即,不是来自这里;))。