gpt4 book ai didi

ios - 如何将在 map 上重叠的 MGLAnnotations 组合在一起?

转载 作者:行者123 更新时间:2023-11-28 07:30:46 25 4
gpt4 key购买 nike

我正在为 ios 使用 mapbox map 。在其中,我添加了注释。当您开始缩小时,注释会越来越接近它们开始重叠的程度。我一直在尝试编写逻辑来解决这个问题,但这会使应用程序变慢。

我想知道是否有一些内置的 mapbox 方法可以处理我的这种[分组]。

是这样吗?可以分享吗?

如果有帮助,下面是我一直在尝试的代码:

    func mapViewRegionIsChanging(_ mapView: MGLMapView) {
print(mapView.zoomLevel, " Cenetr -<<<<")
print(mapView.visibleAnnotations, " These are the visible annotations")
grouping(mapView: mapView)
}

func grouping(mapView: MGLMapView) {

if let annotations = mapView.visibleAnnotations {

var lastVal = 0.0
var arrayAnnotationsToRemove = [CustomPointAnnotation]()
var arrayPolylinesToRemove = [MGLPolyline]()

for val in arrayOfLineInformationObjects {

if mapView.zoomLevel < 10 && mapView.zoomLevel > 7 {
let dif = lastVal-val.arrayOfPointAnnotations![0].coordinate.longitude
print(dif, "The dif is to teh left")
//long is being compared
if dif <= 0.01 {//dif >= 0.05 &&
print("Delete")
arrayAnnotationsToRemove.append(val.arrayOfPointAnnotations![0])
arrayAnnotationsToRemove.append(val.arrayOfPointAnnotations![1])
arrayPolylinesToRemove.append(val.polyline!)
}

} else if mapView.zoomLevel < 7 && mapView.zoomLevel > 5 {
let dif = lastVal-val.arrayOfPointAnnotations![0].coordinate.longitude
print(dif, "The dif is to teh left")
//long is being compared
if dif <= 0.01 {
print("Delete 2")
arrayAnnotationsToRemove.append(val.arrayOfPointAnnotations![0])
arrayAnnotationsToRemove.append(val.arrayOfPointAnnotations![1])
arrayPolylinesToRemove.append(val.polyline!)
}
}
lastVal = val.arrayOfPointAnnotations![0].coordinate.longitude
}
for (valueA, valueP) in zip(arrayAnnotationsToRemove, arrayPolylinesToRemove) {
mapView.removeAnnotation(valueA)
mapView.removeAnnotation(valueA)
mapView.remove(valueP)
}
if arrayAnnotationsToRemove.count > 0 {
let newGroupedAnnotation = MGLPolygon(coordinates: &arrayAnnotationsToRemove[0].coordinate, count: 1)
mapView.addAnnotation(polygonCircleForCoordinate(coordinate: arrayAnnotationsToRemove[0].coordinate, withMeterRadius: 50))
}
//notation(T##annotation: MGLAnnotation##MGLAnnotation)
}
}

最佳答案

您可以为此使用聚类点数据

您可以找到它的详细示例here .

我在这里复制了didFinishLoading函数,以防以后链接断开

func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
let url = URL(fileURLWithPath: Bundle.main.path(forResource: "ports", ofType: "geojson")!)

let source = MGLShapeSource(identifier: "clusteredPorts",
url: url,
options: [.clustered: true, .clusterRadius: icon.size.width])
style.addSource(source)

// Use a template image so that we can tint it with the `iconColor` runtime styling property.
style.setImage(icon.withRenderingMode(.alwaysTemplate), forName: "icon")

// Show unclustered features as icons. The `cluster` attribute is built into clustering-enabled
// source features.
let ports = MGLSymbolStyleLayer(identifier: "ports", source: source)
ports.iconImageName = NSExpression(forConstantValue: "icon")
ports.iconColor = NSExpression(forConstantValue: UIColor.darkGray.withAlphaComponent(0.9))
ports.predicate = NSPredicate(format: "cluster != YES")
style.addLayer(ports)

// Color clustered features based on clustered point counts.
let stops = [
20: UIColor.lightGray,
50: UIColor.orange,
100: UIColor.red,
200: UIColor.purple
]

// Show clustered features as circles. The `point_count` attribute is built into
// clustering-enabled source features.
let circlesLayer = MGLCircleStyleLayer(identifier: "clusteredPorts", source: source)
circlesLayer.circleRadius = NSExpression(forConstantValue: NSNumber(value: Double(icon.size.width) / 2))
circlesLayer.circleOpacity = NSExpression(forConstantValue: 0.75)
circlesLayer.circleStrokeColor = NSExpression(forConstantValue: UIColor.white.withAlphaComponent(0.75))
circlesLayer.circleStrokeWidth = NSExpression(forConstantValue: 2)
circlesLayer.circleColor = NSExpression(format: "mgl_step:from:stops:(point_count, %@, %@)", UIColor.lightGray, stops)
circlesLayer.predicate = NSPredicate(format: "cluster == YES")
style.addLayer(circlesLayer)

// Label cluster circles with a layer of text indicating feature count. The value for
// `point_count` is an integer. In order to use that value for the
// `MGLSymbolStyleLayer.text` property, cast it as a string.
let numbersLayer = MGLSymbolStyleLayer(identifier: "clusteredPortsNumbers", source: source)
numbersLayer.textColor = NSExpression(forConstantValue: UIColor.white)
numbersLayer.textFontSize = NSExpression(forConstantValue: NSNumber(value: Double(icon.size.width) / 2))
numbersLayer.iconAllowsOverlap = NSExpression(forConstantValue: true)
numbersLayer.text = NSExpression(format: "CAST(point_count, 'NSString')")

numbersLayer.predicate = NSPredicate(format: "cluster == YES")
style.addLayer(numbersLayer)
}

关于ios - 如何将在 map 上重叠的 MGLAnnotations 组合在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54798359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com