无法在Mapbox V10中移动注释

发布于 2025-02-11 23:47:45 字数 2906 浏览 1 评论 0原文

我一直在使用Mapbox V6,但是我从Mapbox收到了邮件,以升级到V10以获取最新更新。

我的应用程序使用MAPBOX显示自定义地图并为多个用户显示实时位置。 在V6中,我们已经实现了自定义注释并从此mapView(_ mapView:mglmapview,view for enteration:mglannotation:mglannotation)获取自定义注释视图 - > mglannotationView?方法。然后,我们正在更改andotationView.coorcordecortion工作正常,我们的地图能够显示每个用户的实时位置数据。

在新的V10 SDK中,我尝试显示具有修复位置且无法更改的pointAnnotation。要更改PointAnnotation的位置,我们需要从pointAnnotationManager中删除该注释,然后使用更新的位置添加新的注释,这似乎很奇怪。

我还尝试使用V10使用自定义视图,并基于viewAnnotationOptions添加了注释视图,我无法在地图上看到。

请在此处查看我的代码(我看不到注释视图)

private func annotation(at location: CLLocation, forDevice device: String) -> UIView? {
        guard let mapView = self.mapView else { return nil }
        
        let options = ViewAnnotationOptions(
            geometry: Point(location.coordinate),
            width: 100,
            height: 40,
            associatedFeatureId: device,
            allowOverlap: false,
            anchor: .center
        )
        
        guard let userMarker = mapView.viewAnnotations.view(forFeatureId: device) {
            let sampleView = createSampleView(withText: "Hello world!")
            // Added annotation view when it does not exist
            try? mapView.viewAnnotations.add(sampleView, options: options)
            return sampleView
        }

        // Update Annotation's position if already added
        try? mapView.viewAnnotations.update(userMarker, options: options)
        return userMarker
    }

在对上述代码进行了一些修改之后,它努力在地图上显示注释视图,但随机将其随机显示生成多个注释视图。

private func annotation(at location: CLLocation, forDevice device: String) -> UIView? {
        guard let mapView = self.mapView else { return nil }
        
        let options = ViewAnnotationOptions(
            geometry: Point(location.coordinate),
            width: 100,
            height: 40,
            associatedFeatureId: device,
            allowOverlap: false,
            anchor: .center
        )
        
        if let userMarker = mapView.viewAnnotations.view(forFeatureId: device) {
            // Removed annotation if already exists
            try? mapView.viewAnnotations.remove(userMarker)
        }
        
        // Created new annotation and added again to the map.
        // This seems quite weird as we need to add/remove annotation views just to change location of any annotation.
        let sampleView = createSampleView(withText: "Hello world!")
        try? mapView.viewAnnotations.add(sampleView, options: options)
        //try? mapView.viewAnnotations.update(userMarker, options: options)
        return sampleView
    }

我很难理解为什么在较新的SDK中,改变注释的位置如此之困难,这本来应该很容易。

请建议我一些最佳解决方案,以更新地图上的注释视图的位置。我认为,为多次注释添加/删除将是一个昂贵的操作。

注意:我不想在地图上显示当前用户的位置,而需要在任何用户移动时都需要更新地图上的其他用户位置和这些注释的位置。

I have been using Mapbox v6 but I got mail from Mapbox to upgrade to v10 for the latest updates.

My app uses Mapbox to show custom maps and show live locations for multiple users.
In V6, we have implemented custom Annotations and getting custom annotation view from this mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? method. And then we were changing annotationView.coordinate which was working fine and our map was able to show live location data for every user.

In new v10 SDK, I tried to show PointAnnotation which has fix locations and unable to change. To change location for PointAnnotation, we need to remove that annotation from pointAnnotationManager and then add new annotation with updated location which seems quite weird.

I have also tried to use custom views with V10 and added annotation views based on ViewAnnotationOptions which I am unable to see on my map.

Please check my code here (I am not able to see Annotation View):

private func annotation(at location: CLLocation, forDevice device: String) -> UIView? {
        guard let mapView = self.mapView else { return nil }
        
        let options = ViewAnnotationOptions(
            geometry: Point(location.coordinate),
            width: 100,
            height: 40,
            associatedFeatureId: device,
            allowOverlap: false,
            anchor: .center
        )
        
        guard let userMarker = mapView.viewAnnotations.view(forFeatureId: device) {
            let sampleView = createSampleView(withText: "Hello world!")
            // Added annotation view when it does not exist
            try? mapView.viewAnnotations.add(sampleView, options: options)
            return sampleView
        }

        // Update Annotation's position if already added
        try? mapView.viewAnnotations.update(userMarker, options: options)
        return userMarker
    }

After some modifications to the above code, it worked to show annotation views on the map but randomly it generates multiple annotation views.

private func annotation(at location: CLLocation, forDevice device: String) -> UIView? {
        guard let mapView = self.mapView else { return nil }
        
        let options = ViewAnnotationOptions(
            geometry: Point(location.coordinate),
            width: 100,
            height: 40,
            associatedFeatureId: device,
            allowOverlap: false,
            anchor: .center
        )
        
        if let userMarker = mapView.viewAnnotations.view(forFeatureId: device) {
            // Removed annotation if already exists
            try? mapView.viewAnnotations.remove(userMarker)
        }
        
        // Created new annotation and added again to the map.
        // This seems quite weird as we need to add/remove annotation views just to change location of any annotation.
        let sampleView = createSampleView(withText: "Hello world!")
        try? mapView.viewAnnotations.add(sampleView, options: options)
        //try? mapView.viewAnnotations.update(userMarker, options: options)
        return sampleView
    }

It seems very difficult for me to understand why changing location of annotations is so hard in newer SDK which should have been very easy.

Please suggest me some best solution to update annotation view's locations on the map. I believe adding/removing for multiple annotations will be a costly operation.

NOTE: I am not trying to show current user's location on the map but need to show other users' locations on the map and locations for these annotations need to be updated as any user moves.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸短情长 2025-02-18 23:47:45

AssocitfeatureId用于将视图注释连接到地图上的特定功能:

视图注释没有明确绑定到任何源,但是ViewAnnotationOptions.sassociatedFeatureId可用于将给定的视图注释与某些特征结合。识别符表示视图注释的可见性将由给定特征的可见性驱动。

docs

能够更新没有功能ID的视图注释,您需要维护设备< - gt;视图映射。

    private var viewsForDevices = [String: UIView]()

    private func addOrUpdateAnnotationView(at location: CLLocation, forDevice device: String) -> UIView? {
        guard let mapView = self.mapView else { return nil }

        // check if the view for the device already exists
        if let viewAnnotation = viewsForDevices[device] {
            let updateOptions = ViewAnnotationOptions(geometry: Point(location.coordinate)) // update just the location
            try! mapView.viewAnnotations.update(viewAnnotation, options: updateOptions)
            return viewAnnotation
        } else {
            // create an annotation view and add it to the map
            let options = ViewAnnotationOptions(
                geometry: Point(location.coordinate),
                width: 100,
                height: 40,
                allowOverlap: false,
                anchor: .center
            )

            let sampleView = createSampleView(withText: "Hello world!")
            try! mapView.viewAnnotations.add(sampleView, options: options)
            viewsForDevices[device] = view
            return sampleView
        }
    }

The associatedFeatureId is meant to be used to connect view annotations to a specific feature on the map:

View annotations are not explicitly bound to any sources however ViewAnnotationOptions.associatedFeatureId could be used to bind given view annotation with some Feature by Feature.identifier meaning visibility of view annotation will be driven by visibility of given feature.

Docs

In order to be able to update a view annotation that doesn't have feature id associated you'd need to maintain device<->view mapping.

    private var viewsForDevices = [String: UIView]()

    private func addOrUpdateAnnotationView(at location: CLLocation, forDevice device: String) -> UIView? {
        guard let mapView = self.mapView else { return nil }

        // check if the view for the device already exists
        if let viewAnnotation = viewsForDevices[device] {
            let updateOptions = ViewAnnotationOptions(geometry: Point(location.coordinate)) // update just the location
            try! mapView.viewAnnotations.update(viewAnnotation, options: updateOptions)
            return viewAnnotation
        } else {
            // create an annotation view and add it to the map
            let options = ViewAnnotationOptions(
                geometry: Point(location.coordinate),
                width: 100,
                height: 40,
                allowOverlap: false,
                anchor: .center
            )

            let sampleView = createSampleView(withText: "Hello world!")
            try! mapView.viewAnnotations.add(sampleView, options: options)
            viewsForDevices[device] = view
            return sampleView
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文