gpt4 book ai didi

ios - MKPinAnnotationView 未居中

转载 作者:可可西里 更新时间:2023-11-01 06:20:37 27 4
gpt4 key购买 nike

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

只是要补充一点,我还注​​意到使用新的图钉图像时,单击图钉时不会弹出任何内容。以前,使用默认图钉时,单击图钉后会出现一个文本气泡。既然是这种情况,我想包含在 map 上制作和放置图钉的方法的代码,

-(void) createAndAddAnnotationForCoordinate : (CLLocationCoordinate2D)coordinate{

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

[mapView addAnnotation:annotation];


}

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

Picture of the pin

Picture of the pin relative to the route, polyline, and the circle

最佳答案

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

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

其次,设置 centerOffset,这样本地图缩放时,图像中“指向”坐标的部分会一直指向坐标。这是因为 centerOffset 在屏幕 CGPoint 中并且不随 map 的缩放级别缩放。如果 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;
}

enter image description here

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


以下是代码和生成的外观设置了 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;
}

enter image description here

关于ios - MKPinAnnotationView 未居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29197476/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com