iOS MapKit Annotation,未显示正确的位置 [英] iOS MapKit Annotation, not showing correct location
问题描述
我使用自定义图像进行mapkit注释。但是我在使用自定义图像时遇到的主要问题是,当缩小时,注释不在地图上的正确位置,并且只有在我一直向下放大时,它才会显示注释点在正确的位置。似乎当我使用常规引脚 MKPinAnnotationView 时,它正常工作,因为引脚位于正确的位置放大或缩小,提前感谢任何可以提供帮助的人。
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.
我使用的代码如下:
- (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;
}
推荐答案
你是设置注释视图的 centerOffset
。
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.
在默认 MKPinAnnotationView
中, centerOffset
保留默认值0,0并且引脚图像的设计使得引脚的底点位于坐标上。因此,当你进一步缩小时,引脚图像似乎相对于它下面的地图增长,但引脚的底部仍然指向坐标。
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.
你需要根据您的图像调整 centerOffset
或修改您的图像,这样您就不需要设置 centerOffset
。或者只是尝试评论 centerOffset
的设置 - 也许你不需要它。
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:
-
pprMapNote $ c $有内存泄漏c> alloc + init(添加自动释放)
- 您应该使用
dequeueReusableAnnotationViewWithIdentifier
来允许重复使用注释视图。 - 使用地图视图自己的委托方法<$>而不是使用
addTarget
来调用你自己的方法按下标注按钮<$> c $ c> calloutAccessoryControlTapped
- 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 methodcalloutAccessoryControlTapped
参见这个答案以上三点的例子。
这篇关于iOS MapKit Annotation,未显示正确的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!