iOS MapKit 注释,未显示正确的位置

发布于 2024-12-05 07:47:29 字数 1398 浏览 1 评论 0原文

我使用自定义图像作为我的 Mapkit 注释。但我在使用自定义图像时遇到的主要问题是,缩小时,注释不在地图上的正确位置,只有直到我一直向下放大,它才会显示注释点位于正确的位置。看来,当我使用常规引脚 MKPinAnnotationView 时,它可以正常工作,因为引脚位于放大或缩小的正确位置,提前感谢任何可以提供帮助的人。

我使用的代码如下:

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

NSLog(@"welcome into the map view annotation");

if ([annotation isKindOfClass:[MKUserLocation class]])

return nil;

MKAnnotationView *pprMapNote = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pprMapNote"];


pprMapNote.image = [UIImage imageNamed:[NSString stringWithFormat:@"GPS_note.png"]];

pprMapNote.canShowCallout = YES;
pprMapNote.centerOffset = CGPointMake(-21,-60);
pprMapNote.calloutOffset = CGPointMake(0, 0);
//[pprMapNote addSubview:pprMapNoteImg];

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
                action:@selector(showDetail)
      forControlEvents:UIControlEventTouchUpInside];
pprMapNote.rightCalloutAccessoryView = rightButton;

//remember to write in conditional for the different icons that should be loaded based on location

UIImageView *pprNoteLocIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loc_icon_casino.png"]];
pprMapNote.leftCalloutAccessoryView = pprNoteLocIcon;
[pprNoteLocIcon release];

return pprMapNote;

}

I using a custom image for my mapkit annotation. But the main problem it seems that I am running into in using a custom image, is that when zoomed out, the annotation is not in the correct point on the map, and ONLY until I zoom in all the way down, will it show the annotation point in the correct place. It seems that when I use a regular pin MKPinAnnotationView, it works normally, as with the pin being in the correct place zoomed in or out, thanks in advance for anyone that can help.

The code I have used is as follows:

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

NSLog(@"welcome into the map view annotation");

if ([annotation isKindOfClass:[MKUserLocation class]])

return nil;

MKAnnotationView *pprMapNote = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pprMapNote"];


pprMapNote.image = [UIImage imageNamed:[NSString stringWithFormat:@"GPS_note.png"]];

pprMapNote.canShowCallout = YES;
pprMapNote.centerOffset = CGPointMake(-21,-60);
pprMapNote.calloutOffset = CGPointMake(0, 0);
//[pprMapNote addSubview:pprMapNoteImg];

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
                action:@selector(showDetail)
      forControlEvents:UIControlEventTouchUpInside];
pprMapNote.rightCalloutAccessoryView = rightButton;

//remember to write in conditional for the different icons that should be loaded based on location

UIImageView *pprNoteLocIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loc_icon_casino.png"]];
pprMapNote.leftCalloutAccessoryView = pprNoteLocIcon;
[pprNoteLocIcon release];

return pprMapNote;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天生の放荡 2024-12-12 07:47:29

您正在设置注释视图的centerOffset

请注意,此偏移随缩放级别缩放。缩小得越远,图像距离坐标就越远。

在默认的 MKPinAnnotationView 中,centerOffset 保留默认值 0,0,并且引脚图像的设计使得引脚的底点位于坐标上。因此,当您进一步缩小时,图钉图像似乎相对于其下方的地图有所增长,但图钉的底部仍然指向坐标。

您需要根据您的图像调整 centerOffset 或修改您的图像,以便不需要设置 centerOffset。或者只是尝试注释掉 centerOffset 的设置——也许您不需要它。

其他一些不相关的项目:

  • pprMapNote alloc+init 存在内存泄漏(添加自动释放)
  • 您应该使用 dequeueReusableAnnotationViewWithIdentifier 来允许注释视图重用。
  • 与其使用 addTarget 调用您自己的方法来按下标注按钮,不如使用地图视图自己的委托方法 calloutAccessoryControlTapped

请参阅 这个回答以获取上述三点的示例。

You are setting the centerOffset of the annotation view.

Note that this offset is not scaled with the zoom level. The further you zoom out, the further the image will appear from the coordinate.

In the default MKPinAnnotationView, the centerOffset is left at the default of 0,0 and the pin image is designed such that the bottom point of the pin is on the coordinate. So as you zoom further out, the pin image seems to grow relative to the map under it but the bottom of the pin is still pointing to the coordinate.

You need to either adjust the centerOffset based on your image or modify your image so you don't need to set centerOffset. Or just try commenting out the setting of centerOffset--maybe you don't need it.

Some other unrelated items:

  • You have a memory leak for the pprMapNote alloc+init (add an autorelease)
  • You should be using dequeueReusableAnnotationViewWithIdentifier to allow for annotation view re-use.
  • Instead of using addTarget to call your own method for the callout button press, it's much better to use the map view's own delegate method calloutAccessoryControlTapped

See this answer for an example of the above three points.

羁客 2024-12-12 07:47:29

Pin 是在单独的视图中绘制的,因此它不会根据视图的状态进行缩放。

您必须手动设置自定义 Pin 图的大小。使用 centerOffset 可以轻松完成此操作。对于大多数情况,将框架的高度设置为图像大小的一半就足够了。图像完全填充在框架中,因此您可以轻松使用此框架尺寸(高度)。

  aView.image = [UIImage imageNamed ... ];
  aView.centerOffset = CGPointMake(0,-aView.frame.size.height*0.5);

The Pin is drawn in a separate view, so it will not zoom based on the status of your view.

You have to set the size of your custom Pin image manually. This can easily be done using the centerOffset. For most cases it is enough to set the height of the frame to half the size of the image. The image is fully filled in the frame, so you can easily use this frame size(height).

  aView.image = [UIImage imageNamed ... ];
  aView.centerOffset = CGPointMake(0,-aView.frame.size.height*0.5);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文