0

xml ファイルから緯度と経度を読み取ったので、注釈を追加したいと思います。しかし、ユーザーの場所を除いて、それらのどれも追加されていません.mapview.annotationsの数は常に空です.ここに私のコードがあります:

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if (newLocation != nil) {
        self.myLocation = newLocation;
    }


    mapView.region = MKCoordinateRegionMake(self.myLocation.coordinate, MKCoordinateSpanMake(0.005f, 0.005f));
    mapView.zoomEnabled = YES;
    //mapView.showsUserLocation = YES;
    mapView.centerCoordinate = self.myLocation.coordinate;


    MKPointAnnotation *userLocation = [[MKPointAnnotation alloc] init];
    userLocation.coordinate = myLocation.coordinate;
    userLocation.title = @"my position";
    userLocation.subtitle = @"my position now";
    [mapView removeAnnotations:mapView.annotations];

    [mapView addAnnotation:userLocation];
    [userLocation release];

    if ([self.positionArray count] < 5) {
        NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"position" ofType:@"xml"];
        NSData *positionData = [[NSData alloc] initWithContentsOfFile:dataPath];

        NSXMLParser *parser = [[NSXMLParser alloc] initWithData:positionData];

        parser.delegate = self;

        [parser parse];
    }

    if (self.myLocation != nil) {
        [self.manager stopUpdatingLocation];
    }


}       

MapInfo クラスは次のとおりです。

    #import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface MapInfo : NSObject<MKAnnotation>
{
    NSString *subtitle;
    NSString *title;
    CLLocationCoordinate2D coordinate;
}

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

- (id)initWithCoordinate:(CLLocationCoordinate2D)location;
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;

@end

次に、ここにデリゲートメソッドがあります

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    static NSString *AnnotationIdentifier = @"AnnotationIdentifer";
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if ([annotation isKindOfClass:[MapInfo class]]) {
        if (pinView == nil) {
            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
        }
        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = NO;
    }else if([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        if (pinView == nil) {
            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
        }
        pinView.pinColor = MKPinAnnotationColorPurple;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = NO;
    }
    return pinView;
}

なぜユーザーの場所だけを追加できるのか本当にわかりません。誰か助けてください!!数日間悩まされていました!

4

1 に答える 1

0

mapView のデリゲートを設定するのを忘れているようです。

于 2013-02-17T20:13:52.337 に答える