0

私はiPhoneアプリを開発しています.MapViewにレーダーポイントを表示します.1000以上のポイントがあります. すべてのポイントを表示し、レーダー ポイントとユーザーの位置ポイント間の距離を計算する必要があります。表示するにはすべてのリージョン (1000 以上) を作成する必要がありますか? 誰かが私にアイデアを与えることができますか、これを作成するためにループを使用するにはどうすればよいですか? それ以外の場合は、1000 を超える領域オブジェクトを作成します。これが私のコードです:

- (void)viewDidLoad {


//user's location information

CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];


[super viewDidLoad];

[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];

//region1

MKCoordinateRegion region1 = { {0.0, 0.0 }, { 0.0, 0.0 } }; 

region1.center.latitude = 39.9828000 ;

region1.center.longitude =26.3033200 ;

region1.span.longitudeDelta = 1.90f;

region1.span.latitudeDelta = 0.00f;

[mapView setRegion:region1 animated:YES];


[mapView setDelegate:self];

DisplayMap *ann1 = [[DisplayMap alloc] init]; //display is my class to keep title,subtitle and coordinate information.


ann1.title = @" istanbul";

ann1.subtitle = @"esenler"; 

ann1.coordinate = region1.center; 

[mapView addAnnotation:ann1];

CLLocation *pinLocation1 = [[CLLocation alloc] initWithLatitude:region1.center.latitude longitude:region1.center.longitude];

CLLocationDistance distance1 = [pinLocation1 distanceFromLocation:userLocation];

if(distance1<20000){
    [lbl setText: [NSString stringWithFormat:@"Distance to point %4.2f m.",distance1]];
}



//region2

MKCoordinateRegion region2 = { {0.0, 0.0 }, { 0.0, 0.0 } }; 

region2.center.latitude = 39.9876200 ;

region2.center.longitude =26.3062700 ;

region2.span.longitudeDelta = 1.90f;

region2.span.latitudeDelta = 0.00f;

[mapView setRegion:region2 animated:YES]; 

[mapView setDelegate:self];

DisplayMap *ann2 = [[DisplayMap alloc] init];

ann2.title = @" istanbul";

ann2.subtitle = @"esenler";

ann2.coordinate = region2.center; 

[mapView addAnnotation:ann2];

//aradaki uzaklığı hesaplamak için mevcut yerin location ı cllocation class ı üzerinden hesaplanıyor.

CLLocation *pinLocation2 = [[CLLocation alloc] initWithLatitude:region2.center.latitude longitude:region2.center.longitude];

CLLocationDistance distance2 = [pinLocation2 distanceFromLocation:userLocation];

if(distance2<20000){
    [lbl setText: [NSString stringWithFormat:@"Distance to point %4.2f m.",distance2]];
}

}

4

1 に答える 1

0

Objective-C で for ループを作成する方法は、文字通り何百ものソースが Web 上に存在するため、あなたに任せます。あなたがしなければならないことは、データを配列に入れてループする方法を決定することです。次のコードがあなたのより効率的なバージョンであることがわかったら、これらの考えで

  • これを viewDidLoad で実行していますが、マップがユーザーの位置を適切に取得する時間がなかった可能性があります。すべての注釈を今すぐ追加してから、新しい場所を受け取るデリゲート メソッドが呼び出されたときに、[mapView annotations]すべてのラベルを設定する配列をループする方がよいでしょう。
  • そういえば。region1 と region2の両方lblが のテキストを設定します。それぞれが最後のものを上書きするだけです。1000 個のラベルが必要ですか、それともマップに最後に追加した地域のテキストだけが表示されると予想しますか?
  • なぜ各地域にズームするのですか?マップが 1000 個のポイントのそれぞれに 1 つずつ拡大されることを本当に期待していますか? すべてが完了すると、1000 番目の領域が表示され、他のすべてのポイントは無視されます。
  • デリゲートを 1000 回設定するのはなぜですか?

.

DisplayMap *ann1 = [[DisplayMap alloc] init];

ann1.title = @" istanbul";

ann1.subtitle = @"esenler";

ann1.coordinate = CLLocationCoordinate2DMake(39.9828000, 26.3033200);

[mapView addAnnotation:ann1];

CLLocation *pinLocation1 = [[CLLocation alloc] initWithLatitude:ann1.coordinate.latitude longitude:ann1.coordinate.longitude];

CLLocationDistance distance1 = [pinLocation1 distanceFromLocation:userLocation];

if(distance1<20000)
{
    [lbl setText: [NSString stringWithFormat:@"Distance to point %4.2f m.",distance1]];
}
于 2013-02-25T19:41:47.703 に答える