[英]How to show multiple annotations on a map?
它只显示最后一个注释。 我想展示所有这些。 怎么做? 我也收到错误。 这是“libMobileGestalt MobileGestalt.c:1647:无法检索区域信息”
视图控制器:
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
let locations = [
Location(title: "New York, NY", latitude: 40.713054, longitude: -74.007228),
Location(title: "Los Angeles, CA", latitude: 34.052238, longitude: -118.243344),
Location(title: "Chicago, IL", latitude: 41.883229, longitude: -87.632398)
]
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
override func viewDidLoad() {
super.viewDidLoad()
for location in locations {
let annotation = MKPointAnnotation()
annotation.title = location.title
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
let region = MKCoordinateRegion(center: annotation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005))
mapView.addAnnotation(annotation)
mapView.setRegion(region, animated: true)
}
}
}
数据模型:
import UIKit
struct Location {
let title: String
let latitude: Double
let longitude: Double
}
您可以使用addAnnotations
添加多个注释。 您也可以只调用一次setRegion
override func viewDidLoad() {
super.viewDidLoad()
var annotations = [MKAnnotation]()
for location in locations {
let annotation = MKPointAnnotation()
annotation.title = location.title
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
annotations.append(annotation)
}
mapView.addAnnotations(annotations)
if let lastAnnotation = annotations.last {
let region = MKCoordinateRegion(center: lastAnnotation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005))
mapView.setRegion(region, animated: true)
}
}
谷歌搜索大约一个小时后,我可以让它工作。 这是一个解决方案,但我认为它不是一个有效的解决方案。 有一种使用可重用注释的方法。 如果有人使用该方法,请分享。
这是我如何做到的。
视图控制器:
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
override var preferredStatusBarStyle: UIStatusBarStyle {
.darkContent
}
@IBOutlet weak var mapView: MKMapView!
let locations = [
Location(title: "Dr. James Golf Course", latitude: 40.003252, longitude: -86.0655897),
Location(title: "Avon Town Hall", latitude: 39.7636057, longitude: -86.4080829),
Location(title: "Brookside Park", latitude: 39.7897185, longitude: -86.1070623)
]
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
annotationsOnMap()
}
func annotationsOnMap() {
for location in locations {
let annotations = MKPointAnnotation()
annotations.title = location.title
annotations.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
mapView.addAnnotation(annotations)
let locationCoordinate2d = annotations.coordinate
let span = MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1)
let region = MKCoordinateRegion(center: locationCoordinate2d, span: span)
mapView.setRegion(region, animated: true)
}
}
}
数据模型:
import UIKit
struct Location {
let title: String
let latitude: Double
let longitude: Double
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.