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

ピン全体またはsetRegionをviewWillAppear:またはviewDidAppear:メソッドに追加するだけです

hereは、私たちが入っviewForAnnotationたときに再び呼び出されるので、それを呼び出すか、メソッド of で更新します。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 に答える