2017-01-22 5 views
1

私はユーザーの場所の注釈の外観を変更する予定です。 MGLUserLocationAnnotationViewを使用してこれが可能になったと私は理解していますが、これを実装する方法は不明です。これはどのようにして簡単な例を提供できますか?Swift 3 - Mapbox - ユーザーの場所の注釈をカスタマイズする

ありがとうございました

+0

ん[この質問](http://stackoverflow.com/q/40279217/1072229)助けて? –

+0

私はもっと具体的な例を探していました。 – JordanW

+0

その答えは、例「MGLFaux3DUserLocationAnnotationView」にリンクしました。 '.h'と' .m'ファイルは完全に実装されているようです。 –

答えて

7

あなたは正しいです。 MapBox API MGLUserLocationAnnotationViewの説明は非常に短いです。ユーザーの場所の表示のカスタマイズは、MapBox iOS SDK 3.4.0以降で利用できます。 See also the feature comments on the MapBox GitHub

注意することは重要です。MGLUserLocationAnnotationViewは、MGLAnnotationViewのサブクラスです。つまり、MGLUserLocationAnnotationViewは通常の注釈ビューと同じように機能します。

ここでは、ユーザーの場所の表示をカスタマイズする方法の例を示します。新しいクラス(例:CustomUserLocationAnnotationView)を作成し、layoutSubviews()をオーバーライドしてカスタムコードを追加します。この例では、UIImage UserLocationIcon.pngを使用して、マップ上のユーザーの位置を視覚化します。 と-mapView:あなたのUIViewControllerまたは任意の他(例えば、サービスクラス)は、少なくとも二つの機能-mapViewDidFinishLoadingMapでMGLMapViewDelegateを実装するには

import Foundation 
import UIKit 
import Mapbox 

final class CustomUserLocationAnnotationView: MGLUserLocationAnnotationView { 
    override init(reuseIdentifier: String?) { 
     super.init(reuseIdentifier: reuseIdentifier) 
    } 

    override init(frame: CGRect) { 
     super.init(frame: CGRect(x: 0, y: 0, width: 30, height: 30)) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 

     // Force the annotation view to maintain a constant size when the map is tilted. 
     scalesWithViewingDistance = false 

     layer.contentsScale = UIScreen.main.scale 
     layer.contentsGravity = kCAGravityCenter 

     // Use your image here 
     layer.contents = UIImage(named: "UserLocationIcon")?.cgImage 
    } 
} 

viewForAnnotation :. viewForAnnotation:

extension ViewController: MGLMapViewDelegate { 
    // Wait until the map is loaded before proceed with other actions on map 
    func mapViewDidFinishLoadingMap(_ mapView: MGLMapView) { 
     // Show the user location here 
     mapView.showsUserLocation = true 
    } 

    func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? { 
     // Customise the user location annotation view 
     if annotation is MGLUserLocation { 
      var userLocationAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "CustomUserLocationAnnotationViewIdentifier") as? CustomUserLocationAnnotationView 

      if userLocationAnnotationView == nil { 
       userLocationAnnotationView = CustomUserLocationAnnotationView(reuseIdentifier: "CustomUserLocationAnnotationViewIdentifier") 
      } 

      // Optional: You can save the annotation object for later use in your app 
      self.userLocationAnnotation = annotation 

      return userLocationAnnotationView 
     } 

     // Customise your annotation view here... 

     return customAnnotationView 
    } 
} 

のMapView -mapView:私のコメントをインラインで参照してください委任機能は、ユーザの注釈ビューを含むすべての注釈ビューインスタンスのために呼ばれています。

オプション:あなたCustomUserLocationAnnotationViewのインスタンスを取得するには、あなたのコード内のどこでも、この機能を使用することができます。

// The userLocationAnnotation was previously saved in your ViewController in the -mapView:viewForAnnotation: delegate function 
    let view = mapView.view(for: self.userLocationAnnotation) as? CustomUserLocationAnnotationView 
+0

これは変だけど、ユーザートラッキングを開始するときに注釈のビューが呼び出されることはありません。 – huggie

+0

@huggie私も同じ問題があった、 'viewForAnnotation'は呼び出されませんでした。ストーリーボードで 'mapView'を設定したので、' ShowUserLocation'を 'On'に設定していたことがわかりました。 'Default'に設定すると、私の' viewForAnnotation'が呼び出されました。コードで 'mapView.showsUserLocation'を使うとokです。 – Andrej

+0

@Andrejありがとう、これは私の問題を解決しました!また、 'layoutSubviews()'を呼び出すために 'CustomUserLocationAnnotationView'で' setNeedLayout() 'を呼び出さなければなりませんでした。 – huggie

関連する問題