首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用IOS的Mapbox自定义MGLAnnotationView

使用IOS的Mapbox自定义MGLAnnotationView
EN

Stack Overflow用户
提问于 2020-06-09 06:21:07
回答 2查看 311关注 0票数 0

我很困惑为什么这是显示默认图像,而不是在纽约上空显示一个蓝色圆圈。任何关于这一点的见解,以及何时使用默认图像,都将非常感谢。

代码语言:javascript
运行
复制
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

EN

回答 2

Stack Overflow用户

发布于 2020-06-09 20:15:09

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

代码语言:javascript
运行
复制
mapView.addAnnotation(annotation)

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

mapView.delegate = self

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

代码语言:javascript
运行
复制
Privacy - Location When In Use Usage Description

以及某种解释性字符串:

代码语言:javascript
运行
复制
"We'd like to track you with our satellite."

如果你在模拟器上运行你的应用程序,你可以创建一个自定义的位置:

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

票数 0
EN

Stack Overflow用户

发布于 2020-06-09 23:47:13

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62272090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档