准备:在控制器上嵌入一个Navigation Controller,在viewDidLoad中加入下面两句
let item = MKUserTrackingBarButtonItem(mapView: mapView)
navigationItem.leftBarButtonItem = item
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
lazy var locationM: CLLocationManager = {
let locationM = CLLocationManager()
if #available(iOS 8.0, *) {
locationM.requestAlwaysAuthorization()
}
return locationM
}()
lazy var geoCoder: CLGeocoder = {
return CLGeocoder()
}()
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.userTrackingMode = MKUserTrackingMode.followWithHeading
_ = locationM
let item = MKUserTrackingBarButtonItem(mapView: mapView)
navigationItem.leftBarButtonItem = item
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
let point = touches.first?.location(in: mapView)
let coordinate = mapView.convert(point!, toCoordinateFrom: mapView)
let annotation = addAnnotation(coordinate, title: "title", subTitle: "subTitle")
let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
geoCoder.reverseGeocodeLocation(location) { (pls: [CLPlacemark]?, error: Error?) -> Void in
if error == nil {
let pl = pls?.first
print(pl)
annotation.title = pl?.locality
annotation.subtitle = pl?.name
}
}
}
func addAnnotation(_ coordinate: CLLocationCoordinate2D, title: String, subTitle: String) -> TGAnnotation {
let annotation: TGAnnotation = TGAnnotation()
annotation.coordinate = coordinate
annotation.title = title
annotation.subtitle = subTitle
mapView.addAnnotation(annotation)
return annotation
}
}
class TGAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2DMake(0, 0)
var title: String?
var subtitle: String?
}
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let annotation = view.annotation
if #available(iOS 9.0, *) {
(view.detailCalloutAccessoryView as! UILabel).text = (annotation?.subtitle)!
}
print("选中了\(annotation?.subtitle)")
}
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
let annotation = view.annotation
print("取消选中了\(annotation?.subtitle)")
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let iden = "item"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: iden)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: iden)
}
annotationView?.annotation = annotation
annotationView?.image = UIImage(named: "category_1")
annotationView?.centerOffset = CGPoint(x: 0, y: 0)
annotationView?.canShowCallout = true
annotationView?.calloutOffset = CGPoint(x: -10, y: 10)
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let image = UIImage(named: "category_2")
imageView.image = image
annotationView?.leftCalloutAccessoryView = imageView
let imageView2 = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let image2 = UIImage(named: "category_3")
imageView2.image = image2
annotationView?.rightCalloutAccessoryView = imageView2
if #available(iOS 9.0, *) {
let detailLabel : UILabel = UILabel()
detailLabel.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
detailLabel.numberOfLines = 0
annotationView?.detailCalloutAccessoryView = detailLabel
}
annotationView?.isDraggable = true
return annotationView
}
}