作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 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/
我正在使用 mapbox 并且有一个由 2 个注释组成的帖子。一个可以,另一个不能(不应该)可选。 如何使特定的 MGLAnnotation 不可选择? 我看过here和 here并没有找到解决办法。
我正在为 ios 使用 mapbox map 。在其中,我添加了注释。当您开始缩小时,注释会越来越接近它们开始重叠的程度。我一直在尝试编写逻辑来解决这个问题,但这会使应用程序变慢。 我想知道是否有一些
如何添加自定义属性,例如“工作”或“级别”等? 目前内置仅允许标题、描述和位置。 最佳答案 您必须子类化 MGLAnnotation 并添加自定义属性,如下所示: class MyAnnotation
我是一名优秀的程序员,十分优秀!