我正在使用iOS的Mapbox地图。在其中添加注释。当开始缩小时,注释会越来越接近它们开始重叠的程度。我一直在尝试编写逻辑来解决此问题,但这会使应用程序变慢。
我想知道是否有一些内置的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)
}
}
答案 0 :(得分:0)
您可以为此使用Cluster point data
。
您可以找到有关它的详细示例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)
}