swift - 设置注释时为零。mapitems 的价值,mapkit swift 4
问题描述
我正在使用 Xcode 9 和 mapkit 在 swift 4 中创建一个应用程序。现在,我希望用户单击某些按钮,然后在地图上弹出与单击的按钮相对应的注释。示例:单击“餐厅”按钮,以便在给定区域的所有餐厅弹出注释。
将注释添加到我的地图视图时会出现问题。我收到一个致命错误:展开时发现 nil。这是代码:
enum annotations : String {
case restaurants = "Restaurants"
case movieTheater = "Movie Theater"
case shopping = "Shopping"
case bowling = "Bowling"
case park = "Park"
}
//In viewcontroller.swift
var annotationPoints : [MKMapItem] = []
@IBAction func annotationClicked(_ sender: UIButton) {
guard let title = sender.currentTitle, let annotation = annotations(rawValue: title) else {
return
}
//only added two cases to test
switch annotation {
case .restaurants:
getAnnotations(query: annotation.rawValue)
default:
getAnnotations(query: annotation.rawValue)
}
}
func getAnnotations(query : String) {
print("creating request")
let request = MKLocalSearchRequest()
print("setting request")
request.naturalLanguageQuery = query
let span = MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)
let region = MKCoordinateRegion(center: (mapView.userLocation.location?.coordinate)!, span: span)
print("setting region")
request.region = region
print("creating search")
let search = MKLocalSearch(request: request)
print("starting search")
search.start { (response, error) in
guard let response = response else {
return
}
self.annotationPoints = response.mapItems
var annotation : GetAnnotationPins?
print(self.annotationPoints)
for points in self.annotationPoints {
print("the points name is \(String(describing: points.name))")
print("the points number is \(String(describing: points.phoneNumber))")
print("the points coordinates are \(points.placemark.coordinate)")
annotation?.title = points.name
print("the title is \(String(describing: annotation?.title))")
annotation?.subtitle = points.phoneNumber
print("the subtitle is \(String(describing: annotation?.subtitle))")
annotation?.coordinate = points.placemark.coordinate
print("the coordinates are \(String(describing: annotation?.coordinate))")
print("the annotaiton is \(String(describing: annotation))")
self.mapView.addAnnotation(annotation!)
}
//separate class for pin annotation
class GetAnnotationPins : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var subtitle: String?
var title: String?
init(coordinate : CLLocationCoordinate2D, subtitle : String, title : String) {
self.coordinate = coordinate
self.subtitle = subtitle
self.title = title
}
}
//console print out
the points name is Optional("Lin\'s Garden") // restaurant near my location
the points number is Optional("+1 (715) 693-8899")
the points coordinates are CLLocationCoordinate2D(latitude: 44.791603000000002, longitude: -89.700630000000004)
the title is nil
the subtitle is nil
the coordinates are nil
the annotaiton is nil
解决方案
你应该初始化GetAnnotationPins
类。试试下面的代码:
for points in self.annotationPoints {
var annotation = GetAnnotationPins(coordinate: points.placemark.coordinate,
subtitle: points.phoneNumber,
title: points.name)
....
}
并更换
self.mapView.addAnnotation(annotation!)
和
self.mapView.addAnnotation(annotation)
推荐阅读
- tcpdump - tcpdump 是否在未侦听的端口上捕获数据包?
- python-3.x - 为什么 python 中的 isallnum() 对包含数字“123”的字符串返回“True”
- c# - 如何根据子属性过滤父实体中的子实体?
- database - 如何使用没有热分区的 Amazon DynamoDB 为论坛建模?
- build - Cmake(GUI)在msys64中找不到libpng
- vue.js - 带有 Vue-cli3 和静态站点的部署问题
- c++ - 库。检查窗口是否重叠
- image-processing - 如何用flutter实现过饱和效果
- c# - Elasticsearch _update_by_query 无论如何都会更新文档,即使在版本冲突的情况下也是如此
- python - Tarfile/Zipfile extractall() 更改某些文件的文件名