Swift iOS MapKit Polylines - создание сетки - PullRequest
0 голосов
/ 05 декабря 2018

Я пытаюсь нарисовать сетку, используя полилинии в видимой области карты.Тем не менее, хотя линии нарисованы, они, кажется, временами мерцают и исчезают.У меня были разные подходы, но, похоже, у всех одна и та же проблема.

Я сделал это с помощью делегата mapViewDidChangeVisibleRegion представления карты, если существующие линии poly являются их, тогда я удаляю их и рисую новые в видимой области.

Ниже приведен код делегата, который я просто не могу точно определить, почему строки исчезают.

    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 и эмуляторе.

Заранее спасибо.

...