dropping pin with annotations in DetailView from NSDictionary in MainView

Discussion in 'iOS Programming' started by gbenna, Mar 1, 2012.

  1. gbenna, Mar 1, 2012
    Last edited: Mar 3, 2012

    macrumors member

    Joined:
    Jul 27, 2011
    #1
    I am trying to learn how to drop a pin inside an app that has a title and a picture. These would come from an NSDictionary loaded from a previous view Controller.

    This is the structure


    MainViewController (NSDictionary with annotation title and image objects for keys)

    Loads a DetailViewController map view with different title and image depending on variable in MainViewController

    DetailViewController shows user's location. A pin is dropped at that location with the title and image loaded from the MainViewController's NSDictionary.

    This is my question.

    1. I have the correct title showing up from the Dictionary but i can't change the pin Color and the image isn't showing up.

    Here is the code I use to drop the pin and show the annotations.


    Code:
    -(void) viewDidLoad {
    
    
     [mapView1 setMapType:MKMapTypeHybrid];
    [mapView1 setZoomEnabled:YES];
    [mapView1 setScrollEnabled:YES];
        MKCoordinateRegion region = { {0.0,0.0}, {0.0,0.0} };
        region.center.latitude =locationManager.location.coordinate.latitude;
        region.center.longitude= locationManager.location.coordinate.longitude;
    region.span.longitudeDelta = 0.001f;
    region.span.latitudeDelta = 0.001f;
    [mapView1 setRegion:region animated:YES];
        [mapView1 setDelegate:self];   
    }
    
    
    
    - (IBAction)getLocation:(id)sender {
        
        
        locationManager= [[CLLocationManager alloc] init];
        locationManager.distanceFilter=kCLDistanceFilterNone;
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        [locationManager startUpdatingLocation];
        
        [mapView1 setMapType:MKMapTypeHybrid];
        [mapView1 setZoomEnabled:YES];
        [mapView1 setScrollEnabled:YES];
        MKCoordinateRegion region = {{0.0,0.0},{0.0,0.0}};
        region.center.latitude = locationManager.location.coordinate.latitude;
        region.center.longitude = locationManager.location.coordinate.longitude;
        region.span.longitudeDelta = 0.001f;
        region.span.latitudeDelta = 0.001f;
        [mapView1 setRegion:region animated:YES];
        [mapView1 setDelegate:sender];
        
      
        DetailViewController *ann1 =[[DetailViewController alloc] init];
        ann1.title=[(NSMutableDictionary *)mapDictionary objectForKey:@"titlekey"];
        
        ann1.coordinate= region.center;
        [mapView1 addAnnotation:ann1];
        
    }
    
    
    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
        MKPinAnnotationView *pinView = nil;
            if (annotation == mapView1.userLocation) {
    [mapView1 setCenterCoordinate:mapView1.userLocation.coordinate animated:YES];
    return nil;
                static NSString *defaultPinID = @"com.invasivecode.pin";
                pinView = (MKPinAnnotationView *) [mapView1 dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
                if (pinView == nil) pinView = [[MKPinAnnotationView alloc]
                                                
                                                initWithAnnotation:annotation reuseIdentifier:defaultPinID];
                
                
                pinView.canShowCallout = YES;
                pinView.animatesDrop = YES;
                
                if([[annotation title] isEqualToString:[(NSMutableDictionary *)mapDictionary objectForKey:@"titlekey"]])
                {
    
                   //here is where I try to change the color of the pin (no go)
                 pinView.pinColor = MKPinAnnotationColorGreen;
    
                       //Here is where I try to load the image from the Document's directory into the pin.  For some reason is part of the annotation isn't showing up or being called
                NSString *imageName = [mapDictionary objectForKey:@"mapimagekey"];
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   
                                                                     NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0];
                //Get a full path to the image in the documents directory.
                NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:imageName];
                
                //Now load the image at fullPath and install it into our image view's image property.
                
                UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile: fullPath]];
                myImageView.frame = CGRectMake (0,0,90,60);
                pinView.leftCalloutAccessoryView = myImageView;
                 }
    }
        return pinView;
    }

    Can anyone see where I've gone wrong here? Should the annotation part of the code be included inside the IBAction code?

    Thanks for your help

    I got this going. I tried to delete the post but I'm not sure how to do this. clicked on edit/delete got edit / no delete.
     

Share This Page