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

}

MKAnnotationView方法用于在地图中显示图标。如何在-didUpdateToLocation使用objective-c的方法中调用MKAnnotationView方法?

4

1 回答 1

1

我不确定这是否是您想要的,但是:

这是我为注释编写的类:

.h 文件:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@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

和 .m 文件:

#import "MyLocation.h"

@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;
}

@end

在视图控制器中,我正在调用位置管理器,例如:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = 10.0; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];    

然后在位置管理器的委托方法中,我将我的地图设置如下:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    [locationManager stopUpdatingLocation];
    //CLLocationCoordinate2D newCoord = { newLocation.coordinate.latitude, newLocation.coordinate.longitude };

    CLLocationCoordinate2D centerPoint = { newLocation.coordinate.latitude, newLocation.coordinate.longitude };
    MKCoordinateSpan coordinateSpan = MKCoordinateSpanMake(0.01, 0.01);
    MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(centerPoint, coordinateSpan);

    ["your-map-name" setRegion:coordinateRegion];
    ["your-map-name" regionThatFits:coordinateRegion];

CLLocationCoordinate2D cordPoint = { latitude_value, longitude_value };
            MyLocation *annotation = [[MyLocation alloc] initWithName:@"title of the pin" address:@"some sub string" coordinate:cordPoint] ;
            ["your-map-name" addAnnotation:annotation]; }

这就是我解决注释和地图问题的方法..

我希望这有帮助..

于 2012-05-07T13:48:57.840 回答