【问题标题】:Xcode 6.3.2 - Can't get location via MapKitXcode 6.3.2 - 无法通过 MapKit 获取位置
【发布时间】:2015-06-27 11:53:01
【问题描述】:

我正在将应用从 Xcode 6.2 重建到 6.3.2

在我的 info.plist 中,我使用字符串消息设置 NSLocationWhenInUseUsageDescription,并使用另一个字符串消息设置 Privacy - Location Usage Description。

我打开了功能/地图 我打开了功能/背景模式/位置更新

在构建阶段导入 MapKit 和 CoreLocation 框架

我需要获取单个纬度和经度,因为 Parse 需要它们。

Imas145 回答后更新

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager = CLLocationManager()

    @IBOutlet weak var mapView: MKMapView!

    @IBOutlet weak var anotationsCountLabel: UILabel!
    @IBOutlet weak var distanceFilterLabel: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()

        locationManager.distanceFilter = 100.0      //tot horizontal meters before sending an update on location
        mapView.userTrackingMode = .Follow
        locationManager.startUpdatingLocation()


    }

    //MARK: - Helper - CLLocationManagerDelegate

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

        if let location = locationManager.location {

        var myLatitude = locationManager.location.coordinate.latitude
        var myLongitude = locationManager.location.coordinate.longitude

        var location = CLLocationCoordinate2D(latitude: myLatitude, longitude: myLongitude)
        let span = MKCoordinateSpanMake(0.05, 0.05)             //how much to show
        let region = MKCoordinateRegionMake(location, span)     //how much to show
        mapView.setRegion(region, animated: true)

//        let anotation = MKPointAnnotation()
//        anotation.coordinate = location                         //was : anotation.setCoordinate(location)
//        anotation.title = "you are here"
//        anotation.subtitle = "City name"


//        mapView.removeAnnotation(anotation)
//        mapView.addAnnotation(anotation)


        var totalAnotations = mapView.annotations.count
        anotationsCountLabel.text = "\(totalAnotations)"
        distanceFilterLabel.text = "\(locationManager.distanceFilter)"
        println("latitude is: \(myLatitude)")
        println("longitude is: \(myLongitude)")


//        locationManager.stopUpdatingLocation()

        }

    }



    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        println("problem1: error" + error.localizedDescription)

    }



}

【问题讨论】:

  • 也许@Nils Ziehn 可以帮助我

标签: ios xcode parse-platform


【解决方案1】:

在您使用startUpdatingLocation() 开始位置更新后,您无法立即访问location。正如文档所说:

The last location received. Will be nil until a location has been received.

因此,您需要在确定位置后进行处理。你可以在locationManager:didUpdateLocations:

这是一个快速的实现:

    class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!


    var locationManager = CLLocationManager()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.



        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.distanceFilter = 100.0      //distance before notifing a change
        locationManager.startUpdatingLocation()     //really starts monitoring position

        // move the code to delegate method

    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        if let location = locationManager.location {
            var myLatitutde = location.coordinate.latitude
            var myLongitude = location.coordinate.longitude

            var myLocation = CLLocationCoordinate2D()
            let span = MKCoordinateSpanMake(0.05, 0.05)                 //how much to show
            let region = MKCoordinateRegionMake(myLocation, span)       //how much to show
            mapView.setRegion(region, animated: true)

            let anotation = MKPointAnnotation()

            anotation.coordinate = myLocation                       //XC 6.3.2
            anotation.title = "you are here"
            anotation.subtitle = "city name"

            mapView.addAnnotation(anotation)
        }

    }
}

您还应该处理locationManager:didFailWithError:,以防出现问题。

编辑:

要使大头针下落动画,您需要执行以下操作:

  1. 将MKMapViewDelegate 协议添加到您的班级

    class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate
    
  2. 将代理添加到您的mapView

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        mapView.delegate = self
    
        // rest of the code, location manager setup etc...
    }
    
  3. 实现mapView:viewForAnnotation:委托方法

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        let pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil)
        pin.pinColor = .Red // or .Green or .Purple
        pin.animatesDrop = true
        pin.canShowCallout = true
        return pin
    }
    

现在大头针掉落动画了。

【讨论】:

  • 得到了这个错误:Objective-C 方法 'locationManager:didUpdateLocations:' 提供​​的方法 'locationManager(:didUpdateLocations:)' 与可选要求方法 'locationManager(:didUpdateLocations :)' 在协议'CLLocationManagerDelegate'
  • 您是否复制粘贴了该代码?我可以在 Xcode 中很好地运行该代码
  • 我用我现在的状态更新了这个问题,也许我犯了一些错误?我处理错误的方式是否正确? (我是 Xcode 的新手)
  • 只要在确定初始位置后调用它们。可以肯定的是,您可以使用if let latitude = locationManager.location.coordinate.latitude { // we have a latitude } else { // CoreLocation hasn't determined location yet }。如果你从locationManager:didUpdateLocations: 调用你的其他函数,你可以确定有一个位置。
  • mapView 的 userTracking 模式仅用于向用户显示位置,它不会替换实际的位置数据。在您的情况下,您应该尝试同时使用两者。使用userTrackingMode 向用户显示他们的位置,对于其他位置(附近的用户),使用注释。这将清楚地表明你在哪里以及其他人在哪里。为此,如果您还需要在后台执行此操作,您将需要位置管理器和位置功能。
猜你喜欢
  • 2016-04-22
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多