重新加载地图时未加载带注释的引脚 [英] Annotated pins are not getting loaded during reloading of map

查看:79
本文介绍了重新加载地图时未加载带注释的引脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have used apple maps in my application for implementing maps.I used the following code for that.

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

My problem is that,It is showing the pins only when it is loading for the first time.

我已经写了这样的代码

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

请帮助我找出问题.我设置了断点.第二次只是到了

Please help me to find out the issue.I put breakpoints.Second time it is coming only upto

worldView.delegate = self;

推荐答案

只需放在 viewWillAppear 中,然后在 viewDidLoad

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    locationManager = [[CLLocationManager alloc]init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager startUpdatingLocation];
}

这篇关于重新加载地图时未加载带注释的引脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆