2016-12-02 8 views
1

지도를 방해해야하는 곳에서 iOS 앱이 있습니다. 비트를 검색 한 후 MKMapView 개체를 사용해야하고 아마도 MKMapViewDelegate 프로토콜을 구현해야한다는 결론에 도달했습니다.지도상의 탭 처리

지도에서 사용자가 터치 할 때 터치 포인트 (경도 및 위도)를 캡처 할 수 있는지 궁금합니다. 나는 가정과 함께 주변을 놀리는 것보다 훨씬 좋은 방법이 있다고 생각한다. UITapGestureRecognizer.

import UIKit 
import CoreLocation 
import MapKit 

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate, 
    screenSize: CGRect = UIScreen.mainScreen().bounds, 
    locationManager = CLLocationManager() 
    ......... 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     ......... 

     let mapView = MKMapView(frame: CGRect(origin: CGPoint(x: 0.0, y: 20.0), 
      size: CGSize(width: screenSize.width, 
       height: screenSize.height-70.0))) 
     mapView.delegate = self 
     self.view.addSubview(mapView) 
    } 

    ......... 
} 

내 질문은 :

가 명확하고 간단하게하려면, I로 시작하는 코드의 종류가 무엇인지 나는지도보기 개체에 대한 사용자의 탭을 처리하기 위해 어떻게해야합니까? 나는이 포스트를 쓰기 전에 멍청이를 찾았지만 명확한 해결책이 없었다.

+0

길은 하나 뿐이며'TapGestureRecognizer'를 사용하고 있습니다. –

+0

TapGestureRecognizer로 포인트를 얻은 다음지도의 원점 및 확대/축소 요소를 사용하여지도 좌표로 변환한다는 의미입니까? – Michel

+0

예'Nirav D'의 의견에 동의합니다. 동일한 시나리오를 처리하려고 할 때 많이 찾았지만 마침내'UITapGestureRecognizer'로 돌아 왔습니다. –

답변

0

documentation을 보면 터치를 처리하는 방법이 없습니다.

UITapGestureRecognizer을 사용하여 터치를 감지해야한다고 생각합니다. touchesBegan지도보기가 가로 챌 것으로 생각하기 때문에 테이블보기처럼 작동하지 않습니다.

터치 위치를 감지 한 후 convert(_:toCoordinateFrom:) 메서드를 사용하여지도 뷰의 좌표 공간에있는 CGPoint을지도의 CLLocationCoordinate2D으로 변환합니다.

이 모든 것이 너무 번거로운 경우 Google지도를 대신 사용할 수 있습니다. GMSMapView에는 구현할 수있는 대리 메서드 mapView(_:didTapAt:) 메서드가 있습니다.

0

UITapGestureRecognizerviewDidLoad에 추가하십시오.

let gestureRecognizer = UITapGestureRecognizer(target: self, action:#selector(ViewController.getCoordinatePressOnMap(sender:))) 
    gestureRecognizer.numberOfTapsRequired = 1 
    mapView.addGestureRecognizer(gestureRecognizer) 

getCoordinatePressOnMap 메서드 구현.

@IBAction func getCoordinatePressOnMap(sender: UITapGestureRecognizer) { 
    let touchLocation = sender.location(in: mapView) 
    let locationCoordinate = mapView.convert(touchLocation, toCoordinateFrom: mapView) 
    print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)") 
} 

참고 : toCoordinateFrom : : : _

변환은 (지도 좌표를 지정된 뷰의 좌표계에서 점을 변환합니다.

희망이 당신을 위해 일합니다!