使用MKMapSnapshotter创建可点击的地图预览

问题描述

我有一张地图,该地图当前显示用户的当前位置,没有图钉,以准确显示他们的位置。我想使地图成为用户设置位置的照片。我只知道如何显示他们的设备在哪里,并需要他们能够设置其操作基础。我不需要特定的地址。我只需要他们居住的城市。

然后我需要图像可以被点击。轻按时,图像将使MapKit全屏显示并进行交互。然后,他们可以在地图上缩放并查看其他用户在哪里设置操作基础。

我是编码的新手,即使他们在全国各地移动,也无法弄清楚如何允许用户设置永久位置。我还不知道如何设置mapsnapshot,并在点击以显示完整的地图视图时在其上方扩展。

我目前只能询问是否可以激活位置服务,然后在加载时显示其地图视图。这是代码

import UIKit
import CoreLocation
import MapKit

class HoMetableViewController: UITableViewController,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,CLLocationManagerDelegate,MKMapViewDelegate {

@IBOutlet weak var activityIndicator: UIActivityIndicatorView!

@IBOutlet weak var mapPreviewImageView: UIImageView!

@IBOutlet weak var mapView: MKMapView!

let manager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    
    manager.desiredAccuracy = kCLLocationAccuracyBest // battery
    manager.delegate = self
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
    
    // Always adopt a light interface style.
    overrideUserInterfaceStyle = .light

    takeSnapShot()
    
}

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation,return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation,return the number of rows
    return 7
}

func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
    if let location = locations.first {
        manager.stopUpdatingLocation()
        
        render(location)
    }
}

func render (_ location: CLLocation) {
    
    let coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude,longitude: location.coordinate.longitude)
    
    let span = MKCoordinateSpan(latitudeDelta: 0.1,longitudeDelta: 0.1)
    
    let region = MKCoordinateRegion(center: coordinate,span: span)
                                    
    mapView.setRegion(region,animated: true)
    
 }

 func takeSnapShot() {
    let location = CLLocation()
    
    let mapSnapshotOptions = MKMapSnapshotter.Options()

    let coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude,span: span)
    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

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

    let snapShotter = MKMapSnapshotter(options: mapSnapshotOptions)

    snapShotter.start() { snapshot,error in
        guard let snapshot = snapshot else {
            return
        }
        self.mapPreviewImageView.image = snapshot.image
    }
 }

}

非常感谢您的帮助。我确实需要在此应用上取得一些进展,而且似乎找不到有关如何执行此操作的任何教程或网络结果。

编辑:

我尝试添加一个将UIImage转换为快照的函数。我能够返回图像,但是它不显示我的位置,并且比我的UIImage小。我已经编辑了上面的代码以反映我所做的更改。我不知道我在做什么错。

解决方法

在您的示例中,您正在创建public class HomeViewModel : MvxViewModel { private IRExManager _rexManager; public string SelectedUser { get; set; } public HomeViewModel(IRExManager rExManager) { _rexManager = rExManager; SelectedUser = "pack://application:,/Resources/Images/StandardMedicImage.png"; } } ,但没有使用它。您正在使用CLLocationManager。那显然没有(有意义的)坐标。确保提供有效的坐标。例如,让CLLocation()呼叫didUpdateLocations

takeSnapshot

结果是:

enter image description here


不相关的观察结果

  1. 您正在使用class ViewController: UIViewController { @IBOutlet weak var imageView: UIImageView! private weak var snapshotter: MKMapSnapshotter? private lazy var manager: CLLocationManager = { let manager = CLLocationManager() manager.delegate = self manager.distanceFilter = 20 return manager }() override func viewDidLoad() { super.viewDidLoad() if manager.authorizationStatus == .notDetermined { manager.requestWhenInUseAuthorization() } manager.startUpdatingLocation() } func takeSnapshot(of location: CLLocation) { snapshotter?.cancel() // cancel prior one,if any let options = MKMapSnapshotter.Options() options.camera = MKMapCamera(lookingAtCenter: location.coordinate,fromDistance: 1000,pitch: 0,heading: 0) options.mapType = .satellite options.size = imageView.bounds.size let snapshotter = MKMapSnapshotter(options: options) snapshotter.start() { snapshot,_ in self.imageView.image = snapshot?.image } self.snapshotter = snapshotter } } extension ViewController: CLLocationManagerDelegate { func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) { guard let location = locations.last(where: { $0.horizontalAccuracy >= 0 } ) else { return } takeSnapshot(of: location) } } 。我个人认为跨度并不是非常有用。我可能建议您使用电表,例如

    MKCoordinateSpan(latitudeDelta: 0.1,longitudeDelta: 0.1)

    或使用options.region = MKCoordinateRegion(center: location.coordinate,latitudinalMeters: 1000,longitudinalMeters: 1000)

    MKMapCamera
  2. 如果使用的地图类型为options.camera = MKMapCamera(lookingAtCenter: location.coordinate,heading: 0) ,则没有必要使用showsBuildingsThe docs说:

    必须将mapType属性设置为MKMapType.standard,才能显示挤出的建筑物。

  3. 我认为您不必设置satelliteThe docs说:

    此属性设置为默认值,该值与当前设备的显示器分辨率相对应。

    此外,无论如何,此属性现在已被弃用。

  4. 不过,我建议设置图像的size