2012-04-25 88 views
0

我想在我的mapview中添加註釋,但我無法添加它們。有人可以建議我在哪裏做錯了嗎? 代碼:代碼添加在地圖:無法在iPhone上顯示地圖上的自定義註釋

mapview = [[MKMapView alloc] initWithFrame:CGRectMake(10, 175, 300, 268)]; 
mapview.delegate = self; 
mapview.userInteractionEnabled = TRUE; 
[mapview setZoomEnabled:TRUE]; 
[mapview setScrollEnabled:TRUE]; 
mapview.showsUserLocation = TRUE; 
[self.view addSubview:mapview]; 

//添加註釋在另一種方法中:
爲(在mapview.annotations ID註釋){ [MapView的removeAnnotation:註釋]; }

 for(int i =0;i<arrEventList.count;i++){ 
      CLLocationCoordinate2D theCoordinate; 
      theCoordinate.latitude = [[NSString stringWithFormat:@"%@",[(NSDictionary*)[arrEventList objectAtIndex:i] objectForKey:@"lat"]] floatValue]; 
      theCoordinate.longitude = [[NSString stringWithFormat:@"%@",[(NSDictionary*)[arrEventList objectAtIndex:i] objectForKey:@"lng"]] floatValue]; 

      MyLocation *annotation = [[[MyLocation alloc] initWithName:[(NSDictionary*)[arrEventList objectAtIndex:i] objectForKey:@"event"] address:[(NSDictionary*)[arrEventList objectAtIndex:i] objectForKey:@"place"] coordinate:theCoordinate] autorelease]; 
      [mapview addAnnotation:annotation]; 
      //[annotations addObject:myAnno.annotation]; 
     } 

//委託方法:

- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation { 
static NSString *identifier = @"MyLocation"; 
if ([annotation isKindOfClass:[MyLocation class]]) { 

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 
    if (annotationView == nil) { 
     annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
    } else { 
     annotationView.annotation = annotation; 
    } 

    annotationView.enabled = YES; 
    annotationView.canShowCallout = YES; 
    annotationView.image=[UIImage imageNamed:@"annotation.png"];//here we use a nice image instead of the default pins 

    return annotationView; 
} 

return nil; 

}

以上的委託方法只要求自己的位置,但它不叫了,我想添加其他註釋。

有人點我到我的錯誤

@interface MyLocation : NSObject <MKAnnotation> { 
NSString *_name; 
NSString *_address; 
CLLocationCoordinate2D _coordinate; 

}

@property (copy) NSString *name; 
@property (copy) NSString *address; 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate; 

@end 
//Mylocation class: 

@implementation MyLocation 
@synthesize name = _name; 
@synthesize address = _address; 
@synthesize coordinate = _coordinate; 

- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate { 
if ((self = [super init])) { 
    _name = [name copy]; 
    _address = [address copy]; 
    _coordinate = coordinate; 
    } 
    return self; 
} 

- (NSString *)title { 
if ([_name isKindOfClass:[NSNull class]]) 
    return @"Unknown charge"; 
else 
    return _name; 
} 

- (NSString *)subtitle { 
return _address; 
} 

回答

0

我發現這個問題。我的代碼是正確的。 latittude和經度的價值是一些如何互換。我的錯誤,但我現在已經糾正它