当前位置: 首页 > 面试题库 >

在地图上重复图钉注释

楚煜
2023-03-14
问题内容

我刚刚开始学习Swift。

题:

当我在地图上触摸以放置图钉注释并拖动手指时,它将创建
重复的注释行。

import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    @IBOutlet weak var map: MKMapView!
    var manager:CLLocationManager!
    override func viewDidLoad() {
        super.viewDidLoad()
        //Manager for current location
        manager = CLLocationManager()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
        //Getting touch Gesture to add bookmark
        let uilpgr = UILongPressGestureRecognizer(target: self, action: "action:")
        uilpgr.minimumPressDuration = 1
        uilpgr.numberOfTouchesRequired = 1
        map.addGestureRecognizer(uilpgr)
    }
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0]
        let latitude:CLLocationDegrees = userLocation.coordinate.latitude
        let longitude:CLLocationDegrees = userLocation.coordinate.longitude
        let latDelta:CLLocationDegrees = 0.002
        let lonDelta:CLLocationDegrees = 0.002
        let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
        let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
        map.setRegion(region, animated: true)
    }
    func action(gestureRecognizer: UIGestureRecognizer) {
        let touchPoint = gestureRecognizer.locationInView(self.map)
        let newCoordinate: CLLocationCoordinate2D = map.convertPoint(touchPoint, toCoordinateFromView: self.map)
        print(newCoordinate)
        let annotation = MKPointAnnotation()
        annotation.coordinate = newCoordinate
        annotation.title = "New Place"
        annotation.subtitle = "One day I"ll go here..."
        map.addAnnotation(annotation)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

问题答案:

Xcode 8.2•Swift 3.0.2

您只需要检查手势识别器的状态,并确定是否可以
。返回。只需在操作方法顶部添加if条件:

func action(_ gestureRecognizer: UIGestureRecognizer) {

    if gestureRecognizer.state != UIGestureRecognizerState.began {
        return
    }

    //  your code

如果要允许用户在触摸时移动图钉,则
需要更改手势识别器的状态并在
gestureRecognizer.state更改时更新注释坐标:

func action(_ gestureRecognizer: UIGestureRecognizer) {
    switch gestureRecognizer.state {
    case .began:
        let annotation = MKPointAnnotation()
        annotation.coordinate = mapView.convert(gestureRecognizer.location(in: mapView), toCoordinateFrom: mapView)
        annotation.title =  "Untitled"
        mapView.addAnnotation(annotation)
    case .changed:
        if let annotation = (mapView.annotations.filter{$0.title! == "Untitled" }).first as? MKPointAnnotation {
            annotation.coordinate =  mapView.convert(gestureRecognizer.location(in: mapView), toCoordinateFrom: mapView)
        }
    case .cancelled:
        if let annotation = (mapView.annotations.filter{$0.title! == "Untitled" }).first as? MKPointAnnotation {
            mapView.removeAnnotation(annotation)
        }
    // you can also prompt the user here for the annotation title
    case .ended:
        if let annotation = (mapView.annotations.filter{$0.title! == "Untitled" }).first as? MKPointAnnotation {
            let alert = UIAlertController(title: "New Annotation", message: "", preferredStyle: .alert)
            var inputTextField: UITextField?
            alert.addAction(UIAlertAction(title: "Add", style: .default) { _ in
                if  let annotationTitle = inputTextField?.text {
                    annotation.title =  annotationTitle
                    annotation.subtitle = "Lat:\(String(format: "%.06f", annotation.coordinate.latitude))  Lon:\(String(format: "%.06f", annotation.coordinate.longitude))"
                }
            })
            alert.addTextField(configurationHandler: { textField in
                textField.placeholder = "Place Description"
                inputTextField = textField
            })
            alert.addAction(UIAlertAction(title: "Cancel", style: .cancel){ _ in
                self.mapView.removeAnnotation(annotation)
            })
            present(alert, animated: true, completion: nil)
        }
    default:
        print("default")
    }
}


 类似资料:
  • 图钉能把页面上的某个元素一直固定在那里,页面右侧的导航就是一个活生生的例子。 示例代码 $('.pushpin-demo-nav').each(function() { var $this = $(this); var $target = $('#' + $(this).attr('data-target')); $this.pushpin({ top: $target.o

  • 所以我在我的VS Xamarin项目上实现了谷歌地图。我知道有一种方法可以简化我的代码,但我不知道我还能做些什么。我的地图上有一堆标记,每次我创建一个标记时,我都会创建一个整体,所以我想简化这个过程,如果可能的话,从Excel文件中提取信息。 我的代码: 这里我只展示了2个管脚,但实际上,我有30个管脚。我怎样才能让这更简单?非常感谢!:)

  • 问题内容: 我正在尝试在Swift中更改MKMapView上的大头针图像,但不幸的是它不起作用。知道我在做什么错吗?我在这里看到了一些示例,但是没有用。 GreenDot图片可在其他地方使用。 问题答案: 不要忘记设置: 并确保您实施该协议。 如果您忘记执行此操作,则不会为您的地图调用实现。 此外,它看起来像破坏自定义图像。您必须将其设置为,或使用(没有属性)。

  • 我有下面的地图地图,并想过滤它的基础上的一个值。结果应分配回同一地图。请告诉我什么是最好的方法。 我尝试了如下操作,但无法访问employee对象

  • 我有这样的代码: 我写了它,但我想知道。是否可以在不创建新地图的情况下编写它。类似这样的东西(不正确的代码):

  • 我正在制作一个使用swing的游戏,其中有一个用于选择关卡的JPanel和用于导航到关卡的JButtons。我还为整个级别屏幕创建了一个.png图像--是否可以在JButtons的顶部呈现该图像?如果不是,最简单的替代方案是什么?