使用Plist中的数据在地图上显示多个引脚

时间:2012-09-18 02:39:41

标签: iphone objective-c ios xcode

我正在尝试使用字典数组中的纬度和经度在地图上显示多个图钉。问题是它只显示了plist中最后一个字典的引脚。

这是我的方法:

- (void)loadMapPins
{
MapAnnotation *annotation = [[MapAnnotation alloc] init];

for (int i=0; i<self.dataDictionary.count; i++){

    NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:[self.dataDictionary objectAtIndex:i]];

    double latitude = [[dictionary objectForKey:@"Latitude"] doubleValue];
    double longitude = [[dictionary objectForKey:@"Longitude"] doubleValue];

    CLLocationCoordinate2D coord = {.latitude =
        latitude, .longitude =  longitude};
    MKCoordinateRegion region = {coord};

    annotation.title = [dictionary objectForKey:@"Name"];
    annotation.subtitle = [dictionary objectForKey:@"Center Type"];
    annotation.coordinate = region.center;
    [mapView addAnnotation:annotation];
    }
}

我需要它来完成循环并相应地将引脚放在地图上。任何帮助/示例都表示赞赏。

1 个答案:

答案 0 :(得分:1)

我认为您希望将注释创建移动到循环中。从你只创建一个的东西的外观,然后在循环中你一遍又一遍地改变它。循环完成后,对注释变量的最后修改将反映您正在迭代的self.dataDictionary中的最后一项。

下面的代码在每次循环迭代时创建一个新的注释对象。

- (void)loadMapPins
{

    for (int i=0; i<self.dataDictionary.count; i++){

    NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:[self.dataDictionary objectAtIndex:i]];

    double latitude = [[dictionary objectForKey:@"Latitude"] doubleValue];
    double longitude = [[dictionary objectForKey:@"Longitude"] doubleValue];

    CLLocationCoordinate2D coord = {.latitude =
        latitude, .longitude =  longitude};
    MKCoordinateRegion region = {coord};

    MapAnnotation *annotation = [[MapAnnotation alloc] init];
    annotation.title = [dictionary objectForKey:@"Name"];
    annotation.subtitle = [dictionary objectForKey:@"Center Type"];
    annotation.coordinate = region.center;
    [mapView addAnnotation:annotation];
    }
}

希望这有帮助,

Scott H

相关问题