【问题标题】:Mapview - protection against coordinates found nilMapview - 防止发现坐标为零
【发布时间】:2023-03-09 04:08:01
【问题描述】:

我有从 CloudKit 下载记录的 mapview。每条记录的坐标基于正向地理编码器,用户在其中添加地址(例如:纽约,NY)并获得纬度和经度

当前型号如下:

class Place: NSObject
{
  var name: String
  var address: String
  var comment: String?
  var photo: UIImage?
  var rating: Int
  var location: CLLocation?
  var identifier: String
  var record: CKRecord!

  init(record: CKRecord)
  {
    self.record = record
    self.name = record.valueForKey(placeName) as! String
    self.address = record.valueForKey(placeAddress) as! String
    self.comment = record.valueForKey(placeComment) as? String

    if let photoAsset = record.valueForKey(placePhoto) as? CKAsset
    {
      self.photo = UIImage(data: NSData(contentsOfURL: photoAsset.fileURL)!)
    }

    self.rating = record.valueForKey(placeRating) as! Int
    self.location = record.valueForKey(placeLocation) as? CLLocation

    self.identifier = record.recordID.recordName
  }

  // MARK: Map Annotation

  var coordinate: CLLocationCoordinate2D {
    get {
      return location!.coordinate
    }
  }

这是我在地图视图上放置每个图钉的方法。

func placePins()
  {
    for place: Place in self.places
    {

        let location = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude)
        let dropPin = CustomPointAnnotation(place: place)
        dropPin.pinCustomImageName = "customPin"
        dropPin.coordinate = location
        dropPin.title = place.title
        dropPin.subtitle = place.subtitle
        dropPin.name = place.name
        dropPin.image = place.photo
        mapView.addAnnotation(dropPin)

    }
  }

由于前向地理编码器不是最可靠的方法,我该如何修复它们以防止任何没有坐标的记录?

【问题讨论】:

    标签: ios swift annotations mkmapview fatal-error


    【解决方案1】:

    怎么样

    for place: Place in self.places
        {
            if (place.location == nil) {
                continue;
            }
            ...
        }
    

    不知道有什么问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多