1

iOSで開発を始めたばかりです。問題が些細なことのように思われる場合は、申し訳ありません。

API から SwiftyJSON を介していくつかのマーカー/注釈を読み込み、それらを Mapbox MapView に表示します。注釈をクリックすると、コールアウトがトリガーされます。-これはうまく機能し、地獄のように簡単です。問題: mapView をマーカー/注釈とコールアウトの中央に配置するか、カメラを移動してズームインして中央に表示したいと思います。次のようなものを使用するときはいつでも:

    let camera = MGLMapCamera(lookingAtCenterCoordinate: annotation.coordinate, fromDistance: 1000, pitch: 0, heading: 0)
    mapView.setCamera(camera, withDuration: 2, animationTimingFunction: CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))

また

    mapView.setCenterCoordinate(annotation.coordinate, zoomLevel: 15, animated: true)

吹き出しは同じ位置にとどまります。私が達成したいのは、注釈/マーカーの上にスティッキーで表示されることです。

これは、この部分の完全なコードです (viewdidload での注釈の構築は含まれていません:

func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {
    var annotationImage = mapView.dequeueReusableAnnotationImageWithIdentifier("marker_live")

    if annotationImage == nil {
        // Leaning Tower of Pisa by Stefan Spieler from the Noun Project
        var image = UIImage(named: "marker_live")!
        image = image.imageWithAlignmentRectInsets(UIEdgeInsetsMake(0, 0, image.size.height/2, 0))

        annotationImage = MGLAnnotationImage(image: image, reuseIdentifier: "marker_live")
    }

    return annotationImage
}


func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
    print("tap on annotation")
    mapView.selectAnnotation(annotation, animated: true)

    return true
}

func mapView(mapView: MGLMapView, calloutViewForAnnotation annotation: MGLAnnotation) -> UIView? {

    return CustomCalloutView(representedObject: annotation)

}

func mapView(mapView: MGLMapView, tapOnCalloutForAnnotation annotation: MGLAnnotation) {
    print("Tapped the callout for: \(annotation.title!)")
    self.performSegueWithIdentifier("PlaylistFromMap", sender: annotation.title!)
    mapView.deselectAnnotation(annotation, animated: true)
}

完了ハンドラの使用も考えましたが、その仕組みがわかりませんでした。大変助かりました!

4

1 に答える 1