ios - Swift iOS MapKit Polylines - 创建网格
问题描述
我正在尝试在地图的可视区域中使用折线绘制网格。然而,尽管线条被绘制出来,但它们似乎有时会闪烁并消失。我经历过不同的方法,但似乎都遇到了同样的问题。
我通过地图视图委托 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 和模拟器上运行。
提前致谢。
解决方案
推荐阅读
- kubernetes - 为什么在 Nginx Ingress 前面放一个 LoadBalancer 类型的服务
- javascript - 在 javascript 解释器中解析循环
- python - 有没有比 apply() 更有效的方法来检查和替换 Pandas Dataframe 中的行值?
- python - 在迭代python时从剩余数组中找到2个元素的最小差异
- sql-server - 如何将用户定义的表类型值传递给 SQL Server Management Studio 中执行过程的值字段?
- r - 在一个 ggplot 上结合两个数据子集和 GLM 结果
- python - 如何增加熊猫的行高?
- javascript - 无法检索 JSON 帖子的内容
- amazon-web-services - 通过 Amplify 调用 iOS 中预先存在的 Lambda 函数
- r - 尝试将两个日期时间字段拆分为 RStudio 中的单独日期和时间字段