0

我在我的应用程序中使用了苹果地图来实现地图。我为此使用了以下代码。

我的问题是,它仅在第一次加载时才显示引脚。

我写了这样的代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Locate Engineer";

    [worldView setMapType:MKMapTypeStandard];

    [worldView setShowsUserLocation:YES];

    worldView.delegate = self;
}

    - (void)viewDidUnload
    {
        [super viewDidUnload];
    }
    -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {

        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

        if(self)
        {
            i=0;
            locationManager = [[CLLocationManager alloc]init];
            [locationManager setDelegate:self];
            [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

            [locationManager startUpdatingLocation];
        }
        return self;
    } 
    -(void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [locationManager startUpdatingLocation];
    }

    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        NSLog(@"%@",newLocation);
    }
    -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {     
        pinView = nil;
        if(annotation != mapView.userLocation)
        {
            NSString * defaultPinID = @"aa";

            //removed autorelease from the code below by coder

            pinView = (MKPinAnnotationView *) [worldView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];      
            if(pinView == nil)
            { 
                pinView .tag =i;
                pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;
                //pinView.image = [UIImage imageNamed:@"serviceengineer.png"];

                NSLog(@"pnID is %d",i);
                NSLog(@"value of i is %d",i);

                if ([[pinView.annotation title] isEqualToString:@"Levitton"]) {
                    pinView.pinColor = MKPinAnnotationColorRed;
                    pinView.canShowCallout = YES;
                    pinView.draggable = NO;
                } else {
                pinView.pinColor = MKPinAnnotationColorGreen;

                UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                [rightButton addTarget:self action:@selector(mapAction:) forControlEvents:UIControlEventTouchUpInside];
                rightButton.tag =i;
                pinView.rightCalloutAccessoryView = rightButton;
                // pinView.animatesDrop = YES;
                pinView.canShowCallout = YES;
                pinView.draggable = NO;
                }  
            }
            else {

                [worldView.userLocation setTitle:@"Title here"];        }
            i++;

        }
        return pinView;
    }

    -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
        ServiceEngineersList *engList = [ServiceEngineers findAll];

        MKCoordinateSpan spans;
        spans.latitudeDelta  = 0.5;
        spans.longitudeDelta = 0.5;

        ServiceEngineers *obj1= [engList objectAtIndex:0];
       // location1.latitude = 40.728224000000;
        //location1.longitude =-73.794852000000;
        location1.latitude = [obj1.LATITUDE doubleValue];
        location1.longitude =[obj1.LONGITUDE doubleValue];

        ad1 = [[annotationTest alloc] init];
        ad1.pinColor = MKPinAnnotationColorGreen;

        MKCoordinateRegion regions =
        MKCoordinateRegionMakeWithDistance(location1, 250,250);
        regions.span = spans; 
        [worldView setRegion:regions animated:YES];
        [worldView setZoomEnabled:YES];

        [worldView addAnnotation:ad1];


        ServiceEngineers *obj2= [engList objectAtIndex:1];
        ad2 = [[annotationTest2 alloc] init];
        ad2.pinColor = MKPinAnnotationColorGreen;
        //location2.latitude = 40.710841000000;
        //location2.longitude=-73.897769000000;  
        location2.latitude = [obj2.LATITUDE doubleValue];
        location2.longitude =[obj2.LONGITUDE doubleValue];
        MKCoordinateRegion region1 =
        MKCoordinateRegionMakeWithDistance(location2, 250, 250);
        region1.span = spans;

        [worldView setRegion:region1 animated:YES];
        [worldView setZoomEnabled:YES];
        [worldView addAnnotation:ad2];


        ServiceEngineers *obj3= [engList objectAtIndex:2];
        ad3 = [[annotationTest3 alloc] init];
        ad3.pinColor = MKPinAnnotationColorGreen;
        //location3.latitude = 40.726768000000;
        //location3.longitude=-73.634295000000;
        location3.latitude = [obj3.LATITUDE doubleValue];
        location3.longitude =[obj3.LONGITUDE doubleValue];


        MKCoordinateRegion region2 =
        MKCoordinateRegionMakeWithDistance(location3, 250, 250);
        region2.span = spans;

        [worldView setRegion:region2 animated:YES];
        [worldView setZoomEnabled:YES];
        [worldView addAnnotation:ad3];


        ServiceEngineers *obj4= [engList objectAtIndex:3];
        ad4 = [[annotationTest4 alloc] init];
        ad4.pinColor = MKPinAnnotationColorGreen;
        //location4.latitude = 40.702677000000;
        //location4.longitude=-73.788969000000;
        location4.latitude = [obj4.LATITUDE doubleValue];
        location4.longitude =[obj4.LONGITUDE doubleValue];

        MKCoordinateRegion region3 =
        MKCoordinateRegionMakeWithDistance(location4, 250, 250);
        region3.span = spans;

        [worldView setRegion:region3 animated:YES];
        [worldView setZoomEnabled:YES];
        [worldView addAnnotation:ad4];


        ad5 = [[annotationTest5 alloc] init];
        ad5.pinColor = MKPinAnnotationColorRed;
        location5.latitude = 40.7258;
        location5.longitude= -73.5147;
        MKCoordinateRegion region4 =
        MKCoordinateRegionMakeWithDistance(location5, 250, 250);
        region4.span = spans;
        [worldView setRegion:region4 animated:YES];
        [worldView setZoomEnabled:YES];
        [worldView addAnnotation:ad5];
    }
    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        NSLog(@"Couldnot find location: %@",error);
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
            return YES;
        return  NO;
    }

请帮我找出问题。我放了断点。第二次它只到了

worldView.delegate = self;
4

1 回答 1

2

只需放入 viewWillAppear 并删除 viewDidLoad

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    locationManager = [[CLLocationManager alloc]init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager startUpdatingLocation];
}
于 2013-04-30T13:47:43.043 回答