2016-11-28 9 views
1

これまでに行ったことは地図を作成することでした。次に、ユーザーの位置を表示し、地図を中心に移動するように表示します(車など)注釈ピン付きの長押しのジェスチャ認識機能を作成する

しかし、今では、ユーザーが入力を行うとピンが落ちるようにしたいと思います。私はチュートリアルで苦労し、シミュレータがクラッシュする。

mapViewにピンを落とすように、どうすればlongPressGesturerecognizerを追加しますか?ここで

は私のコード - あなたはまた、ドロップピン用のタップジェスチャーを使用することができます

import UIKit 
import MapKit 
import CoreLocation 

class Page2: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{ 

    @IBOutlet var mapView: MKMapView! 
    let locationManager = CLLocationManager() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.locationManager.delegate = self 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     self.locationManager.requestWhenInUseAuthorization() 
     self.locationManager.startUpdatingLocation() 
     //blue dot on the map 
     self.mapView.showsUserLocation = true 
     //tracking mode on 
     self.mapView.userTrackingMode = .follow 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // location Manager Delegate center user on map 
    private func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 

     let location = locations.last 
     let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: (location?.coordinate.longitude)!) 
     let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)) //zoom on map 
     self.mapView.setRegion(region, animated: true) 
     self.locationManager.stopUpdatingLocation() 
    } 

    // print errors 
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error){ 
     print("Errors: " + error.localizedDescription) 
    } 
} 

答えて

5

にタップジェスチャー

let tapGesture = UITapGestureRecognizer(target: self, action:#selector(AddressViewController.handleTap(_:))) 
    tapGesture.delegate = self 
    mapView.addGestureRecognizer(tapGesture) 

ドロップピンを追加CLLocationManagerDelegate方法のlocationManager(_:didUpdateLocations:)のスウィフト3署名でまず第一に変更されたので、あなたは次のようにその方法を変更する必要があります。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    //your code and don't forgot to remove private 
} 

あなたはviewDidLoadであなたのmapViewにこのようなmapView、最初addGestureRecognizerlongGestureを使用することができます。

let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(addAnnotationOnLongPress(gesture:))) 
longPressGesture.minimumPressDuration = 1.0 
self.mapView.addGestureRecognizer(longPressGesture) 

ここにアクションを追加します。UILongPressGestureRecognizer

func addAnnotationOnLongPress(gesture: UILongPressGestureRecognizer) { 

    if gesture.state == .ended { 
     let point = gesture.location(in: self.mapView) 
     let coordinate = self.mapView.convert(point, toCoordinateFrom: self.mapView) 
     print(coordinate) 
     //Now use this coordinate to add annotation on map. 
     var annotation = MKPointAnnotation() 
     annotation.coordinate = coordinate 
     //Set title and subtitle if you want 
     annotation.title = "Title" 
     annotation.subtitle = "subtitle" 
     self.mapView.addAnnotation(annotation) 
    } 
} 
+0

おかげで、私はこの行に、このエラーをgetingているように見える longPressGesture = UILongPressGestureRecognizer(ターゲットみましょう:自己、アクション:#selector(addAnnotationOnLongPress(ジェスチャー:)))addAnnotationOnLongPressのローカル変数の 使用を(ジェスチャー:) 'を宣言する前に – sabrefm1

+0

心配する必要はありません – sabrefm1

+0

座標だけが出力に記録されますが、ピンは削除されません。 – sabrefm1

1

です。

ジェスチャー

func handleTap(_ sender: UIGestureRecognizer) 
{ 
    if sender.state == UIGestureRecognizerState.ended { 

     let touchPoint = sender.location(in: mapView) 
     let touchCoordinate = mapView.convert(touchPoint, toCoordinateFrom: mapView) 
     let annotation = MKPointAnnotation() 
     annotation.coordinate = touchCoordinate 
     annotation.title = "Event place" 
     mapView.removeAnnotations(mapView.annotations) 
    mapView.addAnnotation(annotation) //drops the pin 
    } 
} 
+0

@RohirParsana優れた答え。しかし、私はどのように緯度と経度を取得するのですか?私はこれらを変数に入れたいと思う。最高、フィリップ ちょうどそれを得ました:-) touchCoordinateは緯度と経度を返します。私はこれらを別々に得ることができます。印刷(touchCoordinate.latitude) – PhilipS

0

私は長押しでプットピンと注釈を実装するために、他のこの更新された小さなコードのヘルプを考える:

import UIKit 
    import MapKit 
    class ViewController: UIViewController, MKMapViewDelegate { 

     @IBOutlet weak var map: MKMapView! 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(gestureRecognized:))) 

      //long press (2 sec duration) 
      uilpgr.minimumPressDuration = 2 
      map.addGestureRecognizer(uilpgr) 
     } 

     func longPressed(gestureRecognized: UIGestureRecognizer){ 
      let touchpoint = gestureRecognized.location(in: self.map) 
      let location = map.convert(touchpoint, toCoordinateFrom: self.map) 

      let annotation = MKPointAnnotation() 
      annotation.title = "Latitude: \(location.latitude)" 
      annotation.subtitle = "Longitude: \(location.longitude)" 
      annotation.coordinate = location 
      map.addAnnotation(annotation) 


     } 

     override func didReceiveMemoryWarning() { 
      super.didReceiveMemoryWarning() 
      // Dispose of any resources that can be recreated. 
     } 


    } 
関連する問題