2012-02-29 19 views
1

私は、指定された座標のセットを含むマップのバウンディングボックスのサイズと位置を計算するアルゴリズムを持っています。 、注釈付きバウンディングボックス

enter image description here

...そして再び:それは完璧に動作しますが、それが生成する境界は、常に私は、多くの場合、右のバウンディングボックスのエッジ上にある座標に配置プッシュピンに対応していませんそれが許容できる結果にほとんどの時間を生成します。

enter image description here

そのプッシュピンを確実にするために、私はしばらくの間、それを超える混練しましたが、私は私のアルゴリズムを変更する方法を考えることができませんでした常に境界ボックス内にあります。私のアルゴリズムは以下の通りです。どんな提案も大歓迎です。 :)

MKMapPoint *points = malloc([coordinates count] * sizeof(MKMapPoint));  
MKMapPoint upperRightCorner; 
MKMapPoint lowerLeftCorner; 

for(int i = 0; i < [coordinates count]; i++) 
{ 
    CLLocation *location = [coordinates objectAtIndex:i]; 
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(herp.coordinate.latitude, herp.coordinate.longitude); 

    MKMapPoint point = MKMapPointForCoordinate(coordinate); 
    points[i] = point; 

    if (i == 0) { 
     upperRightCorner = point; 
     lowerLeftCorner = point; 
    } 
    else { 
     if (point.x > upperRightCorner.x) upperRightCorner.x = point.x; 
     if (point.y > upperRightCorner.y) upperRightCorner.y = point.y; 
     if (point.x < lowerLeftCorner.x) lowerLeftCorner.x = point.x; 
     if (point.y < lowerLeftCorner.y) lowerLeftCorner.y = point.y; 
    }  
} 

MKMapRect boundingBox = MKMapRectMake(lowerLeftCorner.x, lowerLeftCorner.y, 
           upperRightCorner.x - lowerLeftCorner.x, 
           upperRightCorner.y - lowerLeftCorner.y); 
+0

...トップにパディングを少し追加しますか?それは最も簡単な方法です:) – Ryan

+0

あなたの質問への答えではありませんが、これはLatLngBoundsのextend-methodを使用すると簡単に行うことができます –

答えて

3

私はそれが遅れるかもしれないと思うが、ここではそれは私の作品解決策だ、あなたと非常によく似て、私は最後にパディングを追加することになりました。

- (MKCoordinateRegion)boundingBoxForAnnotations:(NSArray *)annotations { 

CLLocationCoordinate2D topLeftCoord; 
topLeftCoord.latitude = -90; 
topLeftCoord.longitude = 180; 

CLLocationCoordinate2D bottomRightCoord; 
bottomRightCoord.latitude = 90; 
bottomRightCoord.longitude = -180; 

for (id<MKAnnotation> annotation in annotations) { 

    topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); 
    topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); 
    bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); 
    bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); 
} 

MKCoordinateRegion region; 
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5; 
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5; 
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; 

region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; 

return region; 

}