我使用 iOS MKMapView 将图像显示为地图上的标记图标。问题是图像总是显示在屏幕的左上角。如果我触摸屏幕或稍微移动地图,图像会在正确的 GPS 位置正确显示。如果我不使用自定义图像,则图钉/标记会正确显示在正确的位置。
这是代码:
func addAnnotationsToMap()
{
self.mapView.removeAnnotations(self.mapView.annotations)
for post in self.posts
{
let gps = CLLocationCoordinate2D(latitude: post.GPS!.latitude!, longitude: post.GPS!.longitude!)
let postPin = PostAnnotation(gps: gps)
self.mapView.addAnnotation(postPin)
}
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
if (annotation is MKUserLocation)
{
return nil
}
let postAnnotation = annotation as! PostAnnotation
let reuseId = "customAnnotationView"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil
{
anView = createCustomIconView(postAnnotation)
}
else
{
anView!.annotation = annotation
}
return anView
} …Run Code Online (Sandbox Code Playgroud)