Выноска пользовательской аннотации iphone отображается в другом месте и перемещается при увеличении карты

начал работать над пользовательской выноской. Я создал одну пользовательскую выноску с изображением, вставленным в аннотацию. когда, если это вид булавки, он показывает правильное местоположение и булавку, привязанную к одной координате.

Но в случае, если аннотация изображения не отображается должным образом, а аннотация перемещается и отображается неправильно. Пожалуйста, проверьте приведенный ниже код и исправьте меня. Я застрял с этим очень плохо, мне нужно решение.

- (void)viewDidLoad {
    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc]init];
    locationManager.delegate = self;

    self.map_View.delegate = self;

    [self createCustomAnnotation];
}

-(Void) createCustomAnnotation {
    CLLocationCoordinate2D locationCoordinate;
    locationCoordinate.latitude=37.300275;
    locationCoordinate.longitude=-121.640625;

    MKCoordinateSpan span;
    span.latitudeDelta = 10;
    span.longitudeDelta = 10;

    MKCoordinateRegion region;
    region.center = locationCoordinate;
    region.span =span;

    [self.map_View setRegion:region];

    MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
    pointAnnotation.title = @"a";
    pointAnnotation.coordinate = locationCoordinate;

    [self.map_View addAnnotation:pointAnnotation];
    [self.map_View selectAnnotation:pointAnnotation animated:YES];
}

- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id      <MKAnnotation>)annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }

    static NSString *google = @"Pin";

    MKAnnotationView *annotationView = [_map_View dequeueReusableAnnotationViewWithIdentifier:google];

    if (!annotationView) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:google];
        annotationView.image = [UIImage imageNamed:@"m.png"];
        annotationView. canShowCallout = YES;

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(-20, -25, 70, 30)];
        label.backgroundColor = [UIColor greenColor];
        label.textColor = [UIColor blackColor];
        label.text = @"DD";
        label. textAlignment = NSTextAlignmentCenter;

        [annotationView addSubview:labl];
    }
    else {
        // unrelated but should handle view re-use...
        annotationView.annotation = annotation;

        UILabel *label2 = (UILabel *)[annotationView viewWithTag:42];
        label2.text = annotation.title;

    }
    return annotationView;
}

Если возможно, предоставьте мне лучшие решения о пользовательских выносках.


person Pavan Chowdary    schedule 01.07.2014    source источник


Ответы (1)


ниже код работает для меня

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

Static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView* customPinView = [[MKAnnotationView alloc]
                               initWithAnnotation:annotation            reuseIdentifier:AnnotationIdentifier];

 customPinView.image = [UIImage imageNamed:@"m.png"];
customPinView.centerOffset = CGPointMake(0.0f, -24.5(my image height/2)f);

return customPinView;
}
person Pavan Chowdary    schedule 02.07.2014