Проблема зацикливания массива координат из базы данных - PullRequest
0 голосов
/ 18 сентября 2011

Я могу анализировать данные из базы данных, но с трудом просматривая все координаты в базе данных и нанося их на карту. Может кто-нибудь, пожалуйста, помогите.

-(void)scanDatabase{

  UIDevice *device = [UIDevice currentDevice];
  NSString *uid = [device uniqueIdentifier];
  NSString *myUrl = [NSString stringWithFormat:@"http://address.php?uid=%@",uid];
  NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: myUrl ]];    

  // to receive the returend value
  NSString *serverOutput =[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
  NSArray *components = [serverOutput componentsSeparatedByString:@"\n"];

  for (NSString *line in components) {
    NSArray *fields = [line componentsSeparatedByString:@"\t"];
    [eventPoints addObjectsFromArray:fields];

    int count = [fields count];
    for(int i=0;i < count; i++) {
      int myindex0 = i*count;
      int myindex1 = (i*count)+1;
      int myindex2 = (i*count)+2;

      NSString *mytitle = [eventPoints objectAtIndex:myindex0];
      NSNumber *myLat = [eventPoints objectAtIndex:myindex1];
      NSNumber *myLon = [eventPoints objectAtIndex:myindex2];

      CLLocationCoordinate2D loc;

      loc.latitude = myLat.doubleValue;
      loc.longitude = myLon.doubleValue;

      customAnnotation *event = [[customAnnotation alloc] initWithCoordinate:loc];
      event.title = mytitle;

      MKPinAnnotationView *newAnnotationPin = [[MKPinAnnotationView alloc] initWithAnnotation:event reuseIdentifier:@"simpleAnnotation"];
      newAnnotationPin.pinColor = MKPinAnnotationColorRed;

      [map addAnnotation:event];
    }
  }
}

Ответы [ 2 ]

1 голос
/ 18 сентября 2011

вам нужно использовать метод делегата для добавления вида аннотации

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

иначе он никогда не появится, смотрите здесь http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *newAnnotationPin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"simpleAnnotation"] autorelease];
        newAnnotationPin.pinColor = MKPinAnnotationColorRed;
    return newAnnotationPin;
}
0 голосов
/ 18 сентября 2011

Не совсем уверен, с какими проблемами вы сталкиваетесь, так как "испытываете трудности ..." не очень хорошо их описывает: -)

Глядя на ваш код, я хотел бы сделать следующие комментарии:

  • Внутренний цикл, кажется, не имеет никакого смысла.
  • Почему вы не обращаетесь к полям напрямую?
  • myLat и myLon должны быть ссылками на объекты NSString.

Может быть, что-то вроде этого работает лучше:

-(void)scanDatabase{

  UIDevice *device = [UIDevice currentDevice];
  NSString *uid = [device uniqueIdentifier];
  NSString *myUrl = [NSString stringWithFormat:@"http://address.php?uid=%@",uid];
  NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: myUrl ]];    

  // to receive the returend value
  NSString *serverOutput =[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
  NSArray *components = [serverOutput componentsSeparatedByString:@"\n"];

  for (NSString *line in components) {
    NSArray *fields = [line componentsSeparatedByString:@"\t"];
    [eventPoints addObjectsFromArray:fields];

    if ( [fields count] != 3 ) {
      // something is wrong/unexpected here, act appropriately
    }
    else {
      NSString *mytitle = [fields objectAtIndex:0];
      NSString *myLat = [fields objectAtIndex:1];
      NSString *myLon = [fields objectAtIndex:2];

      CLLocationCoordinate2D loc;

      loc.latitude = myLat.doubleValue;
      loc.longitude = myLon.doubleValue;

      customAnnotation *event = [[customAnnotation alloc] initWithCoordinate:loc];
      event.title = mytitle;

      MKPinAnnotationView *newAnnotationPin = [[MKPinAnnotationView alloc] initWithAnnotation:event reuseIdentifier:@"simpleAnnotation"];
      newAnnotationPin.pinColor = MKPinAnnotationColorRed;

      [map addAnnotation:event];
    }
  }
}
...