有没有一种方法可以根据当前用户的位置在MKMapView上创建图钉(注释)?

时间:2019-04-24 21:54:06

标签: swift xcode mapkit spotify

总体而言,我正在尝试创建一个使用Spotify API的应用程序,该应用程序的目标是基于用户正在按一下按钮在Spotify上收听的当前歌曲,然后将该歌曲映射为图钉。我目前坚持尝试按用户当前的位置来固定图钉,只需按一下按钮即可。

到目前为止,我已有代码,可以在MKMapView上动态跟踪用户

import UIKit
import MapKit
import CoreLocation

class MapScreen: UIViewController {

    @IBOutlet weak var mapView: MKMapView!

    let locationManager = CLLocationManager()
    let regionInMeters: Double = 1000
    let newPin = MKPointAnnotation()

    override func viewDidLoad() {
        super.viewDidLoad()
        checkLocationServices()
    }


    func setupLocationManager() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }


    func centerViewOnUserLocation() {
        if let location = locationManager.location?.coordinate {
            let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
            mapView.setRegion(region, animated: true)
        }
    }


    func checkLocationServices() {
        if CLLocationManager.locationServicesEnabled() {
            setupLocationManager()
            checkLocationAuthorization()
        } else {
            // Show alert letting the user know they have to turn this on.
        }
    }


    func checkLocationAuthorization() {
        switch CLLocationManager.authorizationStatus() {
        case .authorizedWhenInUse:
            mapView.showsUserLocation = true
            centerViewOnUserLocation()
            locationManager.startUpdatingLocation()
            break
        case .denied:
            // Show alert instructing them how to turn on permissions
            break
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
        case .restricted:
            // Show an alert letting them know what's up
            break
        case .authorizedAlways:
            break
        }
    }


    //    class pinedSong: NSObject , MKAnnotation {
    //        let title: String?
    //        let artist: String?
    //        let coordinate: CLLocationCoordinate2D
    //
    //        init(title: String, artist: String, coordinate: CLLocationCoordinate2D)
    //        {
    //            self.title = title
    //            self.artist = artist
    //            self.coordinate = coordinate
    //
    //            super.init()
    //        }
    //    }
    //
    //
    //
    //
    //    @IBAction func btnPinSong(_ sender: UIButton) {
    //
    //
    //
    //
    //}
    //    func pinMaker(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    //        //Get Current Location
    //        let location = locations.last! as CLLocation
    //        let userLocation:CLLocation = locations[0] as CLLocation
    //        let myAnnotation: MKPointAnnotation = MKPointAnnotation()
    //        myAnnotation.coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude)
    //        myAnnotation.title = "Current location"
    //        mapView.addAnnotation(myAnnotation)
    //
    //    }
    //

}

extension MapScreen: CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.last else { return }
        let region = MKCoordinateRegion.init(center: location.coordinate, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
        mapView.setRegion(region, animated: true)
    }


    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        checkLocationAuthorization()
    }



}

现在,我需要做的只是根据用户当前停留的位置放置图钉的按钮。

1 个答案:

答案 0 :(得分:0)

您可以使用locationManager.location获取当前用户的位置。

@IBAction func btnPinSong(_ sender: UIButton) {
    //Get Current Location
    guard let userLocation = self.locationManager.location else {
        return
    }

    let myAnnotation: MKPointAnnotation = MKPointAnnotation()
    myAnnotation.coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude)
    myAnnotation.title = "Current location"
    mapView.addAnnotation(myAnnotation)
}