2012-02-06 12 views
-1

JsonをURL経由で取得したデータを使用して、Mapviewでアノテーションを使用したいとします。 そうなるかもしれませんか?注釈 - NSArray - これを行う方法

NSMutableArray *annotations = [[NSMutableArray alloc]init]; 

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://example.com/LData.php"]]; 

//Data: Longitud/Latitud/Country;..... 

NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
NSLog(str); 

[foo removeAllObjects]; 

NSArray *foo2 =[str componentsSeparatedByString: @";"]; 
int i=0; 
for(i=0;i<[foo2 count]; i++){ 

    [foo insertObject:[foo2 objectAtIndex:i] atIndex:i]; 
     NSArray *foo3 =[foo2 componentsSeparatedByString: @"/"]; 

これまでのところは良い ¿私は、変数[i]はを導入するレイのよう?

 CLLocationCoordinate2D theCoordinate[i]; 
     theCoordinate1.latitude[i] = [foo3 objectAtIndex:1]);//Longitud 
     theCoordinate1.longitude[i] = [foo3 objectAtIndex:2]);//Latitud 

     MyAnnotation* myAnnotation[i]=[[MyAnnotation alloc] init]; 

    myAnnotation[i].coordinate=theCoordinate[i]; 
    myAnnotation[i][email protected]""+[foo3 objectAtIndex:3]; 
    myAnnotation[i][email protected]"in the city"; 

     [mapView addAnnotation:myAnnotation[i]]; 
     [annotations addObject:myAnnotation[i]]; 




} 

どのようにこの問題を解決できますか?

+1

問題があることを明確に説明してください。 – bneely

答えて

0

なぜ異なるCLLocationCoordinate2D変数を使用していますか?

theCoordinate1.latitude[i] = [foo3 objectAtIndex:1]);//Longitud 
theCoordinate1.longitude[i] = [foo3 objectAtIndex:2]);//Latitud 

、その後

myAnnotation[i].coordinate=theCoordinate[i]; 

私はあなたのコードを調整する必要があり、いくつかの問題は自分で解決できると思います。

+0

CLLocationCoordinate2Dを使用する理由異なる変数はありますか? 注釈を座標に追加するには – user1164990

+0

私が望むのは、経緯度と緯度の方向に関係者のデータを取得し、地図に注釈を付けることです。 – user1164990

+0

あなたは、私はそれを感謝行うための別の方法、感謝 – user1164990

0
NSMutableArray *annotations = [[NSMutableArray alloc]init]; 

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://example.com/LData.php"]]; 

//Data: Longitud/Latitud/Country;..... 

NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
NSLog(str); 

[foo removeAllObjects]; 

NSArray *foo2 =[str componentsSeparatedByString: @";"]; 
int i=0; 
for(i=0;i<[foo2 count]; i++){ 

    [foo insertObject:[foo2 objectAtIndex:i] atIndex:i]; 
     NSArray *foo3 =[foo2 componentsSeparatedByString: @"/"]; 


    float realLatitude = [[foo3 objectAtIndex:1] floatValue];//Longitud 
    float realLongitude = [[foo3 objectAtIndex:0] floatValue];//Latitud   

     MyAnnotation* myAnnotation = [[MyAnnotation alloc] init]; 

    CLLocationCoordinate2D theCoordinate; 
      theCoordinate.latitude = realLatitude; 
      theCoordinate.longitude = realLongitude; 


    myAnnotation.coordinate = theCoordinate; 
    myAnnotation.title = [foo3 objectAtIndex:3]; 
     //myAnnotation.subtitle = [note objectForKey:@"stationAddressKey"]; 

     [_mapView setDelegate:self]; 
     [_mapView addAnnotation:myAnnotation]; 
     [myAnnotation release]; 

} 




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

{ 
    if ([annotation isKindOfClass:[MyAnnotation class]]) 
    { 
     static NSString *reuseId = @"customAnn"; 

     MKAnnotationView *customAnnotationView = [aMapView dequeueReusableAnnotationViewWithIdentifier:reuseId]; 

     if (customAnnotationView == nil) 
     { 
      customAnnotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease]; 
      UIImage *pinImage = [UIImage imageNamed:@"pin-green.png"]; 
      [customAnnotationView setImage:pinImage]; 
      customAnnotationView.canShowCallout = YES; 
      UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      customAnnotationView.rightCalloutAccessoryView = rightButton; 
     } 

     // NSString *iconFilename = @""; 
     // MyAnnotation *myAnn = (MyAnnotation *)annotation; 
     // if ([myAnn.stationIdKey isEqualToString:@"BP"]) 
      iconFilename = @"bp-logo.png"; 
     // else 
     //  if ([myAnn.stationIdKey isEqualToString:@"Caltex"]) 
     //   iconFilename = @"caltex.png"; 
     //  else 
     //   if ([myAnn.stationIdKey isEqualToString:@"Shell"]) 
     //    iconFilename = @"shell.png"; 
     // UIImageView *leftIconView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:iconFilename]] autorelease]; 
     customAnnotationView.leftCalloutAccessoryView = leftIconView; 

     customAnnotationView.annotation = annotation; 

     return customAnnotationView; 
    } 

    return nil; 
} 



-(void)_mapView:(MKMapView *)_mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ if ([view.annotation isKindOfClass:[MyAnnotation class]]) 
{ 
    MyAnnotation *myAnn = (MyAnnotation *)view.annotation; 
    NSLog(@"callout button tapped for station id %@", myAnn.stationIdKey); 
} 
else 
{ 
    NSLog(@"callout button tapped for annotation %@", view.annotation); 
} } 

これは正しいですか?

関連する問題