- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 mapView 和 CLLocationCoordinate2D
数组。我使用这些位置通过 MKPolyline
在我的 mapView 上画线。现在我想将它存储为 UIimage。我发现有 MKMapSnapshotter
类,但不幸的是我无法在其上绘制叠加层“Snapshotter 对象不会捕获您的应用程序创建的任何叠加层或注释的视觉表示。”所以我只得到空白 map 图像。有什么方法可以使用我的叠加层获取图像吗?
private func generateImageFromMap() {
let mapSnapshotterOptions = MKMapSnapshotter.Options()
guard let region = mapRegion() else { return }
mapSnapshotterOptions.region = region
mapSnapshotterOptions.size = CGSize(width: 200, height: 200)
mapSnapshotterOptions.showsBuildings = false
mapSnapshotterOptions.showsPointsOfInterest = false
let snapShotter = MKMapSnapshotter(options: mapSnapshotterOptions)
snapShotter.start() { snapshot, error in
guard let snapshot = snapshot else {
//do something with image ....
let mapImage = snapshot...
}
}
}
如何在这张图片上叠加?或者也许有其他方法可以解决该问题。
最佳答案
不幸的是,你必须自己画它们。幸运的是,MKSnapshot
有一个方便的 point(for:)
将 CLLocationCoordinate2D
转换为快照中的 CGPoint
的方法。
例如,假设您有一个 CLLocationCoordinate2D
数组:
private var coordinates: [CLLocationCoordinate2D]?
private func generateImageFromMap() {
guard let region = mapRegion() else { return }
let options = MKMapSnapshotter.Options()
options.region = region
options.size = CGSize(width: 200, height: 200)
options.showsBuildings = false
options.showsPointsOfInterest = false
MKMapSnapshotter(options: options).start() { snapshot, error in
guard let snapshot = snapshot else { return }
let mapImage = snapshot.image
let finalImage = UIGraphicsImageRenderer(size: mapImage.size).image { _ in
// draw the map image
mapImage.draw(at: .zero)
// only bother with the following if we have a path with two or more coordinates
guard let coordinates = self.coordinates, coordinates.count > 1 else { return }
// convert the `[CLLocationCoordinate2D]` into a `[CGPoint]`
let points = coordinates.map { coordinate in
snapshot.point(for: coordinate)
}
// build a bezier path using that `[CGPoint]`
let path = UIBezierPath()
path.move(to: points[0])
for point in points.dropFirst() {
path.addLine(to: point)
}
// stroke it
path.lineWidth = 1
UIColor.blue.setStroke()
path.stroke()
}
// do something with finalImage
}
}
然后是以下 map View (带有坐标,如 MKPolyline
,由 mapView(_:rendererFor:)
渲染,像往常一样):
上面的代码将创建这个finalImage
:
关于ios - 如何在 MKMapSnapshotter 上绘制 CLLocationCoordinate2Ds(在 mapView 打印图像上绘制),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54678314/
我只是通过使用 [NSString stringWithFormat:@"%.5f, %.5f"]; 将 CLLocations 转换为逗号分隔的字符串,例如 "75.45874, -45.17292
我正在尝试将来 self 的应用程序的跟踪部分的 CCLocationCoordinates 数组存储到 UserDefaults 中,并将跟踪路线的名称作为键,以便以后能够在函数中调用它。问题是,当
我正在尝试使用 CoreLocation(一旦获得许可)来获取用户的 CLLocationCoordinate2D,以便我可以将该信息传递给 Uber 深层链接(在按下我的 TableView 中的
我是一名优秀的程序员,十分优秀!