gpt4 book ai didi

swift - 向 map View 添加注释时出错

转载 作者:行者123 更新时间:2023-11-30 12:47:54 24 4
gpt4 key购买 nike

我有一个应用程序,在其中我从核心数据模型获取数据,然后我希望将其呈现在 map 上。当我向 map View 添加注释时,出现错误:无法将类型 'Swift._ContigouslyArrayStorage<>' () 的值转换为 'MKAnnotation' ()。同样的逻辑适用于 Swift 2,但显然不适用于 Swift 3。在哪里以及如何将注释添加到 map View 。代码是:

  override func viewDidAppear(_ animated: Bool) {

var rainFallGauges = [RainFallGauge]()
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "RainFallGauge")

do {
rainFallGauges = try coreDataStack.context.fetch(fetchRequest) as! [RainFallGauge]

obsCount = rainFallGauges.count
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}

if obsCount == 0 {

let alert = UIAlertController(title: "Sorry", message: "No gauges found", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
} else {

for rainFallGauge in rainFallGauges {

resultsGauge = rainFallGauge.value(forKey: "gaugeName") as? String
resultsLatitude = rainFallGauge.value(forKey: "gaugeLatitude") as? String
resultsLongitude = rainFallGauge.value(forKey: "gaugeLongitude") as? String
print(resultsGauge)

if (resultsLatitude != nil && resultsLongitude != nil) && (resultsLatitude?.isEmpty == false && resultsLongitude?.isEmpty == false) {
let latNSString = NSString(string: resultsLatitude!)
setLatitude = latNSString.doubleValue
print(setLatitude)

let longNSString = NSString(string: resultsLongitude!)
setLongitude = longNSString.doubleValue
print(setLongitude)

self.gauges.append(Gauges(gaugeTitle: resultsGauge, latitude: setLatitude, longitude: setLongitude))
print(gauges.count)
} else if gauges.count == 0 {
let alert = UIAlertController(title: "Sorry", message: "No gauges", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}

self.gaugeMap.delegate = self
self.gaugeMap.addAnnotation(self.gauges as! MKAnnotation)

gaugeMap.mapType = .hybrid

let rectToDisplay = self.gauges.reduce(MKMapRectNull) {
(mapRect: MKMapRect, gauge: Gauges) -> MKMapRect in
let placePointRect = MKMapRect(origin: gauge.location.mapPoint, size: MKMapSize(width: 0, height: 0))

return MKMapRectUnion(mapRect, placePointRect)
}

我将上面的数组“gauges”声明为:

    var gauges = [Gauges]()

并创建了类标尺来保存注释的相关信息。

任何能够指导我了解 addAnnotation 语句的正确语法及其位置的建议将不胜感激。

最佳答案

感谢您的见解。我误读了旧应用程序中的这一行,实际上是:

self.speciesMap.addAnnotations(self.places)

我通过简化并解决了我的问题:

for rainFallGauge in rainFallGauges {

resultsGauge = rainFallGauge.value(forKey: "gaugeName") as? String
resultsLatitude = rainFallGauge.value(forKey: "gaugeLatitude") as? String
resultsLongitude = rainFallGauge.value(forKey: "gaugeLongitude") as? String
print(resultsGauge)

if (resultsLatitude != nil && resultsLongitude != nil) && (resultsLatitude?.isEmpty == false && resultsLongitude?.isEmpty == false) {
let latNSString = NSString(string: resultsLatitude!)
setLatitude = latNSString.doubleValue
print(setLatitude)

let longNSString = NSString(string: resultsLongitude!)
setLongitude = longNSString.doubleValue
print(setLongitude)

gaugeMap.addAnnotation(OneGauge(title: resultsGauge, subtitle: "", coordinate: CLLocationCoordinate2D(latitude: setLatitude, longitude: setLongitude)))

}
}
}
gaugeMap.mapType = .hybrid

关于swift - 向 map View 添加注释时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41389907/

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