用Swift中的图像设置地图注释

时间:2017-03-13 20:59:09

标签: iphone swift annotations maps

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

     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 个答案:

答案 0 :(得分:0)

有几个问题:

  1. 您似乎没有设置coordinate的{​​{1}}。

  2. mapView(_:viewFor:)的签名不正确。如果在方法中添加断点,则会看到它未被调用。您的地图商店可能会被称为CustomPointAnnotation,但代理方法名称为addressMap,无论您的mapView(_:viewFor:)商家信息的名称如何。