iphone自定义注释标注显示在不同的位置,并在放大地图时移动

时间:2014-07-01 20:30:19

标签: ios objective-c mapkit mkannotation mkannotationview

开始处理自定义标注。我创建了一个自定义标注,其中包含注释图钉的图像。如果它是一个引脚视图,它显示正确的位置,并且引脚粘在一个坐标上。

但是如果图像注释没有正确显示并且注释正在移动并且显示错误.PLZ检查以下代码并更正我。我坚持不懈地需要解决方案。

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

如果可能,请提供有关自定义标注的更好解决方案。

1 个答案:

答案 0 :(得分:0)

以下代码为我工作

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