简体   繁体   中英

ios: how to populate mapview from .plist file

Hello and Thanks for the help.

Is it possible to populate 10 mapview locations from a .plist file using a for loop ? If so how ?

my current code for my mapview is all hard coded. I would like to improve upon this by pulling - Longitude, Latitude, Title, SubTitle - from a for loop if possible. Thank You.

  ////
.....
                MKCoordinateRegion region3 = {{0.0,0.0}, {0.0,0.0}};

                region3.center.latitude = 33.45869;
                region3.center.longitude = -84.66931;
                region3.span.longitudeDelta = 0.01f;
                region3.span.latitudeDelta=0.01f;
                //  [mapview setRegion:region4 animated:YES];

                BandsMap *ann3 = [[BandsMap alloc]init];
                ann3.title = @"Indigo Bar & Lounge";
                ann3.subtitle = @"Let the good times roll";
                ann3.coordinate = region3.center;//

            //// 


                annoArray = [[NSArray alloc] initWithObjects:ann1,ann2,ann3.....,nil];


                [mapView addAnnotations:annoArray];.......

Something like this I suppose but not quite sure how to finish

        for(NSDictionary *key in mapDataPlist)
            {
                    NSString *c = [key objectForKey:@"Title"];
                NSString *a = [key objectForKey:@"SubTitle"];
                NSString *lat = [key objectForKey:@"Latitude"];
                NSString *lon = [key objectForKey:@"Longitude"];

                CGFloat strFLat = (CGFloat)[lat floatValue];
                CGFloat strFLon = (CGFloat)[lon floatValue];

//////

?????

             }

Just iterate over your plist array and use the code you have to create the regions from the extracted data. Then, instead of annoArray being an array you create at the end, make it a mutable array and add each item at the end of each loop iteration.

[mapView setRegion:adjustedRegion animated:YES];
    mapView.mapType=MKMapTypeStandard;
    UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.frame = CGRectMake(28, 60, 334, 649);
    [self.view addSubview:tableView];


    NSString *path = [[NSBundle mainBundle] pathForResource:
                      @"CityList" ofType:@"plist"];
    NSMutableArray *cities = [[NSMutableArray alloc] initWithContentsOfFile:path];
    NSMutableArray *localCities= [[NSMutableArray alloc]init];
    for (int i=0;i<[cities count];i++)
    {
    NSLog(@"%@",[[cities objectAtIndex:i]objectForKey:@"cityNameKey"]);

        [localCities addObject:[[cities objectAtIndex:i]objectForKey:@"cityNameKey"]];
    }


    locationArray= localCities;

    Annotation *annotation=[[Annotation alloc]init];
    annotation.coordinate = CLLocationCoordinate2DMake(29.7631,-95.3631);
    annotation.color = [UIColor redColor];
    annotation.title=[NSString stringWithFormat:@"Houston"];
    [mapView addAnnotation:annotation];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM