Swift 3-Mapbox-自定义用户位置注释 [英] Swift 3 - Mapbox - Customize User Location Annotation

查看:216
本文介绍了Swift 3-Mapbox-自定义用户位置注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求更改用户位置注释的外观.我知道现在可以使用MGLUserLocationAnnotationView来实现,但是我不确定如何实现.谁能提供一个简单的示例说明如何完成此操作?

I'm Looking to change the appearance of the users location annotation. I understand this is now possible using MGLUserLocationAnnotationView, however, I'm unsure how to implement this. Can anyone provide a simple example of how this is done?

谢谢

推荐答案

您是对的. MapBox API MGLUserLocationAnnotationView 的描述非常简短.从MapBox iOS SDK 3.4.0开始,可以自定义用户位置视图. 另请参见MapBox GitHub上的功能注释

You are right. The MapBox API MGLUserLocationAnnotationView description is very short. The user location view customisation is available since MapBox iOS SDK 3.4.0. See also the feature comments on the MapBox GitHub

重要的是要注意:MGLUserLocationAnnotationView是MGLAnnotationView的子类.这意味着MGLUserLocationAnnotationView的行为就像普通的注释视图一样.

It is important to note: The MGLUserLocationAnnotationView is a subclass of the MGLAnnotationView. It means the MGLUserLocationAnnotationView acts just like a normal annotation view.

这是一个如何自定义用户位置视图的示例.创建一个新类(例如CustomUserLocationAnnotationView)并覆盖layoutSubviews()以添加一个自定义代码.在此示例中,我使用UIImage UserLocationIcon.png可视化用户在地图上的位置.

Here is an example how to customise the user location view. Create a new class (e.g. CustomUserLocationAnnotationView) and override a layoutSubviews() to add a custom code. In this example I use an UIImage UserLocationIcon.png to visualise the user position on the map.

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
    }
}

在您的UIViewController或其他任何类(例如Service类)中,使用至少两个函数-mapViewDidFinishLoadingMap实现MGLMapViewDelegate: 和-mapView:viewForAnnotation:.内联查看我的评论:

In your UIViewController or whatever else (e.g. Service class) implement the MGLMapViewDelegate with at least two functions -mapViewDidFinishLoadingMap: and -mapView:viewForAnnotation:. See my comments inline:

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:viewForAnnotation:委托函数.

The MapView -mapView:viewForAnnotation: delegate function is called for every annotation view instance including the user annotation view.

可选:要获取CustomUserLocationAnnotationView实例,可以在代码中的任何地方使用此功能:

Optionally: To get your CustomUserLocationAnnotationView instance, you can use this function everywhere in your code:

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

这篇关于Swift 3-Mapbox-自定义用户位置注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆