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,我需要将标记添加到地图层,使用,例如,MGLShapeSource
然后是 style.addSource()
和 style.addLayer()
.
问题是我找不到从 MGLPointAnnotation
到 MGLShapeSource
或类似转换器的方法。
我对此进行了调查,但我能想到的唯一解决方案是将标记信息包含在 GeoJSON
文件中。但我想避免这种情况,因为标记是在 运行 时在 App 内生成的,而不是来自外部 read-only GeoJSON 文件。
关于如何创建单个兴趣点的示例:
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)
您可以用 array of MGLPointAnnotation
or MGLPointFeature
objects. From there, you can add a clustered layer similar to this clustering example 创建一个 MGLShapeSource
。
如果您还想为每个点分配文本或其他数据,请使用 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 website 上找到有关如何显示包含信息的标注的示例。 handleMapTap
方法似乎包含您要查找的内容。
本质上,您将查询用户点击的地图。访问所选特征的属性,然后显示包含该信息的标注。上面的例子使用了一个UILabel
来显示信息。
如果您想使用默认标注,请参阅 dynamically style interactive points 示例中的 handleMapTap
方法。此示例还根据运行时加载的 JSON 数据创建形状源。
我需要找到一种将标记从 MGLPointAnnotation
转换为 MGLShapeSource
或类似方法的方法,以便将标记添加到地图图层并完全控制如何显示和聚类它们例如在地图上。
我正在使用 MapBox SDK v5.2
构建一个 iOS 应用程序。
该应用程序在内部生成标记(标题、副标题、坐标和图标图像名称),点击时标记会显示在地图上并带有标注。标记是使用 MGLPointAnnotation()
创建的,并使用 mapView.addAnnotation()
.
但是为了完全控制标记的显示方式,例如根据缩放级别对它们进行聚类或切换它们 ON/OFF,我需要将标记添加到地图层,使用,例如,MGLShapeSource
然后是 style.addSource()
和 style.addLayer()
.
问题是我找不到从 MGLPointAnnotation
到 MGLShapeSource
或类似转换器的方法。
我对此进行了调查,但我能想到的唯一解决方案是将标记信息包含在 GeoJSON
文件中。但我想避免这种情况,因为标记是在 运行 时在 App 内生成的,而不是来自外部 read-only GeoJSON 文件。
关于如何创建单个兴趣点的示例:
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)
您可以用 array of MGLPointAnnotation
or MGLPointFeature
objects. From there, you can add a clustered layer similar to this clustering example 创建一个 MGLShapeSource
。
如果您还想为每个点分配文本或其他数据,请使用 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 website 上找到有关如何显示包含信息的标注的示例。 handleMapTap
方法似乎包含您要查找的内容。
本质上,您将查询用户点击的地图。访问所选特征的属性,然后显示包含该信息的标注。上面的例子使用了一个UILabel
来显示信息。
如果您想使用默认标注,请参阅 dynamically style interactive points 示例中的 handleMapTap
方法。此示例还根据运行时加载的 JSON 数据创建形状源。