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

}

MKAnnotationViewメソッドは、マップにアイコンを表示するために使用されます。Objective-Cを使用してメソッドでMKAnnotationViewメソッドを呼び出す方法は-didUpdateToLocation?

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 に答える