MKPinAnnotationView红色图钉被替换为图像

时间:2013-01-03 04:30:12

标签: iphone objective-c mkmapview mkannotation mkannotationview

我已将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];

1 个答案:

答案 0 :(得分:1)

只需在viewWillAppear:viewDidAppear:方法中添加整个图钉或setRegion

我们viewForAnnotation addAnnotation时再次调用此MKMapView,因此只需调用它或使用setRegion MKMapView方法刷新它。

或者只是移除这些整个引脚并再次添加到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];
}

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