使用MKMapViewDelegate

时间:2012-08-21 10:37:32

标签: objective-c mkmapview mkannotationview

我无法理解它为什么不起作用。

我有一个带标记的地图,我想更改图标

map.h:

#define METERS_PER_MILE 1609.344

@interface Map : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate>{

IBOutlet MKMapView *map;
CLLocationManager *locationManager;
}


@property(nonatomic,retain) IBOutlet MKMapView *map;
@property(nonatomic,retain)IBOutlet UIWindow *window;
@property (nonatomic, readwrite) CLLocationCoordinate2D location;


- (void)setMarkers:(MKMapView *)mv;
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;

@end

在map.m中:

@implementation Map

@synthesize map, window, location;


- (void)setMarkers:(MKMapView *)mv 
{
 //no necesary here

}


- (void)viewWillAppear:(BOOL)animated {
// 1
location.latitude = 38.989567;
location.longitude= -1.856283;
// 2
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location, 0.8*METERS_PER_MILE, 0.8*METERS_PER_MILE);
// 3
MKCoordinateRegion adjustedRegion = [map regionThatFits:viewRegion];
// 4
[map setRegion:adjustedRegion animated:YES];

//[self setMarkers: map];

CLLocationCoordinate2D pointCoord = location;
NSString *theTitle = @"title";
NSString *theSubtitle = @"subtitle";

MapPoint *mp = [[MapPoint alloc] initWithCoordinate:pointCoord title:theTitle subTitle:theSubtitle];
[map addAnnotation:mp];


}

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

   NSLog(@"here!!");
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MapPoint class]]) {

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [map 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:@"Icon.png"];//here we use a nice image instead of the default pins

    return annotationView;
}

return nil;
}

@end

永远不会执行viewForAnnotation(我用NSLog打开它)

我该怎么办?

提前谢谢

1 个答案:

答案 0 :(得分:2)

看起来您的代码没有任何问题。正如phix23所说,你可能错过了将MKMapView与代表联系起来:

Example 01

Example 02