2012-01-25 15 views
0

私はマップビューに基づいてアプリをやっています。私は場所(場所)を取得し、それを座標に変換しています。これを使用して、指定した場所の正確な場所を拡大しています。mapviewをズームする

しかし、私が直面している問題は、 街頭名や市区町村名や州名や国名を指定する場合、ズームレベルは常に同じです。ズームレベルは常に通りのものと同じです。 国名を指定するだけの場合、ズームレベルは国レベルで、国の任意のランダムな通りにズームしないでください。

ズームで使用したコードは次のとおりです。

-(void)zoomToFitMapAnnotations:(MKMapView*)mapViews 
{ 
if([mapViews.annotations count] == 0) 
    return; 

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

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

for(SJAddressAnnotation* annotation in mapViews.annotations) 
{ 
    topLeftCoordinate.longitude = fmin(topLeftCoordinate.longitude, annotation.coordinate.longitude); 
    topLeftCoordinate.latitude = fmax(topLeftCoordinate.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 = topLeftCoordinate.latitude - (topLeftCoordinate.latitude - bottomRightCoord.latitude) * 0.5; 
region.center.longitude = topLeftCoordinate.longitude + (bottomRightCoord.longitude - topLeftCoordinate.longitude) * 0.5; 
region.span.latitudeDelta = fabs(topLeftCoordinate.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides 
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoordinate.longitude) * 1.1; // Add a little extra space on the sides 

region = [mapView regionThatFits:region]; 
[mapView setRegion:region animated:YES]; 
} 

スパンのレベルを正しく変更しようとしましたが、この場合、ヘルプが必要です。

+1

最終領域はannotation.coordinateによってのみ決定されます。注釈が都市、州または国であるかどうかについての情報は含まれていません。 – chatur

+0

市/国に基づいてどのように座標を設定できますか? –

+1

まず、注釈の座標は単一の位置の座標にすぎません。注釈を使って表現しようとしているもののサイズに関する情報を含むことはできません。サイズ情報を保存する場合は、注釈で別のパラメータを使用する必要があります。第二に、国、都市または通りの標準サイズはありません。たとえば、ロシアやモナコのような国々を考えてみましょう。どこからアノテーションを取得していますか? Webサーバーや何を介して?またはあなたによって定義されていますか? – chatur

答えて

1

前に述べたように、1つの注釈に問題があります。 Google検索から場所を取得する場合は、それぞれの場所でズームの概算レベルを表示します(国/都市の種類によって異なります)。あなたのブラウザにhttp://maps.google.com/maps/geo?q=asia&output=jsonと入力してみてください。 asiaをchinaまたはnewyorkなどに置き換えることで、さまざまなエントリを試すことができます。ブラウザの応答のAccuracy属性を見てください。大陸で0、国で1など

自分で注釈を作成した場合は、zoomlevelに関連するパラメータを添付することができます。

@interface AddressAnnotation : NSObject<MKAnnotation> { 
double zoomLevel; 
} 
@property(readwrite,nonatomic) double zoomLevel ; 
@end 




@implementation AddressAnnotation 
    @synthesize zoomLevel; 
    -(void)setZoomLevel (double) parameter 
    { 
    self.zoomLevel = parameter; 
    } 
@end 

、最終的にANNOTを想定した注釈は座標とannotation.zoomLevelとしてスパンを設定すると、あなたがこの注釈設定された領域の中央に表示しているとき、あなたの注釈は

その後、
[annot setZoomLevel: .1]; //instead of .1 you can set different values 

です。

MKCoordinateRegion region; 
region.center.latitude = annotation.coordinate.latitude; 
region.center.longitude = annotation.coordinate.longitude; 
region.span.latitudeDelta = annotation.zoomLevel; 
region.span.longitudeDelta = annotation.zoomLevel; 
関連する問題