ios - 使用 MapKit 的 swift 3 错误

标签 ios swift xcode

我正在制作 map

在info.plist中添加两个变量“隐私 - 位置何时使用描述”、“隐私 - 位置始终使用描述”

错误: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无效区域”

我的代码:

import UIKit
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate {


    @IBOutlet weak var map: MKMapView!

    var manager: CLLocationManager!


    override func viewDidLoad() {
        super.viewDidLoad()

        manager = CLLocationManager()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()

        let uilpgr = UILongPressGestureRecognizer(target: self, action:Selector(("action:")))
        uilpgr.minimumPressDuration = 2.0
        map.addGestureRecognizer(uilpgr)

    }

    func action(gestureRecognizer:UIGestureRecognizer){
        if gestureRecognizer.state == UIGestureRecognizerState.began{
        let touchPoint = gestureRecognizer.location(in: self.map)
            let newCoordinate = self.map.convert(touchPoint, toCoordinateFrom: self.map)
            let annotation = MKPointAnnotation()

            annotation.coordinate = newCoordinate
            annotation.title = "Meu lugar"
            self.map.addAnnotation(annotation)



        }
    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0]
        let latitude = userLocation.coordinate.latitude
        let longitude = userLocation.coordinate.longitude
        let coordinate = CLLocationCoordinate2DMake(longitude,latitude)
        let latDelta: CLLocationDegrees = 0.01
        let lonDelta: CLLocationDegrees = 0.01
        let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)
        self.map.setRegion(region, animated: false)

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

最佳答案

我认为您忘记导入 CoreLocation 框架。我认为您没有从委托(delegate)方法中获得有效的位置对象。请导入CoreLocation框架并再次测试。

关于ios - 使用 MapKit 的 swift 3 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44056909/

相关文章:

iphone - 为什么我的文件不移动到 "en.lproj",而是移动到 Xcode 中 "English.lproj"之后的新 "Make file Localizable"?

ios - 如何通过绝对路径从 iPhone 上的视频文件读取流

ios - 从 Swift 转换为 Objective C,为什么这段代码只能工作一次?

ios - CallKit:将号码添加到 CXCallDirectoryProvider 的阻止列表

ios - 从子 CollectionViewCell 访问父 tableViewCell

ios - Swift:递归值类型

ios - UIImageView 重复显示在另一个单元格中

ios - 识别字符串中是否有不需要的字符

ios - 配置文件与包标识符不匹配

ios - 无法在控制台(调试区)使用 po 命令