0

I just started programming in Swift and I am a complete beginner. I wanted to write a function or class that, by receiving the coordinates, if there is a photo in MKLookAround.request, saves a photo of the desired location using MKLookAround.Snapshotter, but I don't know how to use the mapkit ready classes. I don't want to use swiftUI, I just want to save pictures for a number of coordinates from different places. enter image description here

  • You will need to tell us (with code) what have you tried so far. – Rudedog Oct 04 '22 at 15:53
  • Have you checked out the [sample code](https://developer.apple.com/documentation/mapkit/explore_a_location_with_a_highly_detailed_map_and_look_around) or watched WWDC 2022 [What’s new in MapKit](https://developer.apple.com/videos/play/wwdc2022/10035/)? – Rob Oct 04 '22 at 17:20
  • @Rob Yes I saw. The program was in the UI environment. But my goal is just to give some coordinates to a function and save the photo, and my goal is not to build an application or display images with lookaround. I mean something like this: let coordinate : CLLocationCoordinate2DMake(37.33, -122.009) But I don't know how to give these coordinates as input to MKLookSnapShotter and save a photo as output. – Pooriya Rezaei Oct 04 '22 at 18:09
  • @Rudedog Unfortunately, I could not write a code for it because I had no idea about it. – Pooriya Rezaei Oct 04 '22 at 18:10
  • Yeah, I feel your pain (and their sample was a little convoluted). But if you search that sample project for `MKLookAroundSnapshotter`, you’ll see precisely how they’re using it, and that gives you a good starting point. – Rob Oct 04 '22 at 18:31

1 Answers1

0

You can create a MKLookAroundSceneRequest, grab its scene, and then supply that to MKLookAroundSnapshotter.

func snapshotImage(for coordinate: CLLocationCoordinate2D) async throws -> UIImage {
    guard let scene = try await MKLookAroundSceneRequest(coordinate: coordinate).scene else {
        throw LookaroundError.unableToCreateScene
    }
    let options = MKLookAroundSnapshotter.Options()
    options.size = CGSize(width: 1000, height: 500)
    return try await MKLookAroundSnapshotter(scene: scene, options: options).snapshot.image
}

enter image description here

That uses this Error object:

enum LookaroundError: Error {
    case unableToCreateScene
}

For more information, see WWDC 2022 What’s new in MapKit or check out the use of MKLookAroundSnapshotter in their sample project.


And if you want to write that to a file get either the PNG (pngData) or JPG (jpgData) representation and write it to a file:

let image = try await snapshotImage(for: coordinate)
let url = try FileManager.default
    .url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    .appending(component: "test.png")
try image.pngData()?.write(to: url)

Or alternatively, you might want to present a user a UI whereby they can specify what they want to do with the image, via UIActivityViewController:

let share = UIActivityViewController(activityItems: [image], applicationActivities: nil)
self.present(share, animated: true)
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • I just ran into a small error when calling the function. let image = try away self.image(for : coordinate) Error: cannot find 'self' in scope – Pooriya Rezaei Oct 04 '22 at 18:45
  • Mine was in a class, calling a method of that class. Just remove `self.`. I also changed method name to avoid ambiguity. – Rob Oct 04 '22 at 19:37
  • Thank you very much for your help. It worked fine.Only I have a data frame of coordinates that I want to give as input to the function inside a for loop and save a picture for each.When I try to run the program, the first coordinate of the photo is saved, but then it gives an error.I encounter this error. Do you know what could be the reason? Playground execution failed:error: execution stopped with unexpected state.error: Execution was interrupted.The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation. – Pooriya Rezaei Oct 04 '22 at 20:04
  • In playground, did you set `PlaygroundPage.current.needsIndefiniteExecution` to `true`? – Rob Oct 04 '22 at 21:04
  • Yes, I did, but I still got the same error. – Pooriya Rezaei Oct 05 '22 at 08:48
  • This is great! Is it possible to somehow influence the bearing for the snapshot? I would like to look down the road for example. If I enter a coordinate I can influence the direction of the snapshot by moving to either side of the road. However, it's a bit hit-and-miss and I can't get it to look up or down the road. – Navigator Jan 06 '23 at 10:42