ios:如何从.plist文件中填充mapview

时间:2013-07-16 17:24:00

标签: ios objective-c for-loop mkmapview

您好,感谢您的帮助。

是否可以使用for循环从.plist文件填充10个mapview位置?如果是这样的话?

我目前的mapview代码都是硬编码的。我希望通过拉 - 经度,纬度,标题,子标题 - 如果可能的话来自for循环来改进。谢谢。

  ////
.....
                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];.......

我想这样的东西,但不太确定如何完成

        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];

//////

?????

             }

2 个答案:

答案 0 :(得分:0)

只需遍历plist数组并使用您拥有的代码从提取的数据中创建区域。然后,不是annoArray是最后创建的数组,而是将其作为一个可变数组,并在每次循环迭代结束时添加每个项目。

答案 1 :(得分:0)

[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];