【问题标题】:Custom MGLAnnotationView using Mapbox for IOS使用 Mapbox for IOS 自定义 MGLAnnotationView
【发布时间】:2020-06-09 23:47:12
【问题描述】:

我很困惑为什么这会显示默认图像而不是纽约上空的蓝色圆形圆圈。对此以及何时使用默认图像的任何见解将不胜感激。

import UIKit
import Mapbox

class ViewController: UIViewController, MGLMapViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupMapview()
    }

    func setupMapview(){
        let mapView = MGLMapView(frame: view.bounds)
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        mapView.setCenter(CLLocationCoordinate2D(latitude: 40.74699, longitude: -73.98742), zoomLevel: 9, animated: false)
        view.addSubview(mapView)

        let annotation = MGLPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2D(latitude: 40.77014, longitude: -73.97480)
        mapView.addAnnotation(annotation)

        mapView.delegate = self
    }


    func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
        print("CORDINATE")
        print(annotation.coordinate)
        if annotation is MGLPointAnnotation {
            print("SET\n\n\n")
            let av = RoundedAnnotationView(annotation: annotation, reuseIdentifier: "ResuseIdentifier")
            av.configure()
            return av
        }
        return nil
    }
}

class RoundedAnnotationView: MGLAnnotationView{
    func configure(){
        backgroundColor = .blue
        layer.cornerRadius = 24
        clipsToBounds = true
    }
}

输出: iPhone_Screen print_statements

【问题讨论】:

  • 你确定你没有错字吗? reuseIdentifier: "ResuseIdentifier" 看起来应该是 reuseIdentifier: "ReuseIdentifier"

标签: ios swift mapbox mapbox-ios


【解决方案1】:

标准默认注释显示在纽约,因为这正是您在setupMapview 中添加到地图的内容。如果你想让地图显示用户的位置,你必须告诉它这样做:

mapView.addAnnotation(annotation)

mapView.showsUserLocation = true // This needs to be set explicitly.

mapView.delegate = self

像往常一样,当您想要访问用户的位置时,您必须通过在info.plist 中插入正确的标志来请求许可:

Privacy - Location When In Use Usage Description

还有一些解释性的字符串:

"We'd like to track you with our satellite."

如果您在模拟器上运行您的应用,您可以创建自定义位置:

模拟器 -> 功能 -> 位置 -> 自定义位置...

【讨论】:

  • 谢谢,但我试图获得一个字面上的蓝色圆圈 AnnotationView,而不是位置。我发现了它为什么会这样,并将链接附加到解决方案
【解决方案2】:

应在地图完全加载后添加注释。我有更详细的分步解决方案:https://github.com/mapbox/mapbox-gl-native/issues/16492

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多