我正在尝试在地图的可视区域中使用折线绘制网格。但是,尽管画出了线条,但它们似乎有时会闪烁并消失。我经历过不同的方法,但似乎所有人都遇到相同的问题。
我已经通过地图视图委托mapViewDidChangeVisibleRegion完成了此操作,如果现有的折线是它们的话,那么我将其删除并在可见区域绘制新的折线。
下面是委托代码,我似乎无法查明行消失的原因。
func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
DispatchQueue.main.async {
let zoomLevel = mapView.getZoomLevel()
if(mapView.overlays.count > 0)
{
for line in mapView.overlays
{
mapView.removeOverlay(line)
}
}
let northEast = mapView.convert(CGPoint(x: mapView.bounds.width, y: 0), toCoordinateFrom: mapView)
let southWest = mapView.convert(CGPoint(x: 0, y: mapView.bounds.height), toCoordinateFrom: mapView)
let minLon = 10 * Int((floor(southWest.longitude) / 10.0).rounded())
let maxLon = 10 * Int((floor(northEast.longitude) / 10.0).rounded())
let minLat = 10 * Int((floor(southWest.latitude) / 10.0).rounded())
let maxLat = 10 * Int((floor(northEast.latitude) / 10.0).rounded())
if(zoomLevel < 6)
{
for latIndex in stride(from: minLat, through: maxLat, by: 10)
{
var pointsLon : [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
if(minLon < 0 && maxLon < 0)
{
let startPos = CLLocationCoordinate2DMake(CLLocationDegrees(latIndex), CLLocationDegrees(minLon))
pointsLon.append(startPos)
let endPos = CLLocationCoordinate2DMake(CLLocationDegrees(latIndex), CLLocationDegrees(maxLon))
pointsLon.append(endPos)
}
if(minLon > 0 && maxLon > 0)
{
let startPos = CLLocationCoordinate2DMake(CLLocationDegrees(latIndex), CLLocationDegrees(minLon))
pointsLon.append(startPos)
let endPos = CLLocationCoordinate2DMake(CLLocationDegrees(latIndex), CLLocationDegrees(maxLon))
pointsLon.append(endPos)
}
if(minLon < 0 && maxLon > 0 )
{
let startPos = CLLocationCoordinate2DMake(CLLocationDegrees(latIndex), CLLocationDegrees(maxLon))
pointsLon.append(startPos)
let midPos = CLLocationCoordinate2DMake(CLLocationDegrees(latIndex), CLLocationDegrees(0))
pointsLon.append(midPos)
let endPos = CLLocationCoordinate2DMake(CLLocationDegrees(latIndex), CLLocationDegrees(minLon))
pointsLon.append(endPos)
}
if(minLon > 0 && maxLon < 0 )
{
let sPos = CLLocationCoordinate2DMake(CLLocationDegrees(latIndex), CLLocationDegrees(maxLon))
pointsLon.append(sPos)
let mPos = CLLocationCoordinate2DMake(CLLocationDegrees(latIndex), CLLocationDegrees(-180))
pointsLon.append(mPos)
let polyline = MKPolyline(coordinates: pointsLon, count: pointsLon.count)
self.polylines.append(polyline)
mapView.addOverlay(polyline, level: .aboveRoads )
pointsLon.removeAll()
let startPos = CLLocationCoordinate2DMake(CLLocationDegrees(latIndex), CLLocationDegrees(minLon))
pointsLon.append(startPos)
let midPos = CLLocationCoordinate2DMake(CLLocationDegrees(latIndex), CLLocationDegrees(180))
pointsLon.append(midPos)
}
let polyline = MKPolyline(coordinates: pointsLon, count: pointsLon.count)
self.polylines.append(polyline)
mapView.addOverlay(polyline, level: .aboveRoads )
}
}
else if(zoomLevel < 12)
{
//to be implemented
}
else{
//to be implemented
}
}
}
关于为什么收到的任何想法,我们都会感激不尽。在iOS 12.1和iPhone 8和模拟器上运行。
谢谢。