2012-01-19 53 views
-1

查看我需要顯示在MKMapKit..I多個annonation視圖使用此代碼..問題在MkMapKit

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib 

    [mapView setMapType:MKMapTypeStandard]; 
    [mapView setZoomEnabled:YES]; 
    [mapView setScrollEnabled:YES]; 
    DisplayMap *ann = [[DisplayMap alloc] init]; 
    //NSLog(@"xapp.arrplacename:%d",[xapp.arrplacename count]); 
    for (i=0;i<=[arrplacename count]-1;i++) 
    { 

     MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
     //arrplacename contains the address of the locations 
     NSString *straddress=[arrplacename objectAtIndex:i]; 
     NSString *[email protected]""; 
     NSString *strs=[straddress stringByAppendingString: strregion]; 
     // lbladdress.text=strs; 
     // NSString add=strs; 
     // here you define the url setting your address 
     NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",[strs stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; 

     NSLog(@"US: %@",urlString); 

     // then you get the response string here 
     NSString *locationString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease]; 
     NSLog(@"loaction strings%@",locationString); 
     // now you can create an array splitting the response values using the "," 
     //[listItems addObject:locationString]; 
     listItems = [locationString componentsSeparatedByString:@","]; 

     NSLog(@"A: %@",[xapp.listItems objectAtIndex:2]); 

     // define latitude and longitude variables 
     double latitude = 0.0; 
     double longitude = 0.0; 

     NSString *strlatitude; 
     NSString *strlongitude; 

     strlatitude=[xapp.listItems objectAtIndex:2]; 
     strlongitude=[xapp.listItems objectAtIndex:3]; 

     float stringFloat = [strlatitude floatValue]; 
     float string1Float = [strlongitude floatValue]; 

     // if your array has inside the sctructure you aspect (4 values as described in my previous post) and the first value is like "200" (OK result) 
     if([xapp.listItems count] >= 4 && [[xapp.listItems objectAtIndex:0] isEqualToString:@"200"]) 
     { 
      // not sure if you need the accurancy parameter 
      //accurancy = [[listItems objectAtIndex:1] intValue]; 

      // here you can get your values 

      // latitude = [[listItems objectAtIndex:2] doubleValue]; 
      //longitude = [[listItems objectAtIndex:3] doubleValue]; 

      latitude = stringFloat; 
      longitude = string1Float; 

     } 
     else { 
      NSLog(@"Error on map"); 
     } 
     region.center.latitude = latitude; 
     region.center.longitude = longitude; 
     NSLog(@"latitude:%g",region.center.latitude); 
     // region.center.latitude = 42.7157850; 
     // region.center.longitude = 27.56470180; 

     region.span.longitudeDelta = 0.01f; 
     region.span.latitudeDelta = 0.01f; 
     [mapView setRegion:region animated:YES]; 
     [mapView setDelegate:self]; 
     ann.title = [xapp.arrplacename objectAtIndex:i]; 
     ann.subtitle = [xapp.arrplacename objectAtIndex:i]; 
     ann.coordinate = region.center; 
     [mapView addAnnotation:ann]; 

    } 

} 

我有這個代碼的一些問題....所有coordnates不添加到listItems中array..I只能看到被atlast添加的對象....

這是我加在我的地圖顯示annonation代表..

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address{ 

    NSLog(@"ssssss"); 

    double latitude = 0, longitude = 0; 
    NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr]; 
    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL]; 
    if (result) { 
     NSScanner *scanner = [NSScanner scannerWithString:result]; 
     if ([scanner scanUpToString:@"\"lat\":" intoString:nil] && [scanner scanString:@"\"lat\":" intoString:nil]) { 
      [scanner scanDouble:&latitude]; 
      if ([scanner scanUpToString:@"\"lng\":" intoString:nil] && [scanner scanString:@"\"lng\":" intoString:nil]) { 
       [scanner scanDouble:&longitude]; 
      } 
     } 
    } 
    CLLocationCoordinate2D center; 
    center.latitude = latitude; 
    center.longitude = longitude; 

    return center; 
} 


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

    MKPinAnnotationView *pinView = nil; 
    NSLog(@"pinnview before release %d",[pinView retainCount]); 

    if (pinView !=nil) { 
     pinView =nil; 
     [pinView release]; 
    } 
    NSLog(@"pinnview after release %d",[pinView retainCount]); 

    // if it's the user location, just return nil. 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    if(annotation != mapView.userLocation) 
    { 

     static NSString *defaultPinID = @"your-pin"; 

     pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 

     if (counting < [mapView.annotations count]) 
     { 
      counting++; 

      pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease]; 


      /* for(DisplayMap* a in mapView.annotations) 
      { 
       if (annotation == a){ 
        pinView.image = 
        [UIImage imageWithContentsOfFile: 
        [[NSBundle mainBundle] pathForResource:a.image ofType:nil]]; 
       } 
      }*/ 
      pinView.centerOffset= CGPointMake(0,-10); 
      pinView.canShowCallout = YES; 


     } 

    } 

    // return pinView; 


} 

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views 
{ 
    NSLog(@"enter"); 
    [mapView selectAnnotation:[[mapView annotations] lastObject] animated:YES]; 
} 

代表AR Ë只調用一次eventhough我使用[mapView addAnnotation:ann];循環內...這也表明針只爲最後一個項目tatz陣列......我怎樣才能克服這種在加入????

+0

的NSString * straddress = [arrplacename objectAtIndex:ⅰ];這個代碼是由 – Deepesh

+0

對不起,我不能讓ú更換? – Icoder

+0

你好icoder看到這個代碼ann.title = [xapp.arrplacename objectAtIndex:ⅰ]; ann.subtitle = [xapp.arrplacename objectAtIndex:ⅰ];並使用這一行DisplayMap * ann = [[DisplayMap alloc] init]; \ – Deepesh

回答

0

你分配和初始化「安」只有一次前的for循環開始,你正在改變相同,一次又一次地將它變成地圖。這就是爲什麼你只能得到一個註釋。

移動這一行內部for循環和嘗試,

DisplayMap *ann = [[DisplayMap alloc] init]; 
+0

我試過達內循環。我找到一些線程阻塞[R我的應用程序.... – Icoder