在地图上选择注释时坐标为零Swift
Coordinates are nil when selecting annotation on map Swift
我正在更改我的应用程序的一部分,从 UITableview
中显示一些商店到 MKMapView
中显示它们并且它们显示正常但是当我 select地图上的注释。在 didAdd
中,我打印了它们,它们很好,事实上,注释显示在地图上。
自从我上次使用 MapKit
以来已经有一段时间了,我无法确定问题是出在自定义注释 class 还是其他地方。你能发现我滥用它的地方吗?
一如既往,非常感谢您的时间和帮助。
这是注释 class:
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()
}
当我 select 一个时,我应该从中获取值,但坐标为零:
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
}
}
奇怪的是,我将纬度和经度分别分配给 selectedShop.coordinate
和 selectedShopCoordinates
,然后它会找到 nil。
我现在将它们分配为 selectedShopCoordinates = selectedShop.coordinate
的坐标并且不再崩溃了。
为什么它会在单个坐标度上找到nil,这仍然是个谜。
任何关于为什么的想法将不胜感激。
希望这会帮助其他人挣扎。
干杯
我正在更改我的应用程序的一部分,从 UITableview
中显示一些商店到 MKMapView
中显示它们并且它们显示正常但是当我 select地图上的注释。在 didAdd
中,我打印了它们,它们很好,事实上,注释显示在地图上。
自从我上次使用 MapKit
以来已经有一段时间了,我无法确定问题是出在自定义注释 class 还是其他地方。你能发现我滥用它的地方吗?
一如既往,非常感谢您的时间和帮助。
这是注释 class:
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()
}
当我 select 一个时,我应该从中获取值,但坐标为零:
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
}
}
奇怪的是,我将纬度和经度分别分配给 selectedShop.coordinate
和 selectedShopCoordinates
,然后它会找到 nil。
我现在将它们分配为 selectedShopCoordinates = selectedShop.coordinate
的坐标并且不再崩溃了。
为什么它会在单个坐标度上找到nil,这仍然是个谜。 任何关于为什么的想法将不胜感激。 希望这会帮助其他人挣扎。 干杯