2014-01-10 159 views
1

我正在使用GMSCoordinateBounds獲取可見區域中標記的列表。但是我得到的是所有繪製的標記,而不僅僅是可見的標記。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

第3行。在那裏劃掉多餘的alloc/init - 它永遠不會被讀取 –

+0

你確定你的標記數組沒有問題嗎?以及表:)代碼看起來不錯 –

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

所以,如果你使用這些方法來代替,你應該能夠完全告訴您的問題是來自(即不從這裏;))。

相關問題