gpt4 book ai didi

iphone - 在 Swift 中使用图像设置 map 注释

转载 作者:行者123 更新时间:2023-11-30 12:36:42 25 4
gpt4 key购买 nike

我正在尝试将 map 注释更改为我在静态单元格中制作的自定义图像。 map 的注释有效,但是当我尝试将图像放入 map 中时, map 会缩放到该位置但不显示图像。

     import UIKit
import MapKit
import CoreLocation


class ContactsTableViewController: UITableViewController, MKMapViewDelegate, CLLocationManagerDelegate {
}

override func viewDidLoad() {
super.viewDidLoad()


self.addressMap.delegate = self
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath) as! customCell

if let _ = currentAnnotation {
addressMap.removeAnnotation(currentAnnotation!)
}


let contact = contacts.contactWithIndex((indexPath as NSIndexPath).row)
print(contact)
cell.nameLabel.text = contact.name
cell.addressLabel.text = contact.address

let country = "USA"
let city = "Cicero"
let street = contact.address

// Create Address String
let address = "\(country), \(city), \(street)"

// Geocode Address String
geocoder.geocodeAddressString(address) { (placemarks, error) in
// Process Response
self.processResponse(withPlacemarks: placemarks, error: error)
var location: CLLocation?

if let placemarks = placemarks, placemarks.count > 0 {
location = placemarks.first?.location
}

if let location = location {
let coordinate = location.coordinate
print("Custom cell: \(coordinate.latitude), \(coordinate.longitude)")

//Drops pin to current MKPointAnnotationis logged in
self.currentAnnotation = MKPointAnnotation()
self.currentAnnotation?.coordinate = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)

let image1 = CustomPointAnnotation()
image1.imageName = "mapPin.png"
self.addressMap.addAnnotation(image1)

//Pins the location on the mapview
//cell.addressMap.addAnnotation(self.currentAnnotation!)
cell.addressMap.addAnnotation(image1)
//Auto zoom into user location
let span = MKCoordinateSpanMake(0.02, 0.02)
let region = MKCoordinateRegion(center: (self.currentAnnotation?.coordinate)!, span: span)
cell.addressMap.setRegion(region, animated: true)

} else {
print("No Matching Location Found")
}
}
}


func addressMap(_ addressMap: MKMapView, viewFor annotation: MKAnnotation!) -> MKAnnotationView? {
if !(annotation is CustomPointAnnotation) {
return nil
}

let reuseId = "test"
var anView = addressMap.dequeueReusableAnnotationView(withIdentifier: reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView?.canShowCallout = true
} else {
anView?.annotation = annotation
}

//Set annotation-specific properties **AFTER**
//the view is dequeued or created...

let cpa = annotation as! CustomPointAnnotation
anView?.image = UIImage(named:cpa.imageName)

return anView
}

这是注释的类

 import UIKit
import MapKit

class CustomPointAnnotation: MKPointAnnotation {
var imageName: String!

}

如果有人能告诉我我做错了什么,导致我的图像无法显示为注释,那就太棒了。

最佳答案

几个问题:

  1. 您似乎从未设置过 CustomPointAnnotation坐标

  2. mapView(_:viewFor:) 的签名是不正确的。如果您在方法中添加断点,您会看到它没有被调用。您的 map socket 可能名为 addressMap,但委托(delegate)方法名称为 mapView(_:viewFor:),无论您的 MKMapView 的名称如何 socket 引用。

关于iphone - 在 Swift 中使用图像设置 map 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42773294/

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