【问题标题】:Is there a way to create a pin (an annotation) on a MKMapView based on the current users location?有没有办法根据当前用户位置在 MKMapView 上创建图钉(注释)?
【发布时间】:2019-04-24 21:54:05
【问题描述】:

总的来说,我正在尝试创建一个使用 Spotify API 的应用程序,该应用程序的目标是根据用户在 Spotify 上收听的当前歌曲,按下按钮,然后将歌曲映射为 pin .我目前一直在尝试根据用户当前的位置制作一个 pin,按下按钮就会停留在那里。

到目前为止,我的代码可以在 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()
    }



}

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

【问题讨论】:

  • 当您使用mapkit时,您绝对可以显示用户位置。如果您只想固定它,那么在某个时间点获取用户的位置坐标然后创建一个图钉而不是调用用户位置(可能会更新)可能是有意义的此外,如果您确实使用用户位置,请确保你可以显示它:developer.apple.com/documentation/mapkit/mkmapview/…

标签: swift xcode mapkit spotify


【解决方案1】:

您可以使用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)
}

【讨论】:

猜你喜欢
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
  • 2014-03-25
  • 1970-01-01
相关资源
最近更新 更多