1

我已经用MKPinAnnotationView图片替换了红色别针

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation  
{
MKAnnotationView *annotationView=[mapView dequeueReusableAnnotationViewWithIdentifier:parkingAnnotationIdentifier];
//If one isn’t available, create a new one
if(!annotationView){
annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:parkingAnnotationIdentifier];
//Here’s where the magic happens
annotationView.image=[UIImage imageNamed:@"dot.png"];
}
return annotationView;
}

现在,当我单击任何按钮时,我有一个详细信息披露,它将转到该注释的详细页面。

到这里为止还好。现在的问题是,当我再次从详细信息页面返回地图视图时,会出现默认的红色图钉而不是 dot.png,因为第一次加载视图时-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 会调用此方法,但是当我们从详细信息返回时页面视图将出现没有被调用。如何解决这个问题?

if ([list_array count]>0) {
        location1.latitude = [[[list_array objectAtIndex:0]valueForKey:@"latitude"]floatValue];
        location1.longitude = [[[list_array objectAtIndex:0]valueForKey:@"longitude"]floatValue];
        region.center = location1;

        if ([[list_array objectAtIndex:0]valueForKey:@"company_id"]) {
            for (int i=0; i<[list_array count]; i++) 
            {
                CLLocationCoordinate2D location1;
                location1.latitude = [[[list_array objectAtIndex:i]valueForKey:@"latitude"]floatValue];
                location1.longitude = [[[list_array objectAtIndex:i]valueForKey:@"longitude"]floatValue];
                region.span = span;

                BasicMapAnnotation *addAnnotation1 = [[BasicMapAnnotation alloc] initWithLatitude:location1.latitude andLongitude:location1.longitude] ; 
                addAnnotation1.tag = i;
                [mapView addAnnotation:addAnnotation1];
            }
        }
    }

    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];
    //[self.view addSubview:self.mapView];
4

1 回答 1

1

viewWillAppear:只需在orviewDidAppear:方法中添加整个引脚或 setRegion

当我们进入viewForAnnotation时再次调用这里,所以只需调用它或使用.addAnnotationMKMapViewsetRegionMKMapView

或者只是删除这些整个引脚并再次添加viewDidAppear:如下......

-(void)viewDidAppear:(BOOL)animated
{
    NSArray *existingpoints = mapView.annotations;
    if ([existingpoints count] > 0)
        [mapView removeAnnotations:existingpoints];

    if ([[list_array objectAtIndex:0]valueForKey:@"company_id"]) {
   for (int i=0; i<[list_array count]; i++) 
   {
    CLLocationCoordinate2D location1;
    location1.latitude = [[[list_array objectAtIndex:i]valueForKey:@"latitude"]floatValue];
    location1.longitude = [[[list_array objectAtIndex:i]valueForKey:@"longitude"]floatValue];
    region.span = span;

    BasicMapAnnotation *addAnnotation1 = [[BasicMapAnnotation alloc] initWithLatitude:location1.latitude andLongitude:location1.longitude] ; 
    addAnnotation1.tag = i;
    [mapView addAnnotation:addAnnotation1];
    }
}
   [mapView setRegion:region animated:TRUE];
   [mapView regionThatFits:region];
}

我希望这对你有帮助......

于 2013-01-03T05:03:48.567 回答