繁体   English   中英

iOS Mapbox SDK-如何将MGLPointAnnotation标记添加到地图图层

[英]iOS Mapbox SDK - How to add MGLPointAnnotation markers into a map layer

我需要找到一种将标记从MGLPointAnnotation转换为MGLShapeSource或类似方法的方法,以便将标记添加到地图图层并完全控制例如如何在地图上显示和聚类标记。

我正在使用MapBox SDK v5.2构建iOS应用。 该应用程序在内部生成标记(标题,字幕,坐标和图标图像名称),并且在点击时标记会在地图上显示并带有标注。 使用在创建标记MGLPointAnnotation()并使用添加到地图mapView.addAnnotation()

但是,为了完全控制标记的显示方式(例如,基于缩放级别对标记进行聚类或将其切换为ON / OFF),我需要将标记添加到Map图层,例如使用MGLShapeSource ,然后添加style.addSource()style.addLayer()

问题是我找不到从MGLPointAnnotationMGLShapeSource或类似文件的转换器。 我对此进行了调查,但我能想到的唯一解决方案是将标记信息包含在GeoJSON文件中。 但我想避免这种情况,因为标记是在应用运行时在应用内生成的,而不是从外部只读GeoJSON文件生成的。

有关如何创建单个poi的示例:

let poi1 = MGLPointAnnotation()
         poi1.coordinate = CLLocationCoordinate2D(latitude: 38.788534, longitude: -9.494489)
         poi1.title = "poi1"
         poi1.subtitle = "This is the text for poi1"
         poiTitleImage[poi1.title!] = "icon1"

mapView.addAnnotation(poi1)

您可以创建一个MGLShapeSource阵列MGLPointAnnotationMGLPointFeature对象。 从那里,您可以添加一个类似于此集群示例的集群层。

如果还要为每个点分配文本或其他数据,请使用MGLPointFeature对象,然后将文本分配为属性值。

例如:

        var features = [MGLPointFeature]()
        for _ in 1...100 {
            let point = MGLPointFeature()

            let lat = Double(arc4random_uniform(180) / 2)
            let lon = Double(arc4random_uniform(360) / 2)

            point.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
            point.attributes["title"] = "\(lat) \(lon)"
            features.append(point)
        }
        let source = MGLShapeSource(identifier: "clusteredFeatures",
                                    features: features,
                                    options: [.clustered: true, .clusterRadius: 20])
        style.addSource(source)

Mapbox网站上可以找到有关如何显示包含信息的标注的示例 似乎handleMapTap方法可能包含您要查找的内容。

本质上,您将查询用户点击的地图。 访问所选功能的属性,然后显示包含该信息的标注。 上面的示例使用UILabel来显示信息。

如果要使用默认标注,请参见动态样式交互点示例上的handleMapTap方法。 此示例还从运行时加载的JSON数据创建形状源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM