您的代碼看起來不錯。我很確定你的問題是什麼,它來自你發佈的代碼以外的地方。
另一個潛在的問題是,如果你的地圖允許旋轉,所有的迷失方向都會發生在你的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;
}
所以,如果你使用這些方法來代替,你應該能夠完全告訴您的問題是來自(即不從這裏;))。
第3行。在那裏劃掉多餘的alloc/init - 它永遠不會被讀取 –
你確定你的標記數組沒有問題嗎?以及表:)代碼看起來不錯 –
在調用時投影是否正確? –