将数据从地图注释传递到新的视图控制器
Passing data from a map annotation to a new view controller
目前,我有一个在地图上显示位置的地图视图。当在地图上选择位置注释时,它会打开一个新的视图控制器。我如何将地图上注释中显示的地点名称传递给新的视图控制器?
这是从注释中打开的新视图控制器的图片。
这是 mapView 的代码。
import UIKit
import MapKit
struct PlacesOnMap {
var name: String
var latitude: Double
var longitude: Double
init(name: String, latitude: Double, longitude: Double) {
self.name = name
self.latitude = latitude
self.longitude = longitude
}
}
class MapViewController: UIViewController {
@IBOutlet var mapView: MKMapView!
var places = [PlacesOnMap(name: place 1),
PlacesOnMap(name: place 2)
]
override func viewDidLoad() {
super.viewDidLoad()
setupAnnotations()
}
func setupAnnotations() {
let places = places.map { placeOnMap -> MKPointAnnotation in
let place = MKPointAnnotation()
place.coordinate = CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
place.title = placeOnMap.name
return place
}
mapView.addAnnotations(places)
}
extension MapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let annotationTitle = view.annotation?.title else { return }
let storyboard: UIStoryboard = UIStoryboard(name: "MyCommunityStoryboard", bundle: nil)
let destVC = storyboard.instantiateViewController(withIdentifier: "DetailMapItemsViewController") as! DetailMapItemsViewController
destVC.placeNameText = annotationTitle!
navigationController?.pushViewController(destVC, animated: true)
}
}
这是我目前的 DetailViewController。
import UIKit
struct PlacesDetailView {
var name: String
init(name: String) {
self.name = name
}
class DetailMapItemsViewController: UIViewController {
@IBOutlet weak var nameOfPlaceLabel: UILabel!
var placeNameText: String = ""
override func viewDidLoad() {
super.viewDidLoad()
nameOfPlaceLabel?.text = placeNameText
}
}
您可以使用以下代码将数据传递给 DetailViewController
。
import UIKit
import MapKit
struct PlacesOnMap {
var name: String
var latitude: Double
var longitude: Double
init(name: String, latitude: Double, longitude: Double) {
self.name = name
self.latitude = latitude
self.longitude = longitude
}
}
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView1: MKMapView!
var places1 = [ PlacesOnMap(name: "Bangalore", latitude: 12.9716, longitude: 77.5946), PlacesOnMap(name: "Mysore", latitude: 12.2958, longitude: 76.6394)]
override func viewDidLoad() {
super.viewDidLoad()
setupAnnotations()
mapView1.delegate = self
}
func setupAnnotations() {
let places = places1.map { placeOnMap -> MKPointAnnotation in
let place = MKPointAnnotation()
place.coordinate = CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
place.title = placeOnMap.name
return place
}
mapView1.addAnnotations(places)
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let annotationTitle = view.annotation?.title else
{
print("Unable to retrieve details")
return
}
print("User tapped on annotation with title: \(annotationTitle!)")
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let home = storyBoard.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
home.placeNameText = annotationTitle!
navigationController?.pushViewController(home, animated: true);
}
}
目前,我有一个在地图上显示位置的地图视图。当在地图上选择位置注释时,它会打开一个新的视图控制器。我如何将地图上注释中显示的地点名称传递给新的视图控制器?
这是从注释中打开的新视图控制器的图片。
这是 mapView 的代码。
import UIKit
import MapKit
struct PlacesOnMap {
var name: String
var latitude: Double
var longitude: Double
init(name: String, latitude: Double, longitude: Double) {
self.name = name
self.latitude = latitude
self.longitude = longitude
}
}
class MapViewController: UIViewController {
@IBOutlet var mapView: MKMapView!
var places = [PlacesOnMap(name: place 1),
PlacesOnMap(name: place 2)
]
override func viewDidLoad() {
super.viewDidLoad()
setupAnnotations()
}
func setupAnnotations() {
let places = places.map { placeOnMap -> MKPointAnnotation in
let place = MKPointAnnotation()
place.coordinate = CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
place.title = placeOnMap.name
return place
}
mapView.addAnnotations(places)
}
extension MapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let annotationTitle = view.annotation?.title else { return }
let storyboard: UIStoryboard = UIStoryboard(name: "MyCommunityStoryboard", bundle: nil)
let destVC = storyboard.instantiateViewController(withIdentifier: "DetailMapItemsViewController") as! DetailMapItemsViewController
destVC.placeNameText = annotationTitle!
navigationController?.pushViewController(destVC, animated: true)
}
}
这是我目前的 DetailViewController。
import UIKit
struct PlacesDetailView {
var name: String
init(name: String) {
self.name = name
}
class DetailMapItemsViewController: UIViewController {
@IBOutlet weak var nameOfPlaceLabel: UILabel!
var placeNameText: String = ""
override func viewDidLoad() {
super.viewDidLoad()
nameOfPlaceLabel?.text = placeNameText
}
}
您可以使用以下代码将数据传递给 DetailViewController
。
import UIKit
import MapKit
struct PlacesOnMap {
var name: String
var latitude: Double
var longitude: Double
init(name: String, latitude: Double, longitude: Double) {
self.name = name
self.latitude = latitude
self.longitude = longitude
}
}
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView1: MKMapView!
var places1 = [ PlacesOnMap(name: "Bangalore", latitude: 12.9716, longitude: 77.5946), PlacesOnMap(name: "Mysore", latitude: 12.2958, longitude: 76.6394)]
override func viewDidLoad() {
super.viewDidLoad()
setupAnnotations()
mapView1.delegate = self
}
func setupAnnotations() {
let places = places1.map { placeOnMap -> MKPointAnnotation in
let place = MKPointAnnotation()
place.coordinate = CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
place.title = placeOnMap.name
return place
}
mapView1.addAnnotations(places)
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let annotationTitle = view.annotation?.title else
{
print("Unable to retrieve details")
return
}
print("User tapped on annotation with title: \(annotationTitle!)")
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let home = storyBoard.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
home.placeNameText = annotationTitle!
navigationController?.pushViewController(home, animated: true);
}
}