1

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

1 回答 1

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

因此,如果您改用这些方法,您应该能够绝对分辨出您的问题来自哪里(即,不是来自这里;))。

于 2014-01-21T02:44:54.703 回答