简体   繁体   English

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

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

I need to find a way to convert markers from MGLPointAnnotation to MGLShapeSource or similar, in order to add the markers to a map layer and have full control over how to display and cluster them on the map, for example. 我需要找到一种将标记从MGLPointAnnotation转换为MGLShapeSource或类似方法的方法,以便将标记添加到地图图层并完全控制例如如何在地图上显示和聚类标记。

I am building an iOS App using MapBox SDK v5.2 . 我正在使用MapBox SDK v5.2构建iOS应用。 The App generates markers internally (title, subtitle, coordinates and icon image name) and the markers are displayed on the map with a callout when tapped. 该应用程序在内部生成标记(标题,字幕,坐标和图标图像名称),并且在点击时标记会在地图上显示并带有标注。 The markers are created using MGLPointAnnotation() and added to the map using mapView.addAnnotation() . 使用在创建标记MGLPointAnnotation()并使用添加到地图mapView.addAnnotation()

But in order to have full control on how the markers are displayed, for example clustering them based on zoom level or toggling them ON/OFF, I need to add the markers to a Map layer, using, for example, MGLShapeSource and then style.addSource() and style.addLayer() . 但是,为了完全控制标记的显示方式(例如,基于缩放级别对标记进行聚类或将其切换为ON / OFF),我需要将标记添加到Map图层,例如使用MGLShapeSource ,然后添加style.addSource()style.addLayer()

The problem is I cannot find a way to the converter from MGLPointAnnotation to MGLShapeSource or similar. 问题是我找不到从MGLPointAnnotationMGLShapeSource或类似文件的转换器。 I have investigated this but the only solution I can think of is to have the marker information contained in a GeoJSON file. 我对此进行了调查,但我能想到的唯一解决方案是将标记信息包含在GeoJSON文件中。 But I want to avoid that as the markers are generated within the App when its running, and not from an external read-only GeoJSON file. 但我想避免这种情况,因为标记是在应用运行时在应用内生成的,而不是从外部只读GeoJSON文件生成的。

Example on how one single poi is created: 有关如何创建单个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)

You can create an MGLShapeSource with an array of MGLPointAnnotation or MGLPointFeature objects. 您可以创建一个MGLShapeSource阵列MGLPointAnnotationMGLPointFeature对象。 From there, you can add a clustered layer similar to this clustering example . 从那里,您可以添加一个类似于此集群示例的集群层。

If you would also like to assign text or other data for each point, use MGLPointFeature objects, then assign the text as an attributes value. 如果还要为每个点分配文本或其他数据,请使用MGLPointFeature对象,然后将文本分配为属性值。

For example: 例如:

        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)

An example of how to present a callout containing the information can be found on the Mapbox website . Mapbox网站上可以找到有关如何显示包含信息的标注的示例 It seems like the handleMapTap method may contain what you're looking for. 似乎handleMapTap方法可能包含您要查找的内容。

Essentially, you'll query the map where the user taps. 本质上,您将查询用户点击的地图。 Access the attributes on the selected feature, then display a callout containing that information. 访问所选功能的属性,然后显示包含该信息的标注。 The above example uses a UILabel to display the information. 上面的示例使用UILabel来显示信息。

If you would like to use a default callout, please see the handleMapTap method on the dynamically style interactive points example. 如果要使用默认标注,请参见动态样式交互点示例上的handleMapTap方法。 This example also creates a shape source from JSON data loaded at runtime. 此示例还从运行时加载的JSON数据创建形状源。

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

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