2

你好朋友在我的应用程序中,我在 mapview 上显示了多个图钉。我有两个可变数组,第一个用于纬度,第二个用于经度,每个数组有 10 个值,因为我在我的方法中显示以下代码:

- (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 得到 nil 或 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];
4

2 回答 2

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;
}
于 2012-09-15T13:38:47.157 回答
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;
}
于 2012-09-15T13:34:07.930 回答