我可以解析数据库中的数据,但是很难遍历数据库中的所有坐标并将其绘制在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];
    }
  }
}

最佳答案

不太确定您遇到了什么问题,因为“遇到困难...”不能很好地描述它们:-)

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


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

关于iphone - 问题遍历数据库中的坐标数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7458941/

10-14 20:29
查看更多