08442
08442

Reputation: 521

MKPinAnnotationView red pin is replaced with image

I have replaced the MKPinAnnotationView red pin with an image in

-(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;
}

Now i have a detail disclosure when I click on any button it will go to the detailed page of that annotation.

Up to here its fine. Now the problem is that when I come back to the map view again from the detail page the default red pin appears instead of dot.png because when a view is loaded for the first time -(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation this method is called but when we come back from the detail page view will appear is not getting called. How to resolve this issue?

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];

Upvotes: 1

Views: 760

Answers (1)

Paras Joshi
Paras Joshi

Reputation: 20541

just add the whole pins or setRegion in viewWillAppear: or viewDidAppear: method

here viewForAnnotation is called again when we addAnnotation in MKMapView so just call it or also refresh it with setRegion method of MKMapView.

Or just remove these whole pins and add again in viewDidAppear: like bellow....

-(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];
}

i hope this helpful to you...

Upvotes: 1

Related Questions