2011-09-18 44 views
0

我可以解析數據庫中的數據,但難於循環查看數據庫中的所有座標並將它們繪製在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]; 
    } 
    } 
} 

回答

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]; 
    } 
    } 
} 
+0

謝謝沃爾克,那就是我一直在尋找的.. ifelse聲明工作。 – Cocell

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; 
} 
+0

謝謝bigkm,但問題是迭代通過從數據庫座標的數組,然後添加註釋到地圖中。 – Cocell

+0

我將代碼移到委託方法中,現在我看不到我目前的位置... – Cocell