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



开始处理自定义标注。我用图像创建了一个自定义callout,而不是注释引脚。当它是一个引脚视图,它显示正确的位置和引脚粘在一个坐标。

但是如果图像注释没有正确显示并且注释正在移动并且它显示错误。请检查下面的代码并纠正我。我被它难住了,我需要解决办法。

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

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

下面的代码为我工作

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

最新更新