我试图给用户输入地址并让地图缩放到其中的能力。当使用MapKit时,可以在Swift中使用可拖动的注释
我想让用户将别针拖到另一个位置,然后获取位置信息并将其用于任何内容。
我试过了,我试过了,但是我可以开始工作的最多的是将实际的针脚更改为自定义图像。我已经阅读了几篇文章和教程(全部为Objective-c),即使在转换代码之后,我仍然无法使其工作。
我的自定义注解视图:
import Foundation
import MapKit
class CustomAnnotationView: MKAnnotationView {
var coordinate:CLLocationCoordinate2D!
init(coordinate:CLLocationCoordinate2D) {
super.init()
self.coordinate = coordinate
}
override init(frame: CGRect) {
super.init(frame: CGRect())
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(annotation: MKAnnotation!, reuseIdentifier: String!) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
image = UIImage(named: "pin")
draggable = true
canShowCallout = true
}
func setCoordinate(newCoordinate: CLLocationCoordinate2D){
self.coordinate = newCoordinate;
}
}
我的控制器,里面我的MapView:
import Foundation
import MapKit
class LocationViewController : UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var searchField: UITextField!
@IBOutlet weak var addressLabel: UILabel!
@IBOutlet weak var locationInstructions: UITextView!
@IBAction func didTapGoButton(sender: UIButton) {
var geocoder = CLGeocoder()
geocoder.geocodeAddressString(searchField.text, {(placemarks: [AnyObject]!, error: NSError!) -> Void in
if let placemark = placemarks?[0] as? CLPlacemark {
var region = self.mapView.region as MKCoordinateRegion
region.center = placemark.location.coordinate
region.span.longitudeDelta /= 30.0;
region.span.latitudeDelta /= 30.0;
self.mapView.zoomEnabled = true
self.mapView.scrollEnabled = true
self.mapView.addAnnotation(MKPlacemark(placemark: placemark))
self.mapView.setRegion(region, animated: true)
}
})
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
var pin = mapView.dequeueReusableAnnotationViewWithIdentifier("CustomAnnotationView")
if pin == nil {
NSLog("PIN NIL")
pin = CustomAnnotationView(annotation: annotation, reuseIdentifier: "CustomAnnotationView")
}
else
{
NSLog("PIN NOT NIL")
pin.annotation = annotation;
}
return pin;
}
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
if newState == MKAnnotationViewDragState.Starting
{
view.dragState = MKAnnotationViewDragState.Dragging
}
else if newState == MKAnnotationViewDragState.Ending || newState == MKAnnotationViewDragState.Canceling
{
view.dragState = MKAnnotationViewDragState.None;
}
}
我缺少什么?
我找不到任何成功完成这个任务的Swift示例。
摆脱了CustomAnnotationView。一切正常,但有一件事已经改变。当我点击引脚时,它通常显示我输入的邮政编码区域的名称。但现在它显示我输入到文本区域的内容。无论如何显示关于引脚被绘制的详细信息? – LondonGuy 2014-09-14 04:33:29
好吧搞乱pa.title = placemark.locality等帮助。我想我可以连接一些字符串。我会看看我的选择。谢谢您的帮助 – LondonGuy 2014-09-14 04:40:59