Iphone 使用Plist数据在地图上显示多个管脚
我试图使用一系列字典中的纬度和经度在地图上显示多个PIN。问题是它总是只显示plist中最后一个字典的pin 以下是我的方法:Iphone 使用Plist数据在地图上显示多个管脚,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,我试图使用一系列字典中的纬度和经度在地图上显示多个PIN。问题是它总是只显示plist中最后一个字典的pin 以下是我的方法: - (void)loadMapPins { MapAnnotation *annotation = [[MapAnnotation alloc] init]; for (int i=0; i<self.dataDictionary.count; i++){ NSDictionary *dictionary = [NSDictionary diction
- (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];
}
}
-(无效)loadMapPins
{
MapAnnotation*annotation=[[MapAnnotation alloc]init];
对于(int i=0;i我认为您希望将注释创建移动到循环中。从外观上看,您只创建了一个注释,然后在循环中反复对其进行变异。循环完成后,对注释变量的最后一次修改反映了您正在迭代的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];
}
}
-(无效)loadMapPins
{
对于(int i=0;就是这样!你知道有没有一种方法可以重用注释,就像我们在tableView中重用单元格一样?