iOS MapKit Annotation, not showing correct location

拜拜、爱过 提交于 2019-12-04 14:05:21

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.

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!