[在地图Swift上选择注释时坐标为零

问题描述 投票:0回答:1

我正在更改应用程序的一部分,从在UITableview中显示一些商店到在MKMapView中显示,它们显示得很好,但是当我在地图上选择注释时,坐标值就为零。在didAdd中,我将它们打印出来,并且效果很好,实际上注释显示在地图上。自从我上次使用MapKit以来已经有一段时间了,我无法发现问题出在自定义注释类还是其他地方。您能发现我在哪里滥用它吗?一如既往,非常感谢您的时间和帮助。这是注释类:

class ShopAnnotation: NSObject , MKAnnotation {

    var title: String?
    var coordinate:CLLocationCoordinate2D
    var image: UIImage?
    var shopName: String?
    var shopLogoUrl: String?

    init(title: String, iconImage: UIImage, coordinate:CLLocationCoordinate2D, shopName: String, shopLogoUrl: String) {


        self.coordinate = coordinate
        self.title = title
        self.shopName = shopName
        self.shopLogoUrl = shopLogoUrl
        self.image = iconImage

    }
}

比我有一个数组要从::>中取值

var availableShopsArray:[(name:String, logoUrl:String, homeLat: String, homeLong: String)] = []

然后遍历并在地图上放置注释:

func displayShops() {
        mapView.removeAnnotations(mapView.annotations)

        for shop in availableShopsArray {
            let coordinates: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: Double(shop.homeLat)!, longitude: Double(shop.homeLong)!)
            let title = "Route Mid"
            let image = UIImage(named: title)!
            let shopAnn: ShopAnnotation = ShopAnnotation(title: title, iconImage: image, coordinate: coordinates, shopName: shop.name, shopLogoUrl: shop.logoUrl)
            mapView.addAnnotation(shopAnn)
        }
//        self.availableShopsTableview.reloadData()
    }

当我选择一个时,我应该从中取值,但坐标为nil:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        guard let selectedShop = view.annotation as? ShopAnnotation else {return}
        self.shopLogoUrl = selectedShop.shopLogoUrl!
        self.selectedShopCoordinates.latitude = selectedShop.coordinate.latitude
        self.selectedShopCoordinates.longitude = selectedShop.coordinate.longitude
        self.selectedShopName = selectedShop.shopName

//        calculateBookingType()
        self.performSegue(withIdentifier: "bookingToCartSegue", sender: self)
    }

这是viewForAnnotation

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { // rajish version
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "")

        if annotation is MKUserLocation{
            return nil
        } else {
            print("Annotation.coordinates are: \(String(describing: annotation.coordinate))")
//            print("annotation title is: \(String(describing: annotation.title!))")
            annotationView.image = UIImage(named:(annotationView.annotation?.title)! ?? "")

            annotationView.canShowCallout = true
            let transform = CGAffineTransform(scaleX: 0.27, y: 0.27) // alert annotation's icons size
            annotationView.transform = transform
            // makin allerts draggable
            annotationView.isDraggable = false
            return annotationView
        }
    }

我正在更改应用程序的一部分,从在UITableview中显示一些商店以在MKMapView上显示它们,并且它们显示得很好,但是当我在...上选择注释时,我得到的坐标值为零]]

ios swift mapkit mkannotation mkannotationview
1个回答
0
投票
[很奇怪,我将纬度和经度分别从selectedShopCoordinates分配给selectedShop.coordinate,因此,它的值为零。我现在将它们指定为selectedShopCoordinates = selectedShop.coordinate坐标,并且不再崩溃。

为什么会发现单个坐标度为零仍然是一个谜。任何想法,为什么将不胜感激。希望这可以帮助其他人为此奋斗。干杯

© www.soinside.com 2019 - 2024. All rights reserved.