Рисование многоугольника и полилинии с помощью MKMapSnapshotter в Swift - PullRequest
0 голосов
/ 06 июня 2019

Я использовал MKMapSnapshotter, чтобы загрузить изображение с определенной координатой и установить изображение в ImageView. Я использую MKMapSnapshotter, так как imageView присутствует в UITableViewCell.

Я хочу сделать то же самое для polygon и polyline, ниже приведен мой код для типа фигуры точки.

private func setMapImage(_ location: Coordinate) {
    let coordinate = "\(location.latitude),\(location.longitude)"
    if let image = SDImageCache.shared().imageFromCache(forKey: coordinate) {
        mapImageView.image = image
    } else {

        let rect = self.mapImageView.bounds
        let mapSnapshotOptions = MKMapSnapshotter.Options()

        // Set the region of the map that is rendered.
        let needleLocation = CLLocationCoordinate2DMake(location.latitude, location.longitude)
        let region = MKCoordinateRegion(center: needleLocation, latitudinalMeters: 200, longitudinalMeters: 200)
        mapSnapshotOptions.region = region

        // Set the scale of the image. We'll just use the scale of the current device, which is 2x scale on Retina screens.
        mapSnapshotOptions.scale = UIScreen.main.scale

        // Set the size of the image output.
        mapSnapshotOptions.size = CGSize(width: rect.width, height: rect.height)

        // Show buildings and Points of Interest on the snapshot
        mapSnapshotOptions.showsBuildings = true
        mapSnapshotOptions.showsPointsOfInterest = true

        let snapshot = MKMapSnapshotter(options: mapSnapshotOptions)
        snapshot.start { snapshot, error in
            guard let snapshot = snapshot, error == nil else {
                print("Failed to get snapshot \(error!.localizedDescription)")
                return
            }

            UIGraphicsBeginImageContextWithOptions(mapSnapshotOptions.size, true, 0)
            snapshot.image.draw(at: .zero)

            let pinView = MKPinAnnotationView(annotation: nil, reuseIdentifier: nil)
            let pinImage = pinView.image

            var point = snapshot.point(for: needleLocation)

            if rect.contains(point) {
                let pinCenterOffset = pinView.centerOffset
                point.x -= pinView.bounds.size.width / 2
                point.y -= pinView.bounds.size.height / 2
                point.x += pinCenterOffset.x
                point.y += pinCenterOffset.y
                pinImage?.draw(at: point)
            }

            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

            DispatchQueue.main.async { [weak self] in
                self?.mapImageView.image = image
                SDImageCache.shared().store(image, forKey: coordinate, completion: nil)
            }
        }
    }
}

Для достижения вышесказанного мне нужно добавить аннотации, а затем нарисовать линию из них. Я не вижу никакого метода на MKMapSnapshotter для этого.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...