没有按钮就没有添加iOS mapkit注释

时间:2014-07-07 22:44:16

标签: ios annotations mapkit

所以,我有一份纬度和经度清单,我希望在地图上加注。

下面的第一个方法是从另一个类的方法调用,该方法从单击按钮开始。现在我想在每次调用此方法时添加注释。但是,它不会向地图添加任何内容。也没有错误。

browse.h

@interface browsePage : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UIActionSheetDelegate>




@property (strong, nonatomic) IBOutlet MKMapView *mapView;

-(void) SwampsData;

- (IBAction)goToAnotherClass:(id)sender;
- (IBAction)testAnnotaions:(id)sender;

@end

browse.m

- (IBAction)goToAnotherClass:(id)sender {

    AlachuaCountySwamps * obje = [[AlachuaCountySwamps  alloc]init];

    [obje startGetting];

}



    - (IBAction)testAnnotaions:(id)sender {

    NSLog(@"\n test annotation button");

    NSMutableArray *LocationName = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"AlachuaCountySwamps_Names"]];
    NSMutableArray *Lat = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"AlachuaCountySwamps_Latitude"]];
    NSMutableArray *Lon = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"AlachuaCountySwamps_Longitude"]];


    for (int i=0; i<[LocationName count]; i++) {
        NSLog(@"%@ :                       Lat Long:  %@ , %@",[LocationName objectAtIndex:i],[Lat objectAtIndex:i],[Lon objectAtIndex:i]);


        currentLatitude = [[Lat objectAtIndex:i] doubleValue];
        currentLongitude = [[Lon objectAtIndex:i] doubleValue];
        currentSwamp = [LocationName objectAtIndex:i];


        /// custom location coordinate
        CLLocationCoordinate2D userLocationNew2 = CLLocationCoordinate2DMake(currentLatitude, currentLongitude);

        // use that cusom coordinate
        MKCoordinateRegion region2 = MKCoordinateRegionMakeWithDistance(userLocationNew2, 20000, 20000);
        [self.mapView setRegion:[self.mapView regionThatFits:region2] animated:YES];

        // Add an annotation

        MKPointAnnotation *point2 = [[MKPointAnnotation alloc] init];
        point2.coordinate = userLocationNew2;
        point2.title = currentSwamp;
        point2.subtitle = @"Swamp";

        [self.mapView addAnnotation:point2];



    }


}






-(void) SwampsData {


    // to be used data....

    NSLog(@"\n\nDoing alachua----SwampsData method now");



    NSMutableArray *LocationName = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"AlachuaCountySwamps_Names"]];
    NSMutableArray *Lat = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"AlachuaCountySwamps_Latitude"]];
    NSMutableArray *Lon = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"AlachuaCountySwamps_Longitude"]];
    NSString *TotalSwamps = [[NSUserDefaults standardUserDefaults] objectForKey:@"NumberOfSwamps"];



        NSLog(@"%@ :  Lat Long:  %@ , %@",[LocationName lastObject],[Lat lastObject],[Lon lastObject]);

    currentLatitude = [[Lat lastObject] doubleValue ];
    currentLongitude = [[Lon lastObject] doubleValue ];
    currentSwamp = [LocationName lastObject];

    double valuesRetrievedYet = [LocationName count];



    if (valuesRetrievedYet == [TotalSwamps doubleValue]) {

        NSLog(@" \n\n Adding the points on map...inside if loop");

     //   [self performSelectorInBackground:@selector(testAnnotaions:) withObject:nil];
            [self testAnnotaions:nil];

    }

}

AlachuaCountySwamps.h

@interface AlachuaCountySwamps : UIViewController


-(void) startGetting ;

@end

AlachuaCountySwamps.m

-(void) startGetting {


NSLog(@"Start startGetting the swamps..... ");

NSLog(@"Clearing the array");
  //  [Names removeAllObjects];


Names= [[NSMutableArray alloc] init];
Latitude= [[NSMutableArray alloc] init];
Longitude= [[NSMutableArray alloc] init];




PFQuery *query = [PFQuery queryWithClassName:@"AlachuaCountySwamps"];

[query whereKey:@"County" equalTo:@"Alachua"];

NSInteger countTotal= [query countObjects];
NSString *totalVals = [NSString stringWithFormat: @"%d", (int)countTotal];
[[NSUserDefaults standardUserDefaults] setObject:totalVals forKey:@"NumberOfSwamps"];



[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    if (!error) {
        for (PFObject *obj in objects) {

            NSString *LastName = [obj objectForKey:@"Name"] ;
            NSString *Lat = [obj objectForKey:@"Latitude"] ;
            NSString *Lon = [obj objectForKey:@"Longitude"] ;



            // add values to array
            [Names addObject:LastName];
            [Latitude addObject:Lat];
            [Longitude addObject:Lon];


                [self getResults];

        }

    } // if end...


    else{
    NSLog(@"Error: %@ %@", error, [error userInfo]);
    }

}];

NSLog(@"Entering");

}

-(void) getResults {

   // storing the data in the NSUserDefault


NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];


[[NSUserDefaults standardUserDefaults] setObject:Names forKey:@"AlachuaCountySwamps_Names"];
[[NSUserDefaults standardUserDefaults] setObject:Latitude forKey:@"AlachuaCountySwamps_Latitude"];
[[NSUserDefaults standardUserDefaults] setObject:Longitude forKey:@"AlachuaCountySwamps_Longitude"];


[standardUserDefaults synchronize];


// go to the browsePage class in alachuaSwampData method

browsePage * obje = [[browsePage  alloc]init];
[obje SwampsData];

}

输出:

Doing alachua----SwampsData method now
 Buck Bay :                       Lat Long:  29.724151 , -82.327413

Doing alachua----SwampsData method now
 Gum Root Swamp :                       Lat Long:  29.695778 , -82.2332541

Doing alachua----SwampsData method now
 Long Pond :                       Lat Long:  29.5497615 , -82.267022

Doing alachua----SwampsData method now
Mill Pond :                       Lat Long:  29.5176473 , -82.2721187


Doing alachua----SwampsData method now
Morans Prairie :                       Lat Long:  29.6859248 , -82.1125949

Doing alachua----SwampsData method now
Mosquito Bay :                       Lat Long:  29.5529874 , -82.1806915


Doing alachua----SwampsData method now
Mud Swamp :                       Lat Long:  29.8632273 , -82.3151139


Doing alachua----SwampsData method now
Orange Creek Camp Lake :                       Lat Long:  29.6387325 , -82.0824958


Doing alachua----SwampsData method now
Potato Patch Bay :                       Lat Long:  29.7165329 , -82.3395955


Doing alachua----SwampsData method now
River Styx :                       Lat Long:  29.5181366 , -82.2210298


Doing alachua----SwampsData method now
 Saluda Swamp :                       Lat Long:  29.7390842 , -82.1283104


Doing alachua----SwampsData method now
Sanchez Prairie :                       Lat Long:  29.7496668 , -82.456391


Doing alachua----SwampsData method now
Stanley Prairie :                       Lat Long:  29.6184384 , -82.0670156


 Adding the points on map...inside if loop

test annotation button
Buck Bay :                       Lat Long:  29.724151 , -82.327413
Gum Root Swamp :                       Lat Long:  29.695778 , -82.2332541
Long Pond :                       Lat Long:  29.5497615 , -82.267022
Mill Pond :                       Lat Long:  29.5176473 , -82.2721187
Morans Prairie :                       Lat Long:  29.6859248 , -82.1125949
Mosquito Bay :                       Lat Long:  29.5529874 , -82.1806915
Mud Swamp :                       Lat Long:  29.8632273 , -82.3151139
Orange Creek Camp Lake :                       Lat Long:  29.6387325 , -82.0824958
 Potato Patch Bay :                       Lat Long:  29.7165329 , -82.3395955
 River Styx :                       Lat Long:  29.5181366 , -82.2210298
 Saluda Swamp :                       Lat Long:  29.7390842 , -82.1283104
 Sanchez Prairie :                       Lat Long:  29.7496668 , -82.456391
 Stanley Prairie :                       Lat Long:  29.6184384 , -82.0670156

0 个答案:

没有答案