gpt4 book ai didi

swift - CLLocationManager、核心数据和数组

转载 作者:行者123 更新时间:2023-11-28 15:58:45 24 4
gpt4 key购买 nike

我已经获得了这个功能,并且非常接近让它发挥作用!我想我遇到了转换问题。我可以将坐标放入具有 3 个属性纬度( double )、经度( double )和时间(日期)的实体中。这行得通,我可以将内容打印到屏幕上,看起来不错。但是当我尝试提取并用作 map 上的坐标时,它不起作用。我认为这是因为它们没有被识别为真实坐标。有什么想法吗?

   func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
//ARRAY for lat/long data
var latArr:[Double] = []
var longArr:[Double] = []
var timeArr:[Date] = []

let location = locations[0]
let annotation = MKPointAnnotation()
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let myLocation = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
//annotation.coordinate = myLocation
//annotation.title = "\(location.timestamp)"
//self.mapView.addAnnotation(annotation)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
self.mapView.setRegion(region, animated: true)
self.mapView.showsUserLocation = false //SET THIS
latitude.text = "latitude: " + "\(location.coordinate.latitude)"
longitude.text = "longitude: " + "\(location.coordinate.longitude)"
altitude.text = "altitude: " + "\(location.altitude)"
speed.text = "speed: " + "\(location.speed)"
timestamp.text = "timestamp: " + "\(location.timestamp)"

//START -- Core Data
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext

let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Data")
request.returnsObjectsAsFaults = false

let data = NSEntityDescription.insertNewObject(forEntityName: "Data", into: context)
//let data = NSNumber(double: self.location?.coordinate.latitude)

let dlat = Double("\(location.coordinate.latitude)")
let dlong = Double("\(location.coordinate.longitude)")

data.setValue(dlat, forKey: "lat")
data.setValue(dlong, forKey: "long")
data.setValue(location.timestamp, forKey: "time")
//END -- Core Data

do
{
try context.save()
//print("Saved")
}
catch
{

}

do
{
var lat: Double = 0
var long: Double = 0
let t = NSDate()
let results = try context.fetch(request)

if results.count > 0
{
for result in results as! [NSManagedObject]
{
if let datalat = result.value(forKey: "lat") as? Double
{
//latArr.append(datalat)
let lat = datalat
print(lat)
}
if let datalong = result.value(forKey: "long") as? Double
{
//longArr.append(datalong)
let long = datalong
print(long)
}
if let time = result.value(forKey: "time") as? Date
{
//timeArr.append(time)
let t = time
print(t)
}
let mLocation = CLLocationCoordinate2DMake(lat, long)
annotation.coordinate = mLocation
annotation.title = "\(t)"
self.mapView.addAnnotation(annotation)
}
}
}
catch
{

}

最佳答案

我将您的代码与我在类似场景中使用的函数进行了比较,并合并了所有差异(并为简洁起见进行了一些编辑,您可以接受或离开)。相关部分如下:

    let request = NSFetchRequest<Data>(entityName: "Data")
let annotations: [MKAnnotation] = []

annotation.title = “\(t)”

for result in results
{
if data.lat != nil && data.lon != nil
{
annotation.coordinate = CLLocationCoordinate2D(latitude: data.lat.doubleValue, longitude: data.lon.doubleValue)
annotations.append(annotation)
}
self.mapView.addAnnotations(annotations)
self.mapView.showAnnotations(annotations, animated: false)
}

我的代码有效,但它是 Swift 3.0,因此如果您使用的是 2.2. 或 2.3,则一些差异可能是由于较早版本的更改所致。我知道我需要将它从 NSNumber(从 CoreData)推到 Double,但我认为如果这是问题所在,你会得到一个编译错误 - 不确定为什么不是。正如我在评论中提到的,如果航路点只是在可见 map 区域之外,显示注释将有所帮助。

除此之外,您的代码在我看来还不错 - 让我知道它是否仍然有效。

关于swift - CLLocationManager、核心数据和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41385737/

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