0

I am developing an iphone app,ı show radar points on MapView.There are over 1000 points. I have to show all points and calculate distances between radar points and user location points. I have to create all regions (over 1000) to show? Can anyone give me an idea, how can I use for loop to make this? otherwise I will create over 1000 region objects. Here is my code:

- (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]];
}

}

Craig
  • 8,093
  • 8
  • 42
  • 74

1 Answers1

0

I'll let you work out how to write a for-loop in objective-C because there are literally hundreds of sources for that on the web already. What you need to do is decide how you are going to get your data into an array to loop through. Once you've worked that out the following code is a more efficient version of yours, with these thoughts

  • You're doing this in viewDidLoad but the map may not have had time to get the user's location properly. You'd be better off adding all the annotations now and then when the delegate method for receiving a new location gets called you then loop through the [mapView annotations] array setting all the labels.
  • Speaking of which. Both region1 and region2 set lbl's text. Each one just overwrites the last. Do you expect to have 1000 labels or do you expect to just see the text for the last region you added to the map?
  • Why do you zoom to each region? Do you really expect the map to zoom in to each of the 1000 points one-by-one? When it's all done you'll be left looking at the 1000th region and ignoring all the other points
  • Why would you set the delegate 1000 times?

.

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]];
}
Craig
  • 8,093
  • 8
  • 42
  • 74
  • thx for your answer Craig.The code will reply 1000 times.I zoom because annotations is near of them.I have over 1000 list of points.The label is not important,just ı added to see the distance.Yes you are right,adding 1000times delegate is unnecessary.Thx again for your help.I think there is no short-way to make this.I will add all points one-by-one. – KenanYildirim Feb 26 '13 at 07:17
  • If you have 1000 points of data, you will need to add 1000 annotations, the trick is to avoid writing it out 1000 times. If you put the data into a database, or even flat a file, you could write something that took each line, got the data and added an annotation for it. There really is no point in zooming to 1000 different locations, the map will only stop on the last one and will just be a crazy blur until then. – Craig Feb 26 '13 at 08:00
  • I understand,I wrote codes for all pins,but now users have to wait about 30 seconds when they run the program.(to see all pins on the map,the pins fall down like rain:) )Do you have an idea,how can ı decrease the starting program time? – KenanYildirim Feb 26 '13 at 14:14
  • You should ask another question in a separate page rather than in the comments of this question. Or better yet search for the answer first. The solution is probably to remove the animation (http://stackoverflow.com/questions/9610796/how-to-dissmiss-the-add-annotation-animation-of-a-mkannotation) or to not show 1000 markers at the same time, I doubt users will be able to make any sense of that anyway. – Craig Feb 26 '13 at 19:41