2012-09-15 41 views
2

你好朋友在我的應用程序中,我在mapview上顯示多個針腳。我有兩個mutablearray第一緯度和第二經度每個陣列有10個值,因爲我示出在我的方法波紋管代碼:在MKMap上顯示多個針腳iPhone

- (void)showAddressAllBusiness { 
    annotationsarray=[[NSMutableArray alloc] init]; 

     NSLog(@"%d",[listOfLatitude count]); 
    NSLog(@"%d",[listOfLongitude count]); 
    NSLog(@"%d",[listOfPlaceName count]); 

    for (int i =0;i < [listOfLatitude count]; i++) { 
     MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
     region.center.latitude = [[listOfLatitude objectAtIndex:i] doubleValue]; 
     region.center.longitude = [[listOfLongitude objectAtIndex:i] doubleValue]; 
     region.span.longitudeDelta = 0.1f; 
     region.span.latitudeDelta = 0.1f; 
     [self.mapViewCityGuide setRegion:region animated:YES]; 
     [self.mapViewCityGuide setZoomEnabled:YES]; 
     addAnnotation = [[AddressAnnotation alloc]init]; 

     addAnnotation.mTitle = [NSString stringWithFormat:@"%@",[listOfPlaceName objectAtIndex:i]]; 
     addAnnotation.coordinate = region.center; 
     [self.mapViewCityGuide addAnnotation:addAnnotation]; 
     [annotationsarray addObject:addAnnotation]; 


    } 
    NSLog(@"%d",[self.mapViewCityGuide.annotations count]); 
    [self centerMap]; 
} 

當我使用的緯度和經度數組值,其是從web服務出現然後 此代碼不能正常工作,我正在調試代碼,然後mapViewCityGuide變爲零或0x0。

如果我使用硬編碼緯度和經度值作爲數組中提到波紋管和調用這個方法那麼它會在地圖上顯示的所有引腳我怎樣才能解決這個問題

listOfPlaceName = [[NSMutableArray alloc] initWithObjects:@"test",@"test",@"test",@"pithempur",@"test",@"test",@"test",@"test", nil]; 

    listOfLatitude = [[NSMutableArray alloc] initWithObjects:@"22.71957",@"23.17930",@"22.96123",@"22.60987",@"22.45260",@"22.55244",@"22.60000",@"22.68330", nil]; 

    listOfLongitude = [[NSMutableArray alloc] initWithObjects:@"75.85773",@"75.78491",@"76.05141",@"75.69644",@"75.94197",@"75.75653",@"75.30000",@"75.61670", nil]; 


    [self showAddressAllBusiness]; 

回答

1

* 請嘗試驗證碼: - *

-(void)viewDidLoad 
{ 

    CLLocation *userLoc = mapView.userLocation.location; 
    CLLocationCoordinate2D userCoordinate = userLoc.coordinate; 

    NSLog(@"user latitude = %f",userCoordinate.latitude); 
    NSLog(@"user longitude = %f",userCoordinate.longitude); 

    mapView.delegate=self; 

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

    for (int i = 0; i<[DataArr count]; i++) 
    { 
     CLLocationCoordinate2D theCoordinate; 

     double a = [[[DataArr objectAtIndex:i] valueForKey:@"lat"] doubleValue]; 
     double b =[[[DataArr objectAtIndex:i] valueForKey:@"lon"] doubleValue]; 

     theCoordinate.latitude = a; 
     theCoordinate.longitude =b; 

     myAnnotation=[[MyAnnotation alloc] init]; 
     NSString *str=[NSString stringWithFormat:@"%d",i]; 
     [myAnnotation indexOfAccessibilityElement:str]; 

     myAnnotation.coordinate=theCoordinate; 
     myAnnotation.title=[[DataArr objectAtIndex:i] valueForKey:@"name"]; 
     myAnnotation.ID_map=i; 
     [annotations addObject:myAnnotation]; 
    } 

    for (int i =0; i<[annotations count]; i++) 
    { 
     [mapView addAnnotation:[annotations objectAtIndex:i]]; 
    } 

    // Walk the list of overlays and annotations and create a MKMapRect that 
    // bounds all of them and store it into flyTo. 
    MKMapRect flyTo = MKMapRectNull; 
    for (id <MKAnnotation> annotation in annotations) 
    { 
     NSLog(@"fly to on"); 
     MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); 
     MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0); 
     if (MKMapRectIsNull(flyTo)) 
     { 
      flyTo = pointRect; 
     } else 
     { 
      flyTo = MKMapRectUnion(flyTo, pointRect); 
      //NSLog(@"else-%@",annotationPoint.x); 
     } 
    } 

    // Position the map so that all overlays and annotations are visible on screen. 
    mapView.visibleMapRect = flyTo; 

} 

#pragma mark MKMapViewDelegate 

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 

    NSLog(@"didSelectAnnotationView"); 
} 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    NSLog(@"welcome into the map view annotation"); 

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

    // try to dequeue an existing pin view first 
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
    MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc] 
            initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease]; 
    pinView.animatesDrop=YES; 
    pinView.canShowCallout=YES; 
    pinView.pinColor=MKPinAnnotationColorPurple; 


    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
    [rightButton addTarget:self 
        action:@selector(showDetails:) 
      forControlEvents:UIControlEventTouchUpInside]; 
    pinView.rightCalloutAccessoryView = rightButton; 



    UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile.png"]]; 
    pinView.leftCalloutAccessoryView = profileIconView; 
    [profileIconView release]; 


    return pinView; 
} 
2
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    mapView.delegate=self; 




    [mapView removeAnnotations:mapView.annotations]; 




    NSMutableArray* annotations=[[NSMutableArray alloc] init]; 
    for (int j=0; j<10; j++) 
    { 

     CLLocationCoordinate2D theCoordinate1; 

     theCoordinate1.latitude = [[NSString stringWithFormat:@"%i",10+j]doubleValue]; 

     theCoordinate1.longitude = [[NSString stringWithFormat:@"%i",72+j]doubleValue]; 

     //my annotation is mkannotation class 

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

     myAnnotation1.coordinate=theCoordinate1; 

     myAnnotation1.title=[NSString stringWithFormat:@"locatiion %i",j]; 



     [mapView addAnnotation:myAnnotation1]; 

     [annotations addObject:myAnnotation1]; 


    } 

    NSLog(@"%d",[annotations count]); 

    MKMapRect flyTo = MKMapRectNull; 

    for (id <MKAnnotation> annotation in annotations) { 
     NSLog(@"fly to on"); 
     MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); 
     MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0); 
     if (MKMapRectIsNull(flyTo)) { 
      flyTo = pointRect; 
     } else { 
      flyTo = MKMapRectUnion(flyTo, pointRect); 

     } 
    } 


    mapView.visibleMapRect = flyTo; 


} 

#pragma mark MKMapViewDelegate 
/* 
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
{ 
return [kml viewForOverlay:overlay]; 
} 
*/ 
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    NSLog(@"welcome into the map view annotation"); 

    if ([annotation isKindOfClass:[MyAnnotation class]]) 
    { 
     static NSString *AnnotationIdentifier = @"AnnotationIdentifier"; 
     MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc] 
             initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease]; 
     if (!pinView) 
     { 
      pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease]; 
      pinView.canShowCallout = YES; 
      pinView.animatesDrop = YES; 
     } 
     else 
     { 
      pinView.annotation = annotation; 
      pinView.tag=((MyAnnotation *)annotation).tag; 
      pinView.canShowCallout = YES; 
      pinView.animatesDrop = NO; 
     } 

     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     rightButton.frame=CGRectMake(0, 0, 14, 16); 
     [rightButton setBackgroundImage:[UIImage imageNamed:@"arrow_s5.png"] forState:UIControlStateNormal]; 

     [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
     rightButton.tag=((MyAnnotation *)annotation).tag; 
     [rightButton addTarget:self 
         action:@selector(showDetails:) 
       forControlEvents:UIControlEventTouchUpInside]; 
     pinView.rightCalloutAccessoryView = rightButton; 


     return pinView; 
    } 

    return nil; 
}