I'm noob in ios and I need implement a draggable MKPointAnnotation
in a MKMapView
using swift. I need an example. I now implement the MKMapView
and add a MKPointAnnotation
in the map (addAnnotation).
我是ios中的noob,我需要使用swift在MKMapView中实现可拖动的MKPointAnnotation。我需要一个例子。我现在实现MKMapView并在地图中添加MKPointAnnotation(addAnnotation)。
My english is bad, help me please!
我的英语不好,请帮帮我!
I found this link,but I didn't understand.
我找到了这个链接,但我不明白。
2 个解决方案
#1
8
You have to create a custom class, derived from MKMapView. This class has to implement the MKMapViewDelegate
protocol.
您必须创建一个派生自MKMapView的自定义类。该类必须实现MKMapViewDelegate协议。
Then you need 2 steps: Create the annotation object and create a view for that annotation.
然后,您需要两个步骤:创建注释对象并为该注释创建视图。
Create Annotation:
创建注释:
Somewhere in your code, depends on your needs:
代码中的某个位置取决于您的需求:
let annotation = MKPointAnnotation()
annotation.setCoordinate(location) // your location here
annotation.title = "My Title"
annotation.subtitle = "My Subtitle"
self.mapView.addAnnotation(annotation)
Create the annotation View
创建注释视图
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKPointAnnotation {
let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")
pinAnnotationView.pinColor = .Purple
pinAnnotationView.draggable = true
pinAnnotationView.canShowCallout = true
pinAnnotationView.animatesDrop = true
return pinAnnotationView
}
return nil
}
#2
4
this is my example code. This code allows you to long press on the map to add a Point and to drag that point until you release your finger from the screen. Also take a look to the gestureRecognizer that you have to add on the map view. Hope this could helps you.
这是我的示例代码。此代码允许您长按地图以添加点并拖动该点,直到您从屏幕上释放手指。另请查看必须在地图视图中添加的gestureRecognizer。希望这可以帮助你。
class TravelLocationMapController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
var dragPin: MKPointAnnotation!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: "addPin:")
gestureRecognizer.numberOfTouchesRequired = 1
mapView.addGestureRecognizer(gestureRecognizer)
}
func addPin(gestureRecognizer:UIGestureRecognizer){
let touchPoint = gestureRecognizer.locationInView(mapView)
let newCoordinates = mapView.convertPoint(touchPoint, toCoordinateFromView: mapView)
if dragPin != nil {
dragPin.coordinate = newCoordinates
}
if gestureRecognizer.state == UIGestureRecognizerState.Began {
dragPin = MKPointAnnotation()
dragPin.coordinate = newCoordinates
mapView.addAnnotation(dragPin)
} else if gestureRecognizer.state == UIGestureRecognizerState.Ended {
dragPin = nil
}
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKPointAnnotation {
let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")
pinAnnotationView.pinTintColor = UIColor.purpleColor()
pinAnnotationView.animatesDrop = true
return pinAnnotationView
}
return nil
}
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
let lat = view.annotation?.coordinate.latitude
let long = view.annotation?.coordinate.longitude
print("Clic pin lat \(lat) long \(long)")
}
#1
8
You have to create a custom class, derived from MKMapView. This class has to implement the MKMapViewDelegate
protocol.
您必须创建一个派生自MKMapView的自定义类。该类必须实现MKMapViewDelegate协议。
Then you need 2 steps: Create the annotation object and create a view for that annotation.
然后,您需要两个步骤:创建注释对象并为该注释创建视图。
Create Annotation:
创建注释:
Somewhere in your code, depends on your needs:
代码中的某个位置取决于您的需求:
let annotation = MKPointAnnotation()
annotation.setCoordinate(location) // your location here
annotation.title = "My Title"
annotation.subtitle = "My Subtitle"
self.mapView.addAnnotation(annotation)
Create the annotation View
创建注释视图
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKPointAnnotation {
let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")
pinAnnotationView.pinColor = .Purple
pinAnnotationView.draggable = true
pinAnnotationView.canShowCallout = true
pinAnnotationView.animatesDrop = true
return pinAnnotationView
}
return nil
}
#2
4
this is my example code. This code allows you to long press on the map to add a Point and to drag that point until you release your finger from the screen. Also take a look to the gestureRecognizer that you have to add on the map view. Hope this could helps you.
这是我的示例代码。此代码允许您长按地图以添加点并拖动该点,直到您从屏幕上释放手指。另请查看必须在地图视图中添加的gestureRecognizer。希望这可以帮助你。
class TravelLocationMapController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
var dragPin: MKPointAnnotation!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: "addPin:")
gestureRecognizer.numberOfTouchesRequired = 1
mapView.addGestureRecognizer(gestureRecognizer)
}
func addPin(gestureRecognizer:UIGestureRecognizer){
let touchPoint = gestureRecognizer.locationInView(mapView)
let newCoordinates = mapView.convertPoint(touchPoint, toCoordinateFromView: mapView)
if dragPin != nil {
dragPin.coordinate = newCoordinates
}
if gestureRecognizer.state == UIGestureRecognizerState.Began {
dragPin = MKPointAnnotation()
dragPin.coordinate = newCoordinates
mapView.addAnnotation(dragPin)
} else if gestureRecognizer.state == UIGestureRecognizerState.Ended {
dragPin = nil
}
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKPointAnnotation {
let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")
pinAnnotationView.pinTintColor = UIColor.purpleColor()
pinAnnotationView.animatesDrop = true
return pinAnnotationView
}
return nil
}
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
let lat = view.annotation?.coordinate.latitude
let long = view.annotation?.coordinate.longitude
print("Clic pin lat \(lat) long \(long)")
}