0

我正在尝试创建一个使用 MapKit 的应用程序,并且我想在地图上创建注释,但我正在努力将图像放在地图注释上。

我是 Swift 编程的初学者,我一直在学习本教程,但是我在网上找不到任何可以帮助您实现图像的地方,大多数指南都使用 Objective C。

这是我的注释类,在我的视图控制器中调用。

注意:我有我的小图像,我想在我的 images.xcassets 文件夹中显示为“mapAnnotation”

let buildingName = location(title: "Building Name",
                            locationName: "Location",
                            coordinate: CLLocationCoordinate2D(latitude: NUMBER, longitude: NUMBER))

theMapview.addAnnotation(buildingName)

这是注释类...

class location: NSObject, MKAnnotation {
    let title: String
    let locationName: String
    let coordinate: CLLocationCoordinate2D

    //Retrieve an image
    var image = UIImage(named: "mapAnnotation")

    init(title: String, locationName:String, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.locationName = locationName
        self.coordinate = coordinate
        self.image
        super.init()
    }

    var subtitle: String {
        return locationName
    }
}
4

1 回答 1

0

你在正确的轨道上。

MKAnnotation 是一个抽象对象,用于存储有关地图注释的信息。

现在您需要创建 MKAnnotationView 的自定义子类。这是一个实际的视图对象,它被添加到注释位置的地图视图中。它可以像 UIImageView 的子类一样简单。

然后你需要实现 MKMapViewDelegate 方法mapView:viewForAnnotation:。该方法将注释作为输入,并返回一个 MKAnnotationView。

有关更多信息,请参阅 Xcode 文档MKAnnoationViewmapView:viewForAnnotation:方法。

于 2015-05-12T15:56:30.260 回答