在for循环中的MapView上显示多个引脚?

时间:2013-02-25 15:21:52

标签: xcode for-loop mkmapview pins

我正在开发一款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]];
}

}

1 个答案:

答案 0 :(得分:0)

我会让你弄清楚如何在objective-C中编写一个for循环,因为在网上已有数百个来源。您需要做的是决定如何将数据导入数组以进行循环。一旦你完成了这项工作,下面的代码是你的更有效的版本,带着这些想法

  • 您在viewDidLoad中执行此操作,但地图可能没有时间正确获取用户的位置。你现在最好添加所有注释,然后当receiving a new location的委托方法被调用时,然后循环遍历所有标签的[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]];
}