从数据库循环遍历坐标数组的问题

时间:2011-09-18 01:30:52

标签: iphone objective-c ios xcode android-mapview

我可以解析数据库中的数据,但很难循环遍历数据库中的所有坐标并将它们绘制在mapview上。有人可以帮忙。

-(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 个答案:

答案 0 :(得分:1)

您需要使用委托方法添加注释视图

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

答案 1 :(得分:0)

不确定你遇到了什么问题,因为“困难时期......”并没有很好地描述它们: - )

查看您的代码,我想发表以下评论:

  • 内循环似乎没有任何意义。
  • 为什么不直接访问这些字段?
  • 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];
    }
  }
}