在极少数情况下,我的 map 上的叠加层(小蓝点)会出现奇怪的眩光(右侧的大蓝色区域)(如图所示)。有时放大或缩小会修复它,但并非总是如此。找不到任何关于为什么会发生这种情况的信息。这与它的呈现方式有关吗?
func drawLocations(_ loc: CLLocation)
{
let center = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
let lat: CLLocationDegrees = center.latitude
let long: CLLocationDegrees = center.longitude
var points = [CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long)]
let polygon = MKPolygon(coordinates: &points, count: points.count)
DispatchQueue.main.async(execute: {
self.mapView.add(polygon)
})
}
func mapView(_ mapView: MKMapView!, rendererFor overlay: MKOverlay!) -> MKOverlayRenderer!
{
if overlay is MKPolygon
{
let polygonView = MKPolygonRenderer(overlay: overlay)
polygonView.lineWidth = 4
polygonView.strokeColor = UIColor(red: 30/255.0, green: 12/255.0, blue: 242/255.0, alpha: 0.4)
return polygonView
}
return nil
}
最佳答案
看起来您正在使用 MKPolygon
,即使您只绘制了一个具有相同纬度和经度的点。我认为对单个点使用 MKCircle
会更好。
func drawLocation(_ loc: CLLocation)
{
let center = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
let circle = MKCircle(center: center, radius: 4)
DispatchQueue.main.async(execute: {
self.mapView.add(circle)
})
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
{
if overlay is MKCircle
{
let circleView = MKCircleRenderer(circle: overlay)
circleView.lineWidth = 4
circleView.strokeColor = UIColor(red: 30/255.0, green: 12/255.0, blue: 242/255.0, alpha: 0.4)
return circleView
}
return nil
}
正如您所说,放大和缩小功能可能会导致此故障,因为放大时的点可能看起来更像多边形,而当您缩小时,渲染器仍然具有多边形人工制品,但现在只渲染一个单坐标。
通常最好使用多边形来渲染间隔较远的点,这有助于渲染器,因为 map 是在逐个图 block 的基础上渲染的,这样可以很容易地区分多边形中的点。对于非常靠近的多个点,我们可以考虑将它们分组以使用更大的半径 MKCircle
。您还可以考虑将 MKMapView
的 isZoomEnabled
设置为 false
,并在所需的缩放级别设置固定的 MKCoordinateRegion
避免这些渲染伪像。
关于ios - Swift MKMapView 多边形叠加故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40873179/