添加多个注释到地图

时间:2014-08-06 12:17:08

标签: ios mapkit

-(void)setLocation
{
    MKCoordinateRegion myregion;
    CLLocationCoordinate2D center;

    center.latitude=Nashik_Latitude;
    center.longitude=Nashik_Longitude;

    MKCoordinateSpan span;
    span.latitudeDelta=the_span;
    span.longitudeDelta=the_span;

    myregion.center=center;
    myregion.span=span;

    [self.mapView setRegion:myregion animated:YES];
    CLLocationCoordinate2D location;
    Annotation *myann;
    for (APIElement *element in feedsFromRoot)
    {
        myann=[[Annotation alloc]init];
        CLLocationCoordinate2D coord;
        coord.longitude = (CLLocationDegrees)[element.strlat doubleValue];
        coord.latitude = (CLLocationDegrees)[element.strlong doubleValue];
        location.latitude=coord.latitude;
        location.longitude=coord.longitude;
        myann.coordinate=location;
        myann.title=element.strName;
        myann.subtitle=element.strVicinity;
        [locations addObject:myann];
    }
    [self.mapView addAnnotations:locations];
}

我想添加多个注释,这些注释存储在数组中并编写了以下代码,但它没有显示任何注释。请指出错误。

我已经从feedsFromRoot数组中读取了纬度和经度。数据已正确获取,但地图上仍缺少注释。书面代码是正确的形式还是应该进行任何修改?

数据由JSOn解析收集并存储在feedFromRoot数组中。

1 个答案:

答案 0 :(得分:0)

尝试以下代码......您应该在每次迭代中添加新的Annotation对象。

-(void)setLocation
{

    MKCoordinateRegion myregion;
    CLLocationCoordinate2D center;

    center.latitude=Nashik_Latitude;
    center.longitude=Nashik_Longitude;

    MKCoordinateSpan span;
    span.latitudeDelta=the_span;
    span.longitudeDelta=the_span;

    myregion.center=center;
    myregion.span=span;

    [self.mapView setRegion:myregion animated:YES];


    for (APIElement *element in feedsFromRoot)
    {
        Annotation *myann=[[Annotation alloc]init];
        CLLocationCoordinate2D coord;
        coord.longitude = (CLLocationDegrees)[element.strlat doubleValue];
        coord.latitude = (CLLocationDegrees)[element.strlong doubleValue];
        CLLocationCoordinate2D location;
        location.latitude=coord.latitude;
        location.longitude=coord.longitude;
        myann.coordinate=location;
        myann.title=element.strName;
        myann.subtitle=element.strVicinity;
        [locations addObject:myann];
    }
    [self.mapView addAnnotations:locations];
}