首页 > 解决方案 > 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 和模拟器上运行。

提前致谢。

标签: iosmapkitpolyline

解决方案


推荐阅读