2016-09-01 6 views
0

iOS MKMapViewを使用して、画像を地図上のマーカーアイコンとして表示します。問題は、画像が常に画面の左上隅に表示されることです。私が画面に触れるか地図を少し動かすと、画像は正しいGPS位置に正しく表示されます。 カスタム画像を使用しない場合、正しい位置にピン/マーカーが正しく表示されます。ここiOS MKMapViewカスタム画像が左上隅に表示されます

コードは次のとおりです。

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 
} 

func createCustomIconView(postAnnotation:PostAnnotation) -> CustomAnnotationView 
{ 
    let w = (UIScreen.mainScreen().bounds.size.width)/8 
    let h = w 
    let customIconView = CustomAnnotationView(frame: CGRectMake(0, 0, w, h)) 
    customIconView.imageView.image = UIImage(imageLiteral: postAnnotation.picName!) 
    customIconView.annotation = postAnnotation 
    return customIconView 
} 
+0

からインスピレーションを得ている=私はview.translatesAutoresizingMaskIntoConstraintsを削除した後、問題が修正され、偽

を信じられないかもしれませんが、あなたが使用してみました他の初期化メソッド? CustomAnnotationView(フレーム:CGRectMake(0、0、w、h))ではなく、 'CustomAnnotationView(注釈:postAnnotation、reuseIdentifier:" customAnnotationView ")のようなものですか? – Losiowaty

+0

あなたの提案をありがとう、私は以下の答えを投稿しました。 – cooltch

答えて

関連する問題