gpt4 book ai didi

xcode - 在 Swift Xcode 7.3 中查找当前位置

转载 作者:行者123 更新时间:2023-11-30 13:30:43 26 4
gpt4 key购买 nike

我目前正在开发一个项目,需要我在应用程序中获取用户位置。最初我将其编码为仅显示工作 map View ,但后来想让它显示用户位置。我不断收到错误 EXC_BAD_INSTRUCTION 线上

self.mapView.showsUserLocation = true

import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate
{
@IBOutlet weak var mapView: MKMapView!

let locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()

self.locationManager.delegate = self

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

self.locationManager.requestWhenInUseAuthorization()

self.locationManager.startUpdatingLocation()

self.mapView.showsUserLocation = true
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

// MARK: - Location Delegate Methods

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

let location = locations.last
let center = CLLocationCoordinate2DMake(location!.coordinate.latitude, location!.coordinate.latitude)

let region = MKCoordinateRegion(center: center, span:MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))

self.mapView.setRegion(region, animated: true)

self.locationManager.startUpdatingLocation()
}

最佳答案

这可能对你有帮助。

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate,NSURLConnectionDelegate,UITableViewDataSource,UITableViewDelegate{

let locationManager = CLLocationManager()

var latitude = 0.00;
var Longitude = 0.00;




override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestLocation()
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()

// Do any additional setup after loading the view, typically from a nib.
}


@IBOutlet weak var myLabel: UILabel!



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




func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location11 = locations.first {
print("Found User's location: \(location11)")
print("Latitude: \(location11.coordinate.latitude) Longitude: \(location11.coordinate.longitude)")
latitude = location11.coordinate.latitude
Longitude = location11.coordinate.longitude
startConnection()
}
}


func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Failed to find user's location: \(error.localizedDescription)")
}

}

在您的 plist 文件中添加权限。

关于xcode - 在 Swift Xcode 7.3 中查找当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36598748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com