2012-02-06 79 views
-1

我想在Mapview中使用通過URL訪問數據的Json進行註釋。 可能嗎?註釋 - 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 2012-02-06 10:04:28

回答

0

爲什麼你使用不同的CLLocationCoordinate2D變量?

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

然後

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

我認爲你需要調整你的代碼,有些問題可以自行解決。

+0

爲什麼你使用CLLocationCoordinate2D不同的變量? 要將註釋添加到其座標 – user1164990 2012-02-06 10:25:20

+0

我想要的是獲取有關經緯度direfentes的各方數據,並在地圖上添加註釋。 – user1164990 2012-02-06 10:25:36

+0

如果你知道另一種方式我會很感激,謝謝 – user1164990 2012-02-06 10:26:44

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

這是正確的嗎?