私はユーザーの場所の注釈の外観を変更する予定です。 MGLUserLocationAnnotationView
を使用してこれが可能になったと私は理解していますが、これを実装する方法は不明です。これはどのようにして簡単な例を提供できますか?Swift 3 - Mapbox - ユーザーの場所の注釈をカスタマイズする
ありがとうございました
私はユーザーの場所の注釈の外観を変更する予定です。 MGLUserLocationAnnotationView
を使用してこれが可能になったと私は理解していますが、これを実装する方法は不明です。これはどのようにして簡単な例を提供できますか?Swift 3 - Mapbox - ユーザーの場所の注釈をカスタマイズする
ありがとうございました
あなたは正しいです。 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
これは変だけど、ユーザートラッキングを開始するときに注釈のビューが呼び出されることはありません。 – huggie
@huggie私も同じ問題があった、 'viewForAnnotation'は呼び出されませんでした。ストーリーボードで 'mapView'を設定したので、' ShowUserLocation'を 'On'に設定していたことがわかりました。 'Default'に設定すると、私の' viewForAnnotation'が呼び出されました。コードで 'mapView.showsUserLocation'を使うとokです。 – Andrej
@Andrejありがとう、これは私の問題を解決しました!また、 'layoutSubviews()'を呼び出すために 'CustomUserLocationAnnotationView'で' setNeedLayout() 'を呼び出さなければなりませんでした。 – huggie
ん[この質問](http://stackoverflow.com/q/40279217/1072229)助けて? –
私はもっと具体的な例を探していました。 – JordanW
その答えは、例「MGLFaux3DUserLocationAnnotationView」にリンクしました。 '.h'と' .m'ファイルは完全に実装されているようです。 –