0

我正在开发一个 iphone 应用程序,我在 MapView 上显示雷达点。有超过 1000 个点。我必须显示所有点并计算雷达点和用户位置点之间的距离。我必须创建所有区域(超过 1000 个)才能显示?谁能给我一个想法,我该如何使用 for 循环来做到这一点?否则我将创建超过 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 循环,因为网络上已经有数百个源代码。您需要做的是决定如何将数据放入数组中进行循环。一旦你解决了这个问题,下面的代码就是你的一个更有效的版本,有了这些想法

  • 您在 viewDidLoad 中执行此操作,但地图可能没有时间正确获取用户的位置。您最好不时添加所有注释,然后在调用用于接收新位置的委托方法时,然后循环遍历[mapView annotations]设置所有标签的数组。
  • 说到这。region1 和 region2 都设置了lbl文本。每个都只是覆盖最后一个。您希望有 1000 个标签,还是希望只看到您添加到地图的最后一个区域的文本?
  • 为什么要放大每个区域?您真的希望地图能够一一放大到 1000 个点中的每一个吗?完成后,您将只看到第 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 回答