2013-12-18 53 views
0

我似乎無法讓我的註釋在我的地圖視圖中彈出。我有很多來自plist的註釋,除了一個外,他們沒有出現在地圖上。請幫助?註解未出現

地圖viewcontroller.h

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 
#import <MapKit/MapKit.h> 


@interface Map_ViewController : UIViewController 
<CLLocationManagerDelegate, MKMapViewDelegate > 

@property (weak, nonatomic) IBOutlet MKMapView *mapView; 

@end 

地圖ViewController.m

#import "Map ViewController.h" 
#import "MADAnnotation.h" 
@interface Map_ViewController() 

@end 

#define CHARMANDER_LATITUDE 40.0078429; 
#define CHARMANDER_LONGITUDE -105.26966709999999; 

#define CHARMELEON_LATITUDE 40.0104104; 
#define CHARMELEON_LONGITUDE -105.28965619999997; 

#define CHARIZARD_LATITUDE 40.0064377; 
#define CHARIZARD_LONGITUDE -105.27301119999998; 

#define BULBASAUR_LATITUDE 40.0096216; 
#define BULBASAUR_LONGITUDE -105.27282120000001; 

#define IVYSAUR_LATITUDE 40.0108876; 
#define IVYSAUR_LONGITUDE -105.27389820000002; 

#define VENUSAUR_LATITUDE 40.0079411; 
#define VENUSAUR_LONGITUDE -105.27507350000002; 

#define SQUIRTLE_LATITUDE 40.0043601; 
#define SQUIRTLE_LONGITUDE -105.26956860000001; 

#define WARTORTLE_LATITUDE 40.0104104; 
#define WARTORTLE_LONGITUDE -105.26633140000001; 

#define BLASTOISE_LATITUDE 40.0104961; 
#define BLASTOISE_LONGITUDE -105.26594160000002; 


#define THE_SPAN 0.20f; 

@implementation Map_ViewController 
{ 
CLLocationManager *locationManager; 
MADAnnotation *annotation; 


} 

@synthesize mapView; 


- (void)viewDidLoad { 



    mapView.delegate=self; 
    mapView.mapType=MKMapTypeHybrid; 
    locationManager=[[CLLocationManager alloc] init]; locationManager.delegate=self; locationManager.desiredAccuracy=kCLLocationAccuracyBest; locationManager.distanceFilter=kCLDistanceFilterNone; 


    [locationManager startUpdatingLocation]; 




    [super viewDidLoad]; 
    // Create the region 
    MKCoordinateRegion myRegion; 

    //Center 
    CLLocationCoordinate2D center; 
    center.latitude = BULBASAUR_LATITUDE; 
    center.longitude = BULBASAUR_LONGITUDE; 

    //Span 
    MKCoordinateSpan span; 
    span.latitudeDelta = THE_SPAN; 
    span.longitudeDelta = THE_SPAN; 

    myRegion.center = center; 
    myRegion.span=span; 

    //Set our mapView 
    [mapView setRegion:myRegion animated:YES]; 



    NSMutableArray *annotations = [[NSMutableArray alloc]init]; 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"PokeLocations" ofType:@"plist"]; 
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; 
    NSArray *anns = [dict objectForKey:@"PokeLocations"]; 
    NSLog(@"read1"); 

    for(int i = 0; i < [anns count]; i++) { 
     NSLog(@"read2"); 
     float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] floatValue]; 
     float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] floatValue]; 
     NSLog(@"read3"); 

     MADAnnotation *myAnnotation = [[MADAnnotation alloc] init]; 
     CLLocationCoordinate2D theCoordinate; 
     theCoordinate.latitude = realLatitude; 
     theCoordinate.longitude = realLongitude; 
     myAnnotation.coordinate = theCoordinate; 
     myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Title"]; 
     myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"Subtitle"]; 
     [mapView addAnnotation:myAnnotation]; 
     [annotations addObject:myAnnotation]; 
     //[myAnnotation release]; 
    } 


} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ 


    MKCoordinateSpan span; 
    span.latitudeDelta=.001; 
    span.longitudeDelta=.001; 
    MKCoordinateRegion region; 
    region.center= manager.location.coordinate; 
    region.span=span; 
    [mapView setRegion:region animated:YES]; 
    if(annotation){ 
     [annotation moveAnnotation:manager.location.coordinate]; 
    } 
    else { 
     annotation=[[MADAnnotation alloc] initWithCoordinate:manager.location.coordinate]; 
     [mapView addAnnotation:annotation];  } 
} 
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ 
    NSString *errorType; 
    if (error.code == kCLErrorDenied) { 
     [email protected]"Access Denied"; 
    } else [email protected]"Error"; 
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error" 

                message:errorType delegate:nil 
             cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 


} 
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation1 
{ 
    static NSString *AnnotationViewID = @"annotationViewID"; 

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 

    if (annotationView == nil) 
    { 
     annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation1 reuseIdentifier:AnnotationViewID]; 
    } 

    //Custom Pin 
    annotationView.image = [UIImage imageNamed:@"pokeballsmall2.png"]; 

    //Custom Thumbnail (left side) 
    UIImageView *IconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pokeballsmall.png"]]; 
    annotationView.leftCalloutAccessoryView = IconView; 

    annotationView.canShowCallout = YES; 
    annotationView.annotation = annotation1; 

    return annotationView; 


} 


@end 

MADAnnotation.h

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 
@interface MADAnnotation : NSObject <MKAnnotation> 

@property (nonatomic, readonly)CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy)NSString *title; 
@property (nonatomic, copy)NSString *subtitle; 
@property (nonatomic, copy) UIImageView * leftCalloutAccessoryView; 

-(id)initWithCoordinate:(CLLocationCoordinate2D)coor; 
-(void) moveAnnotation:(CLLocationCoordinate2D)newCoordinate; 

@end 

MADAnnotation.m

#import "MADAnnotation.h" 

@implementation MADAnnotation 

@synthesize coordinate, title, subtitle, leftCalloutAccessoryView; 

-(id)initWithCoordinate:(CLLocationCoordinate2D)coor{ coordinate=coor; 
    [email protected]"You are here"; 
    subtitle=[NSString stringWithFormat:@"Latitude: %f. Longitude: %f", coordinate.latitude, coordinate.longitude]; return self; 
} 


-(void) moveAnnotation:(CLLocationCoordinate2D)newCoordinate{ 
    [self willChangeValueForKey:@"coordinate"];   [self willChangeValueForKey:@"subtitle"];  coordinate = newCoordinate; 
    subtitle = [[NSString alloc] initWithFormat:@"Latitude: %f. Longitude: %f", coordinate.latitude, coordinate.longitude]; 
    [self didChangeValueForKey:@"coordinate"]; 
    [self didChangeValueForKey:@"subtitle"]; 
} 


@end 

annotations.plist

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>PokeLocations</key> 
    <array> 
     <dict> 
      <key>Title</key> 
      <string>#004 Charmander</string> 
      <key>Subtitle</key> 
      <string>Fire Type</string> 
      <key>Latitude</key> 
      <string>40.0078429</string> 
      <key>Longitude</key> 
      <string>-105.26966709999999 </string> 
     </dict> 
     <dict> 
      <key>Title</key> 
      <string>#005 Charmeleon</string> 
      <key>Subtitle</key> 
      <string>Fire Type</string> 
      <key>Latitude</key> 
      <string>40.0104104</string> 
      <key>Longitude</key> 
      <string>-105.28965619999997</string> 
     </dict> 
     <dict> 
      <key>Title</key> 
      <string>#006 Charizard</string> 
      <key>Subtitle</key> 
      <string>Fire/Flying Type</string> 
      <key>Latitude</key> 
      <string>40.0064377 </string> 
      <key>Longitude</key> 
      <string>-105.27301119999998 </string> 
     </dict> 
     <dict> 
      <key>Title</key> 
      <string>#001 Bulbasaur</string> 
      <key>Subtitle</key> 
      <string>Grass/Poison Type</string> 
      <key>Latitude</key> 
      <string>40.0096216</string> 
      <key>Longitude</key> 
      <string>-105.27282120000001 </string> 
     </dict> 
     <dict> 
      <key>Title</key> 
      <string>#002 Ivysaur</string> 
      <key>Subtitle</key> 
      <string>Grass/Poison Type</string> 
      <key>Latitude</key> 
      <string>40.0108876</string> 
      <key>Longitude</key> 
      <string>-105.27389820000002 </string> 
     </dict> 
     <dict> 
      <key>Title</key> 
      <string>#003 Venusaur</string> 
      <key>Subtitle</key> 
      <string>Grass/Poison Type</string> 
      <key>Latitude</key> 
      <string>40.0079411</string> 
      <key>Longitude</key> 
      <string>-105.27507350000002 </string> 
     </dict> 
     <dict> 
      <key>Title</key> 
      <string>#007 Squirtle</string> 
      <key>Subtitle</key> 
      <string>Water Type</string> 
      <key>Latitude</key> 
      <string>40.0043601</string> 
      <key>Longitude</key> 
      <string>-105.26956860000001 </string> 
     </dict> 
     <dict> 
      <key>Title</key> 
      <string>#008 Wartortle</string> 
      <key>Subtitle</key> 
      <string>Water Type</string> 
      <key>Latitude</key> 
      <string>40.0104961</string> 
      <key>Longitude</key> 
      <string>-105.26633140000001</string> 
     </dict> 
     <dict> 
      <key>Title</key> 
      <string>#009 Blastoise</string> 
      <key>Subtitle</key> 
      <string>Water Type</string> 
      <key>Latitude</key> 
      <string>40.0104961</string> 
      <key>Longitude</key> 
      <string>-105.26594160000002 </string> 
     </dict> 
    </array> 

</dict> 
</plist> 
+0

的一個這是出現 - 這是從plist最後的註釋? –

+0

沒有任何東西從plist中出現。它來自地圖視圖控制器中的位置管理器 – spoons55

+0

這是正在運行的實際代碼嗎? viewDidLoad中的這一行不應起作用:'myAnnotation.coordinate = theCoordinate;'因爲在'MADAnnotation'類中,'coordinate'屬性被標記爲只讀。你爲什麼要'init'而不是'initWithCoordinate'?代碼是否真的進入'for'循環? – Anna

回答

0

我不能對評論進行回覆,所以我張貼作爲一個答案

你可以用下面的方法將屬性添加到您的註釋:

-(MKAnnotationView *) mapView:(MKMapView *)mapMKMapView viewForAnnotation:  (id<MKAnnotation>)annotation 
{ 
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation  reuseIdentifier:@"current"]; 
MyPin.pinColor = MKPinAnnotationColorPurple; // <--for coloring purpose 

UIButton *adverButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[adverButton addTarget:self action:@selector(button:)forControlEvents:UIControlEventTouchUpInside]; 

MyPin.rightCalloutAccessoryView = adverButton; 
MyPin.draggable = YES; 
MyPin.highlighted = YES; 
MyPin.animatesDrop = TRUE; 
MyPin.canShowCallout = YES; 

return MyPin; 
} 

-(void)button:(id)sender 
{ 
    //add your nextView you want to open here 
}