iOS MapKit 注释,未显示正确的位置
我使用自定义图像作为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在设置注释视图的
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
, thecenterOffset
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 setcenterOffset
. Or just try commenting out the setting ofcenterOffset
--maybe you don't need it.Some other unrelated items:
pprMapNote
alloc+init (add an autorelease)dequeueReusableAnnotationViewWithIdentifier
to allow for annotation view re-use.addTarget
to call your own method for the callout button press, it's much better to use the map view's own delegate methodcalloutAccessoryControlTapped
See this answer for an example of the above three points.
Pin 是在单独的视图中绘制的,因此它不会根据视图的状态进行缩放。
您必须手动设置自定义 Pin 图的大小。使用 centerOffset 可以轻松完成此操作。对于大多数情况,将框架的高度设置为图像大小的一半就足够了。图像完全填充在框架中,因此您可以轻松使用此框架尺寸(高度)。
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).