gpt4 book ai didi

ios - 我们如何将选定的注释打印到控制台?

转载 作者:搜寻专家 更新时间:2023-11-01 06:57:40 24 4
gpt4 key购买 nike

我是 swift 的新手,这里有一个通过长手势触摸添加注释的解决方案,我们如何将选定的注释打印到控制台?

swift 4

import UIKit
import MapKit

class MapViewController: UIViewController , MKMapViewDelegate {

@IBOutlet weak var mapView : MKMapView!

override func viewDidLoad() {
super.viewDidLoad()


let longPressRecogniser = UILongPressGestureRecognizer(target: self, action: #selector(MapViewController.handleTap(_:)))
longPressRecogniser.minimumPressDuration = 1.0
mapView.addGestureRecognizer(longPressRecogniser)

mapView.mapType = MKMapType.standard

let location = CLLocationCoordinate2D(latitude: 23.0225,longitude: 72.5714)

let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(center: location, span: span)
mapView.setRegion(region, animated: true)

let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "iOSDevCenter-Kirit Modi"
annotation.subtitle = "Ahmedabad"
mapView.addAnnotation(annotation)
}

这里是添加注解的函数:

    @objc func handleTap(_ gestureReconizer: UILongPressGestureRecognizer)
{

let location = gestureReconizer.location(in: mapView)
let coordinate = mapView.convert(location,toCoordinateFrom: mapView)

// Add annotation:
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
mapView.addAnnotation(annotation)
}

最佳答案

您可以使用mapView:didSelect 方法。

例如

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
print("selected annotation", view.annotation)
print("selected annotation", view.annotation?.title)
print("selected annotation", view.annotation?.coordinate)
}

不要忘记在 viewDidLoad 中设置 mapView.delegate = self

关于ios - 我们如何将选定的注释打印到控制台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52521160/

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