2011-09-18 14 views
0

データベースからデータを解析できますが、データベース内のすべての座標をループしてマップビューにプロットするのは困難です。誰かが助けてくれますか?あなたは注釈ビューデータベースから座標の配列をループする際の問題

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

を追加するために、デリゲートメソッドを使用する必要が

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

フォルカさん、ありがとうございました。 – Cocell

1

それ以外の場合は、ここでは 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

関連する問題