Help with XCode Mapview Annotation

Discussion in 'iOS Programming' started by Ruoz, Feb 7, 2014.

  1. Ruoz macrumors newbie

    Joined:
    Feb 7, 2014
    #1
    Hi,

    Im new to XCode and still learning. Wondering if you guys can help me with my problem with mapview, annotation. The problem is really taking a toll on me.

    What im trying to do is this. All my annotation is setup. I have 2 locations listed. And when the user click on one of the location, a bubble would appear, saying the location title and subtitle. But when the user click on the ">" icon, within the bubble. 2 popups would appear. Showing 2 location information, instead of the one that is selected.

    I have copy and paste the codes onto this board. If you have time, please take a look, Thanks!

    File Name: Annotation.h
    ________________________________________________________
    Code:
    #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>
    
    @interface Annotation : NSObject <MKAnnotation>
    
    @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, copy) NSString *subtitle;
    @property (nonatomic, copy) NSString *name;
    
    - initWithPosition:(CLLocationCoordinate2D)coords;
    
    @end
    
    
    File Name: Annotation.m
    ________________________________________________________
    Code:
    #import "Annotation.h"
    
    @implementation Annotation
    
    @synthesize coordinate;
    @synthesize title;
    @synthesize subtitle;
    @synthesize name;
    
    - initWithPosition:(CLLocationCoordinate2D)coords {
        if (self = [super init]) {
            self.coordinate = coords;
        }
        return self;
    }
    @end
    

    File Name: AnnotationViewController.h
    ________________________________________________________
    Code:
    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>
    
    @interface AnnotationViewController : UIViewController <MKMapViewDelegate> {
        
        IBOutlet MKMapView *mapView;
        
    }
    
    @property(nonatomic, retain) IBOutlet MKMapView *mapView;
    
    @end
    

    File Name: AnnotationViewController.m
    _________________________________________________________
    Code:
    #import "AnnotationViewController.h"
    #import "Annotation.h"
    
    @implementation AnnotationViewController
    @synthesize mapView;
    
    -(void)viewDidLoad {
        
        [super viewDidLoad];
        [mapView setMapType:MKMapTypeStandard];
        [mapView setZoomEnabled:YES];
        [mapView setScrollEnabled:YES];
        [mapView setDelegate:self];
        
        MKCoordinateRegion TT = { {0.0, 0.0} , {0.0, 0.0} };
        TT.center.latitude = 43.76919;
        TT.center.longitude = -79.41245;
        TT.span.longitudeDelta = 0.02f;
        TT.span.latitudeDelta = 0.02f;
        [mapView setRegion:TT animated:YES];
        
        Annotation *ann7 = [[Annotation alloc] init];
        ann7.title = @"Chinatown";
        ann7.subtitle = @"Adress Here Chinatown";
        ann7.coordinate = TT.center;
        [mapView addAnnotation:ann7];
        
        MKCoordinateRegion TTY = { {0.0, 0.0} , {0.0, 0.0} };
        TTY.center.latitude = 43.76919;
        TTY.center.longitude = -79.41445;
        TTY.span.longitudeDelta = 0.02f;
        TTY.span.latitudeDelta = 0.02f;
        [mapView setRegion:TTY animated:YES];
        
        Annotation *ann8 = [[Annotation alloc] init];
        ann8.title = @"Empress";
        ann8.subtitle = @"Address Here Empress";
        ann8.coordinate = TTY.center;
        [mapView addAnnotation:ann8];
        
        
    }
    
    
    -(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
        MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
        view.pinColor = MKPinAnnotationColorPurple;
        view.enabled = YES;
        view.animatesDrop = YES;
        view.canShowCallout = YES;
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"GPSicon.png"]];
        view.leftCalloutAccessoryView = imageView;
        view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        
        return view;
        
    }
    
    
    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
        Annotation *ann7 = (Annotation *)view.annotation;
        [self.mapView deselectAnnotation:ann7 animated:YES];
        NSString *msg = [@"Chinatown Location01" stringByAppendingFormat:@"Store Open Hr Chinatown"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Phone Number Chinatown" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        
        Annotation *ann8 = (Annotation *)view.annotation;
        [self.mapView deselectAnnotation:ann8 animated:YES];
        NSString *msg02 = [@"Empress Location02" stringByAppendingFormat:@"Store Open Hr Empress"];
        UIAlertView *alert02 = [[UIAlertView alloc] initWithTitle:@"Phone Number Empress " message:msg02 delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert02 show];
    }
    
    
    
    -(void)button:(id)sender {
        
        NSLog(@"Button action");
        
    }
    
    - (void)dealloc
    {
        
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    /*
     // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
     - (void)viewDidLoad
     {
     [super viewDidLoad];
     }
     */
    
    - (void)viewDidUnload
    {
        [self setMapView:Nil];
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    @end
    

    Any help or guesses would be great help. Thanks alot!
     
  2. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #2
    I'm sure you found the cause already, above; if so, please mark the thread as resolved.
     

Share This Page