gpt4 book ai didi

ios - 使用 MKAnnotation 继续下一个 View Controller

转载 作者:可可西里 更新时间:2023-11-01 01:20:57 25 4
gpt4 key购买 nike

我是快速编程的新手。我希望当用户点击 MKAnnotationPoint 时移动到下一个 View Controller 。我现在的做法是按下图像 1 顶部显示的“Button”按钮。我的代码:

ma​​pViewController.swift

import UIKit
import MapKit
import CoreLocation

class mapViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, CLLocationManagerDelegate, MKMapViewDelegate {

// MARK: Properties
let pin = UIImage(named: "pin")
var annotationTouched = String()
var viaSegue = MKAnnotationView()

// MARK: MAP
@IBOutlet weak var mapView: MKMapView!


let coreLocationManager = CLLocationManager()
let locations = LocationList().Location

// all locations will be stored on this array
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations [0]

//map zoomed
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.003, 0.003)
//users location
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)

let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
mapView.setRegion(region, animated: true)

self.mapView.showsUserLocation = true
}

// func to change red pin to my custom pin
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
{

if let annotation = annotation as? Locations{
if let view = mapView.dequeueReusableAnnotationView(withIdentifier: annotation.identifier){
return view
}else{
let view = MKAnnotationView(annotation: annotation, reuseIdentifier: annotation.identifier)
view.image = pin
view.isEnabled = true
view.canShowCallout = true
//view.leftCalloutAccessoryView = UIImageView(image: pin)
let btn = UIButton(type: .detailDisclosure)
view.rightCalloutAccessoryView = btn

return view
}
}
return nil
}

// assigning the pin that is selected in the map to the annotation touched variable
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
{
if let annotation = view.annotation as? Locations {
annotationTouched = annotation.title ?? "No title"
}
}


override func viewDidLoad()
{
super.viewDidLoad()

coreLocationManager.delegate = self
//desired accuracy is the best accuracy, very accurate data for the location
coreLocationManager.desiredAccuracy = kCLLocationAccuracyBest
//request authorization from the user when user using my app
coreLocationManager.requestWhenInUseAuthorization()

coreLocationManager.startUpdatingLocation()

mapView.delegate = self

mapView.addAnnotations(locations)
}

// passing the name of the place on the next view controller which is the review view controller (RateViewController)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
let destViewController : RateViewController = segue.destination as! RateViewController
destViewController.placeLabelString = annotationTouched

}


}

Locations.swift

import UIKit
import MapKit

class Locations: NSObject, MKAnnotation {
// required coordinate, title, and the reuse identifier for this annotation
var identifier = "locations"
var title: String?
var coordinate: CLLocationCoordinate2D
//initializer taking a name, a latitude and longitude to populate the title and coordinate for each instance of this object
init(name:String,lat:CLLocationDegrees,long:CLLocationDegrees){
title = name
coordinate = CLLocationCoordinate2DMake(lat, long)
}

}
// Creating the list of the places that will be pinned in map
class LocationList: NSObject {
var Location = [Locations]()
override init(){
Location += [Locations(name: "Dio Con Dio", lat: 40.590130, long: 23.036610)]
Location += [Locations(name: "Paradosiako - Panorama", lat: 40.590102, long:23.036180)]
Location += [Locations(name: "Veranda", lat: 40.607740, long: 23.103044)]
Location += [Locations(name: "Markiz", lat: 40.634252, long: 22.936276)]
Location += [Locations(name: "Moi Lounge Bar", lat: 40.653481, long: 22.994131)]
Location += [Locations(name: "Boulevard Lounge Bar", lat: 40.658462, long: 22.983198)]
Location += [Locations(name: "Ernést Hébrard", lat: 40.631829, long: 22.941014)]
Location += [Locations(name: "Tribeca - All Day & Night Bar", lat: 40.631029, long: 22.942396)]

}
}

image1

image2

最佳答案

创建一个从您的 mapViewController 到您的 destination viewController 的 segue,然后像这样:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
self.performSegue(withIdentifier: "destViewController", sender: nil)
}

Here是我创建的示例项目,向您展示了它是如何工作的。

要编辑 segue 标识符,请执行以下操作: enter image description here

关于ios - 使用 MKAnnotation 继续下一个 View Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43962457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com