2015-12-23 7 views
10

मैं आईओएस मानचित्र के स्पर्श पर एनोटेशन जोड़ना चाहता हूं और संबंधित स्थान का विस्तृत पता (प्लेमार्क) प्राप्त करना चाहता हूं। स्विफ्ट में मैं इसे कैसे प्राप्त कर सकता हूं?आईओएस: स्विफ्ट - स्पर्श पर मानचित्र पर पिनपॉइंट कैसे जोड़ें और उस स्थान का विस्तृत पता कैसे प्राप्त करें?

अग्रिम धन्यवाद।

उत्तर

17

मानचित्र पर उसे स्पर्श करने के लिए प्रतिक्रिया करने के लिए आप ऊपर viewDidLoad में MapView

के लिए एक नल recogniser निर्धारित करने की आवश्यकता:

let gestureRecognizer = UITapGestureRecognizer(target: self, action:"handleTap:") 
    gestureRecognizer.delegate = self 
    mapView.addGestureRecognizer(gestureRecognizer) 

नल संभाल कर रखें और टैप स्थान निर्देशांक मिलती है:

func handleTap(gestureReconizer: UILongPressGestureRecognizer) { 

    let location = gestureReconizer.locationInView(mapView) 
    let coordinate = mapView.convertPoint(location,toCoordinateFromView: mapView) 

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

अब आपको एनोटेशन आकर्षित करने के लिए केवल एमकेमैपव्यू प्रतिनिधि कार्यों को लागू करना होगा। एक साधारण Google खोज आपको बाकी के बारे में मिलनी चाहिए।

+0

स्विफ्ट 3 में आप के लिए यह काम करता है? – Neo42

+1

मैंने स्विफ्ट 3 @ नियो 42 के लिए एक संपादन जोड़ा है। एक बार पीयर की समीक्षा के बाद आप इसे देख पाएंगे – KyleHodgetts

0

के लिए तेजी से 3,0

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

func handleTap(_ gestureReconizer: UILongPressGestureRecognizer) { 

    let location = gestureReconizer.locationInView(mapView) 
    let coordinate = mapView.convertPoint(location,toCoordinateFromView: mapView) 

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

स्विफ्ट 4:

@IBOutlet weak var mapView: MKMapView! 

func handleLongPress (gestureRecognizer: UILongPressGestureRecognizer{ 
    if gestureRecognizer.state == UIGestureRecognizerState.began { 

     let touchPoint: CGPoint = gestureRecognizer.location(in: mapView) 
     let newCoordinate: CLLocationCoordinate2D = mapView.convert(touchPoint, toCoordinateFrom: mapView) 

     addAnnotationOnLocation(pointedCoordinate: newCoordinate) 
    } 
} 

func addAnnotationOnLocation(pointedCoordinate: CLLocationCoordinate2D{ 

    let annotation = MKPointAnnotation() 
    annotation.coordinate = pointedCoordinate 
    annotation.title = "Loading..." 
    annotation.subtitle = "Loading..." 
    mapView.addAnnotation(annotation) 
} 
संबंधित मुद्दे