【问题标题】:MKPinAnnotationView not centeredMKPinAnnotationView 未居中
【发布时间】:2015-05-25 16:32:15
【问题描述】:

为 MKPinAnnotationView 上传自定义图像后,我注意到图钉偏离了中心。大头针应该在路线折线上的一点上,并且在 mkcircle 的中心;但是,大头针似乎位于折线的右侧,中心偏北一点。我尝试使用 centerOffset 属性进行试验,但是当我将值插入该属性时,似乎没有任何变化。这是代码,

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

static NSString *viewID = @"MKPinAnnotationView";
MKPinAnnotationView *annotationView = (MKPinAnnotationView*)
[self.mapView dequeueReusableAnnotationViewWithIdentifier:viewID];

if(annotationView ==nil){
    annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:viewID];
}


annotationView.image = [UIImage imageNamed:@"Pin.png"];
annotationView.enabled = YES;

//doesn't move the pin, still offcentered
annotationView.centerOffset = CGPointMake(0,-50);
return annotationView;
 }

只是要添加一些东西,我还注意到,使用新的 pin 图像,当我单击 pin 时没有弹出任何内容。以前,使用默认图钉,单击图钉后会出现一个文本气泡。既然是这种情况,我想包含在地图上制作和放置图钉的方法的代码,

-(void) createAndAddAnnotationForCoordinate : (CLLocationCoordinate2D)coordinate{

MKPointAnnotation* annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = coordinate;
annotation.title = @"This is a pin!";

[mapView addAnnotation:annotation];


}

我还尝试更改图钉图像,看看这是否会影响 MKPinAnnotationView 的定位。虽然我可以通过编辑图像使图钉居中,但它并没有为其他折线居中。任何帮助将不胜感激!

【问题讨论】:

  • 如果您想使用自定义图像,请创建一个普通的 MKAnnotationView(不是 MKPinAnnotatioView)。这也可以解决居中问题。要在点击时激活标注,请执行 annotationView.canShowCallout = YES。
  • 感谢您回复安娜。将其更改为 MKAnnotationView 确实解决了标注问题,但注释仍然偏离中心。我尝试使用 centeroffSet 和 center 属性,但是当我弄乱它们时,注释似乎没有移动。
  • 请注意,centerOffset 不随地图的缩放级别缩放。这是一个固定值,因此您必须对其进行设置,以使图像中应该与实际坐标保持一致的点保持这种状态。将其更改为 MKAnnotationView 与标注无关(设置 canShowCallout 做到了)。
  • 我不确定我是否阅读正确,但是我无法移动注释吗?如果有帮助的话,注释就在它所在的 mkcircle 中心的右侧一点。
  • 请编辑问题并发布 Pin.png 以及注释在地图上相对于圆圈的外观的小屏幕截图。

标签: ios objective-c mkmapview mkannotationview mkpinannotationview


【解决方案1】:

你必须设置 MKCoordinateRegion 来加载地图,编辑你的 createAndAddAnnotationForCoordinate 方法如下

-(void) createAndAddAnnotationForCoordinate : (CLLocationCoordinate2D)coordinate{

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(coordinate, 3000, 3000); //Set zooming level
    MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion]; //add location to map
    [mapView setRegion:adjustedRegion animated:YES]; // create animation zooming

MKPointAnnotation* annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = coordinate;
annotation.title = @"This is a pin!";

[mapView addAnnotation:annotation];


}

【讨论】:

  • 感谢您的回复。 MKCoordinateRegion 正在成功加载,您添加的代码已经在我的 viewdidLoad 方法中。该图钉仍然偏离中心,并且标题未显示。但是,如果我摆脱了被覆盖的 viewForAnnotation 方法,那么图钉(带有默认图像)就会以弹出的标题为中心。
  • 在viewForAnnotation方法返回annotationView之前试试这段代码,annotationView.center = CGPointMake(annotationView.bounds.size.width*0.1f, -annotationView.bounds.size.height*0.5f);跨度>
  • 我添加了代码行并且图钉保持偏离中心。代码似乎没有移动图钉。
  • 然后试试这个,UIImage *pin = [UIImage imageNamed:@"Pin.png"]; annotationView.center = CGPointMake(pin.size.width*0.1f, -pin.size.height*0.5f);
  • 很遗憾,附加代码对图钉的位置没有影响。
【解决方案2】:

首先,重要的一点是,在使用自定义注释图像时,最好使用普通的MKAnnotationView 类,而不是其旨在自动显示标准图钉图像的子类MKPinAnnotationView

这是因为MKPinAnnotationView 包含一些基于其自己的 pin 图像的注释视图框架和 centerOffset 的内置调整。在某些情况下,您的自定义图像甚至会在屏幕上替换为默认图钉图像。因此,即使 MKPinAnnotationView 具有 image 属性,该类也不会总是按预期使用它。

其次,设置centerOffset,使得随着地图的缩放,图像中“指向”坐标的部分一直指向坐标。这是因为centerOffset 位于屏幕CGPoints 中,并且不随地图的缩放级别缩放。如果centerOffset设置不正确,图像的“点”就会开始偏离目标坐标。

另外请注意,您甚至可能不需要设置centerOffset,因为默认情况下会将图像的中心放置在您可以接受的坐标处。


根据您发布的图片,这里是代码和结果外观没有设置centerOffset(保留默认值)

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

    static NSString *viewID = @"MKPinAnnotationView";

    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:viewID];

    if (annotationView ==nil) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:viewID];
        annotationView.image = [UIImage imageNamed:@"Pin.png"];
        annotationView.canShowCallout = YES;
    }
    else {
        annotationView.annotation = annotation;
    }

    return annotationView;
}

(我添加了红色中心线以显示目标坐标相对于图钉图像的位置。)


这是代码和生成的外观设置了centerOffset,以便底部指向坐标

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

    static NSString *viewID = @"MKPinAnnotationView";

    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:viewID];

    if (annotationView ==nil) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:viewID];
        annotationView.image = [UIImage imageNamed:@"Pin.png"];
        annotationView.canShowCallout = YES;
        annotationView.centerOffset = CGPointMake(0,-15);
    }
    else {
        annotationView.annotation = annotation;
    }

    return annotationView;
}

【讨论】:

  • 感谢您结构合理的回答!我更改了代码,但 centerOffset 似乎没有做任何事情。我对为什么 centerOffset 属性不起作用感到困惑。大头针仍然在完全相同的位置。
  • 我知道已经过去了几天,但我仍然无法找到解决方案。当我为 centeroffset 属性输入任何值时,似乎没有任何变化。请帮忙!
  • 这很奇怪,因为 calloutOffset 正在工作但不是 centerOffset
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-22
  • 2012-05-16
  • 1970-01-01
  • 2012-11-07
  • 1970-01-01
  • 2013-01-15
  • 1970-01-01
相关资源
最近更新 更多